package com.basic.security.utils; import android.os.SystemClock; import com.basic.security.manager.OfficeDeviceManager; import com.basic.security.utils.yuv.BitmapListener; import com.basic.security.utils.yuv.YuvToJpgFrameBufferManager; import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.util.HashMap; import java.util.Map; public class SocketUtil { public ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); public Socket socket = null; public byte[] buffer = new byte[1]; // public BufferedInputStream inputStream; // public BufferedOutputStream outputStream; public DataInputStream dataInputStream; public DataOutputStream dataOutStream; public ObjectOutputStream objectOutputStream; public ObjectInputStream objectInputStream; public YuvToJpgFrameBufferManager mBufferManager; public byte[] imageBuff; public String remoteIp = ""; public String deviceId = ""; boolean useJson = true; public SocketUtil() { } public Map readJsonAsMap() throws Exception { String json = null; int len = 0; while ((len = dataInputStream.read(buffer)) != -1) { for (int i = 0; i < len; i++) { if (buffer[i] == '\0') { Map messageMap = new HashMap<>(); try { byte[] jsonByteArray = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.reset(); json = new String(jsonByteArray, 0, jsonByteArray.length); json = json.substring(0, json.lastIndexOf("}") + 1); if (json.length() > 3) { // System.out.println(FrameUtil.getFrames("Client")+" json="+json); JsonParser parser = new JsonParser(); boolean isJSON = true; JsonElement element = null; try { element = parser.parse(json); } catch (JsonParseException e) { System.out.println("exception: " + e); isJSON = false; } if (isJSON && element != null) { JsonObject obj = element.getAsJsonObject(); for (Map.Entry entry : obj.entrySet()) { if (!(entry.getValue() instanceof JsonNull)) { messageMap.put(entry.getKey(), entry.getValue().getAsString()); } } } } } catch (Exception e) { e.printStackTrace(); } return messageMap; } else { byteArrayOutputStream.write(buffer[i]); } } } throw new RuntimeException("inputStream.read(buffer) == -1"); } public void close() { try { // if (outputStream != null) { // outputStream.close(); // outputStream = null; // } // if (inputStream != null) { // inputStream.close(); // inputStream = null; // } if (socket != null) { socket.close(); socket = null; } if (byteArrayOutputStream != null) { byteArrayOutputStream.close(); byteArrayOutputStream = null; } if (mBufferManager != null) { mBufferManager.close(); mBufferManager = null; } } catch (IOException e) { } } public void setSocket(Socket socket) throws Exception { this.socket = socket; // socket.setSoTimeout(10*1000); this.remoteIp = (((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/", ""); ; BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream outputStream = new BufferedOutputStream(socket.getOutputStream()); dataInputStream = new DataInputStream(socket.getInputStream()); dataOutStream = new DataOutputStream(outputStream); if (byteArrayOutputStream == null) { byteArrayOutputStream = new ByteArrayOutputStream(); } byteArrayOutputStream.reset(); } public void openSocket(String ip, int port) throws Exception { // System.out.println("SocketUtil.openSocket " + FrameUtil.getFrames("Client")); close(); SystemClock.sleep(Constants.reconnectInMilliSeconds); socket = new Socket(); // socket.setSoTimeout(10*1000); socket.connect(new InetSocketAddress(ip, port), 10000); this.setSocket(socket); } // public void writeEmptyMessage() throws Exception { // outputStream.write('\0'); // outputStream.flush(); // } public void writeMessage(String key, String value) throws Exception { if (useJson) { JsonObject jsonObj = new JsonObject(); jsonObj.addProperty(key, value); dataOutStream.write(jsonObj.toString().getBytes()); dataOutStream.write('\0'); dataOutStream.flush(); } else { // JsonObject jsonObj = new JsonObject(); // jsonObj.addProperty(key, value); // outputStream.write(jsonObj.toString().getBytes()); // outputStream.write('\0'); // outputStream.flush(); } } public void writeMessage(Map keyValues) throws Exception { JsonObject jsonObj = new JsonObject(); for (Map.Entry entry : keyValues.entrySet()) { jsonObj.addProperty(entry.getKey(), entry.getValue()); } // System.out.println("SocketUtil.writeMessage json="+jsonObj); dataOutStream.write(jsonObj.toString().getBytes()); dataOutStream.write('\0'); dataOutStream.flush(); } public void writeByteArray(byte[] byteArray) throws Exception { if (byteArray != null) { dataOutStream.write(byteArray); dataOutStream.flush(); } } public void readFullByteArray(byte[] imageBuff) throws Exception { // if (dataInputStream == null) { // dataInputStream = new DataInputStream(inputStream); // } dataInputStream.readFully(imageBuff, 0, imageBuff.length); } public void readFrameInfo() throws Exception { Map messageMap = this.readJsonAsMap(); int length = Integer.parseInt(messageMap.get("length")); int width = Integer.parseInt(messageMap.get("width")); int height = Integer.parseInt(messageMap.get("height")); imageBuff = new byte[length]; mBufferManager = new YuvToJpgFrameBufferManager(length, width, height); } public boolean readBitmapHeader(Map frameInfo) { boolean success = frameInfo.get(Constants.SUCCESS).equals(Constants.TRUE); if (success) { int length = Integer.parseInt(frameInfo.get("length")); int width = Integer.parseInt(frameInfo.get("width")); int height = Integer.parseInt(frameInfo.get("height")); imageBuff = new byte[length]; mBufferManager = new YuvToJpgFrameBufferManager(length, width, height); } return success; } public boolean readBitmapHeader() throws Exception { return readBitmapHeader((Map) this.readObject()); } public void setBitmapListener(BitmapListener cameraDataListener) { mBufferManager.setOnDataListener(cameraDataListener); } public void readFrames(BitmapListener cameraDataListener) throws Exception { readFrameInfo(); setBitmapListener(cameraDataListener); while (!Thread.currentThread().isInterrupted()) { this.readFullByteArray(imageBuff); mBufferManager.fillBuffer(imageBuff, imageBuff.length); } } public void readFrame(BitmapListener yuvToJpgFrameListener) throws Exception { readFrameInfo(); setBitmapListener(yuvToJpgFrameListener); while (!Thread.currentThread().isInterrupted()) { this.readFullByteArray(imageBuff); mBufferManager.fillBuffer(imageBuff, imageBuff.length); break; } } public void writeFrameInfo() throws Exception { Map map = new HashMap<>(); map.put("type", "data"); map.put("length", "460800"); map.put("width", "640"); map.put("height", "480"); map.put("rotation", (Constants.isHuaWeiPad ? 270 : 90) + ""); this.writeMessage(map); } public Map getFrameInfo() { Map frameInfo = new HashMap<>(); frameInfo.put("type", "data"); frameInfo.put("length", "460800"); frameInfo.put("width", "640"); frameInfo.put("height", "480"); frameInfo.put("rotation", (Constants.isHuaWeiPad ? 270 : 90) + ""); return frameInfo; } public void writeObject(Object object) throws Exception { if (useJson) { writeMessage((Map) object); } else { // if (objectOutputStream == null) { // objectOutputStream = new ObjectOutputStream(outputStream); // } // objectOutputStream.writeObject(object); // objectOutputStream.flush(); } } public void writeOneBitmap(Object object) throws Exception { if (useJson) { writeByteArray((byte[]) object); } else { // if (objectOutputStream == null) { // objectOutputStream = new ObjectOutputStream(outputStream); // } // objectOutputStream.writeObject(object); // objectOutputStream.flush(); } } public void readObject(BitmapListener cameraDataListener) throws Exception { readFrameInfo(); setBitmapListener(cameraDataListener); while (!Thread.currentThread().isInterrupted()) { this.readFullByteArray(imageBuff); mBufferManager.fillBuffer(imageBuff, imageBuff.length); } } public void readObject1(BitmapListener cameraDataListener) throws Exception { setBitmapListener(cameraDataListener); while (!Thread.currentThread().isInterrupted()) { this.readFullByteArray(imageBuff); mBufferManager.fillBuffer(imageBuff, imageBuff.length); } } public Object readObject() throws Exception { if (useJson) { return readJsonAsMap(); } else { return readJsonAsMap(); // if (objectInputStream == null) { // objectInputStream = new ObjectInputStream(inputStream); // } // return objectInputStream.readObject(); } } public boolean remoteIpAllowed() { return OfficeDeviceManager.remoteIpAllowed(deviceId); } public boolean remoteIpAllowed(Map map) { if (remoteIpAllowed()) { map.put(Constants.SUCCESS, Constants.TRUE); } else { map.put(Constants.SUCCESS, Constants.FALSE); } return OfficeDeviceManager.remoteIpAllowed(deviceId); } public boolean writeBitmapHeader() throws Exception { boolean success; Map map = new HashMap<>(); if (remoteIpAllowed()) { map.put(Constants.SUCCESS, Constants.TRUE); map.put("type", "data"); map.put("length", "460800"); map.put("width", "640"); map.put("height", "480"); // map.put("rotation", (Constants.isHuaWeiPad ? 270 : 90) + ""); map.put("rotation", "270"); success = true; } else { map.put(Constants.SUCCESS, Constants.FALSE); success = false; } if (useJson) { writeMessage(map); } else { writeObject(map); } return success; } public void readOneBitmap() throws Exception { if (useJson) { readFullByteArray(imageBuff); mBufferManager.fillBuffer(imageBuff, imageBuff.length); } else { byte[] frameData = (byte[]) this.readObject(); mBufferManager.fillBuffer(frameData, frameData.length); } } public boolean writeMap(String key, String value) throws Exception { boolean success = false; Map map = new HashMap<>(); if (remoteIpAllowed()) { if (key != null && value != null) { map.put(key, value); } map.put(Constants.SUCCESS, Constants.TRUE); success = true; } else { map.put(Constants.SUCCESS, Constants.FALSE); success = false; } writeObject(map); return success; } public void writeMap(Map map) throws Exception { if (useJson) { writeMessage(map); } else { writeObject(map); } } public Map readMap() throws Exception { return (Map) readObject(); } public Object readMapValueByKey(String key) throws Exception { return ((Map) readObject()).get(key); } public String readMapStringValueByKey(String key) throws Exception { if (useJson) { return readJsonAsMap().get(key); } else { return (String) ((Map) readObject()).get(key); } } public Object getBitmapHeader() { Map map = new HashMap<>(); map.put(Constants.SUCCESS, Constants.TRUE); map.put("type", "data"); map.put("length", "460800"); map.put("width", "640"); map.put("height", "480"); map.put("rotation", (Constants.isHuaWeiPad ? 270 : 90) + ""); return map; } }