From 08c7b9221a5230206c0ae164ecd750a9757efbd4 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期二, 07 十二月 2021 15:53:30 +0800
Subject: [PATCH] 第一版本

---
 HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 101 insertions(+), 8 deletions(-)

diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
index 3cd750c..0f9fcae 100644
--- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
+++ b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
@@ -2,6 +2,7 @@
 
 
 
+import com.hdl.sdk.common.utils.ByteUtils;
 import com.hdl.sdk.common.utils.ThreadToolUtils;
 import com.hdl.sdk.socket.SocketBoot;
 import com.hdl.sdk.socket.SocketOptions;
@@ -14,7 +15,7 @@
 import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.net.Socket;
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.List;
 
 
@@ -22,6 +23,11 @@
  * Created by Tong on 2021/9/15.
  */
 public final class TcpClient implements IClient {
+    /*
+     *鎺ユ敹鏁版嵁鐨勭紦鍐插尯
+     */
+    private final List<Byte> byteList;
+    private final byte[] head = "Topic:".getBytes();
 
     private SocketOptions socketOptions;
 
@@ -33,6 +39,7 @@
     private byte[] readBuffer;
 
     private TcpClient(String ip, int port, SocketOptions socketOptions) {
+        this.byteList = new ArrayList<>();
         this.socketOptions = socketOptions;
         this.ip = ip;
         this.port = port;
@@ -84,18 +91,104 @@
         return socketOptions;
     }
 
+    /// <summary>
+    /// 鑾峰彇鍐呭闀垮害
+    /// </summary>
+    /// <param name="topMsgs"></param>
+    /// <returns></returns>
+    int getLenght(String[] topMsgs)
+    {
+        for (int i = 0; i < topMsgs.length; i++)
+        {
+            String topMsg = topMsgs[i].trim();
+            if (topMsg.startsWith("Length:"))
+            {
+                return Integer.parseInt(topMsg.replace("Length:", ""));
+            }
+        }
+        //鎵句笉鍒伴暱搴�
+        return -1;
+    }
+
+    /// <summary>
+    /// 鑾峰彇涓婚
+    /// </summary>
+    /// <param name="topMsgs"></param>
+    /// <returns></returns>
+    private String getTopic(String[] topMsgs)
+    {
+        for (int i = 0; i < topMsgs.length; i++)
+        {
+            String topMsg = topMsgs[i].trim();
+            if (topMsg.startsWith("Topic:"))
+            {
+                return topMsg.replace("Topic:", "");
+            }
+        }
+        //鎵句笉鍒颁富棰�
+        return null;
+    }
+
+    /**
+     * 鑾峰彇鏁版嵁鐨勫紑濮嬩綅缃�
+     * @param arrayList 鎺ユ敹鍒扮殑鎵�鏈夋暟鎹�
+     * @return 鏁版嵁浣嶇殑寮�濮嬬储寮�
+     */
+    int getDataIndex(List<Byte> arrayList)
+    {
+        byte r = (byte)'\r';
+        byte n = (byte)'\n';
+        for (int i = 0; i < arrayList.size(); i++)
+        {
+            //鎵惧嚭鏁版嵁鍐呭鍓嶉潰鐨勪袱涓崲琛�
+            if (3 <= i && arrayList.get(i - 3) == r && arrayList.get(i - 2) == n && arrayList.get(i - 1) == r && arrayList.get(i) == n)
+            {
+                //鍓╀綑鐨勬暟鎹�
+                return i + 1;
+            }
+        }
+        return -1;
+    }
+
+    void initReceiveData(List<Byte> list) {
+
+        int index = 0;
+        boolean isMatch=false;
+        for (; index < list.size() - head.length; index++) {
+            isMatch=true;
+            for (int j = 0, k = 0; j < head.length; j++, k++) {
+                if (head[j] != list.get(index + k)) {
+                    isMatch=false;
+                    break;
+                }
+            }
+            if(isMatch) {
+                break;
+            }
+        }
+
+        if (0 < index&&isMatch) {
+            List<Byte> tempList = new ArrayList<Byte>();
+            for(int i=index;i<list.size();i++)
+            {
+                tempList.add(list.get(index));
+            }
+            list=tempList;
+        }
+
+    }
     @Override
     public void onHandleResponse() throws Exception {
         final InputStream stream = getInputStream();
-
         if (stream != null && getOptions() != null) {
-            readBuffer = new byte[1024];
-            int len;
-            while ((len = getInputStream().read(readBuffer)) != -1) {
+            int len=0;
+            while ( (len=getInputStream().read(readBuffer)) != -1) {
                 IHandleMessage handleMessage = getOptions().getHandleMessage();
-                if (handleMessage != null && len > 0) {
-                    handleMessage.read(Arrays.copyOfRange(readBuffer, 0, len));
-//                    handleMessage.read(readBuffer);
+                if (handleMessage != null) {
+                    byte []bytes = new byte[len];
+                    System.arraycopy(readBuffer,0,bytes,0,len);
+                    //瀹屾暣鐨勬暟鎹墠鍥炶皟
+                    handleMessage.read(bytes);
                 }
             }
         }

--
Gitblit v1.8.0