From 93f13e7b2e046c6d86d10e7abc2ecaa4c4adabc3 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期二, 21 十二月 2021 11:47:38 +0800
Subject: [PATCH] 完善tcp

---
 HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java |   50 ++++++++++++++++++++++++++++++++++----------------
 1 files changed, 34 insertions(+), 16 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 51a03d8..3f3c359 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,6 +15,7 @@
 import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.util.ArrayList;
 import java.util.List;
 
 
@@ -22,6 +24,7 @@
  */
 public final class TcpClient implements IClient {
 
+
     private SocketOptions socketOptions;
 
     private final String ip;
@@ -29,12 +32,32 @@
 
     private Socket mSocket;
 
-    private byte[] readBuffer;
+    private final static List<TcpClient> tcpClientList = new ArrayList();
+
+    /**
+     * 浠庤繛鎺ユ睜涓壘鍑哄綋鍓岻P鍙婄鍙g殑杩炴帴瀹㈡埛绔�
+     * @param ipAdderss 杩炴帴IP鍦板潃
+     * @param port 杩炴帴绔彛
+     * @return
+     */
+    public static TcpClient getTcpClientByIP(String ipAdderss,int port) {
+        for(TcpClient tcpClient:tcpClientList){
+            if(tcpClient.ip.equals(ipAdderss)&&tcpClient.port==port)
+            {
+                return tcpClient;
+            }
+        }
+        return  null;
+    }
+
+    private byte[] readBuffer = new byte[4*1024];
 
     private TcpClient(String ip, int port, SocketOptions socketOptions) {
         this.socketOptions = socketOptions;
         this.ip = ip;
         this.port = port;
+        socketOptions.setIp(ip);
+        socketOptions.setPort(port);
     }
 
     public static SocketBoot init(String ip, int port, SocketOptions options) {
@@ -50,8 +73,11 @@
         mSocket.setTcpNoDelay(true);
         mSocket.setReuseAddress(true);
         mSocket.setKeepAlive(true);
-        readBuffer = new byte[options.getReadMaxBufferSize()];
+
+        tcpClientList.add(this);
     }
+
+
 
 
     @Override
@@ -66,16 +92,6 @@
     }
 
     @Override
-    public boolean isConnect() {
-        if (mSocket == null) {
-            return false;
-        }
-
-        return mSocket.isConnected() && !mSocket.isClosed();
-    }
-
-
-    @Override
     public synchronized SocketOptions getOptions() {
         if (socketOptions == null) {
             socketOptions = new SocketOptions();
@@ -86,13 +102,15 @@
     @Override
     public void onHandleResponse() throws Exception {
         final InputStream stream = getInputStream();
-
         if (stream != null && getOptions() != null) {
-            readBuffer = new byte[1024];
-            while ((getInputStream().read(readBuffer)) != -1) {
+            int len=0;
+            while ( (len=getInputStream().read(readBuffer)) != -1) {
                 IHandleMessage handleMessage = getOptions().getHandleMessage();
                 if (handleMessage != null) {
-                    handleMessage.read(readBuffer);
+                    byte []bytes = new byte[len];
+                    System.arraycopy(readBuffer,0,bytes,0,len);
+                    //瀹屾暣鐨勬暟鎹墠鍥炶皟
+                    handleMessage.read(bytes);
                 }
             }
         }

--
Gitblit v1.8.0