From 2a01ef5e49422cca49bc7476fc1b8be8c8556561 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期四, 08 九月 2022 10:46:57 +0800 Subject: [PATCH] 最新sdk java版本 --- HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java | 80 +++++++++++++++++++++++++++------------- 1 files changed, 54 insertions(+), 26 deletions(-) diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java b/HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java similarity index 78% rename from HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java rename to HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java index 0965fc7..2c21959 100644 --- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java +++ b/HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java @@ -1,14 +1,18 @@ -package com.hdl.sdk.socket; +package com.hdl.sdk.link.socket; -import com.hdl.sdk.common.utils.LogUtils; -import com.hdl.sdk.common.utils.TextUtils; -import com.hdl.sdk.common.utils.ThreadToolUtils; -import com.hdl.sdk.socket.annotation.ConnectStatus; -import com.hdl.sdk.socket.client.IClient; -import com.hdl.sdk.socket.client.IHeartbeat; -import com.hdl.sdk.socket.listener.SendListener; +import com.hdl.sdk.link.HDLLinkLocalSdk; +import com.hdl.sdk.link.common.utils.LogUtils; +import com.hdl.sdk.link.common.utils.TextUtils; +import com.hdl.sdk.link.common.utils.ThreadToolUtils; +import com.hdl.sdk.link.socket.client.IClient; +import com.hdl.sdk.link.socket.client.IHeartbeat; +import com.hdl.sdk.link.socket.listener.SendListener; +import com.hdl.sdk.link.socket.annotation.ConnectStatus; + +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; @@ -18,7 +22,7 @@ * Created by Tong on 2021/9/26. * Tcp/Udp 鍚姩鍣� */ -public class SocketBoot { +public class TcpSocketBoot { private ExecutorService connectThread; private ExecutorService sendThread; @@ -33,8 +37,7 @@ /** * 褰撳墠鎺ユ敹鍒版暟鎹殑鏃堕棿 */ - private static long time=System.currentTimeMillis(); - + private long time=System.currentTimeMillis(); /** * tcp鏄惁宸茬粡杩炴帴 */ @@ -48,12 +51,37 @@ private final Map<String, SendListener> sendMap = new HashMap<>(); - public SocketBoot(IClient client) { + public TcpSocketBoot(IClient client) { + TCP_SOCKET_BOOT_LIST.add(this); this.client = client; initConnectThread(); initReceiveThread(); initSendThread(); initHeartbeat(); + } + + /** + * 璁板綍鎵�鏈塖ocketBoot + */ + final static List<TcpSocketBoot> TCP_SOCKET_BOOT_LIST = new ArrayList(); + + /** + * 鏍规嵁IP鍦板潃鍙婄鍙h幏鍙栧綋鍓峴ocketBoot + * @param ipAddress + * @param port + * @return + */ + public static TcpSocketBoot getByEndPoint(String ipAddress, int port){ + if(ipAddress==null){ + return null; + } + for(TcpSocketBoot tcpSocketBoot : TCP_SOCKET_BOOT_LIST){ + if(ipAddress.equals(tcpSocketBoot.getClient().getOptions().getIp())&& tcpSocketBoot.getClient().getOptions().getPort()==port) + { + return tcpSocketBoot; + } + } + return null; } /** @@ -63,13 +91,13 @@ try { LogUtils.i("TCP杩炴帴"); client.onConnectStatus(ConnectStatus.CONNECTING); - Thread.sleep(1000); +// Thread.sleep(700); client.connect(); - connected=true; LogUtils.i("TCP杩炴帴鎴愬姛"); + connected=true; client.onConnectStatus(ConnectStatus.CONNECTED); }catch(Exception e) { - LogUtils.e("杩炴帴寮傚父"+e.getMessage()); + LogUtils.e(e.getMessage()); } } @@ -85,9 +113,11 @@ public void run() { while (true) { try { + if(connected==false){ + Thread.sleep(100); + continue; + } SocketRequest socketRequest = mMessageQueue.take(); -// final String sendStr = new String(socketRequest.getData(), 0, socketRequest.getData().length); -// LogUtils.i("鍙戦�佹暟鎹細" + sendStr); final String action = socketRequest.getAction(); try { client.sendMsg(socketRequest.getData()); @@ -108,7 +138,7 @@ } } } catch (Exception e) { - + LogUtils.e("鍙戦�佸け璐�1:" + e.getMessage()); } } } @@ -163,9 +193,9 @@ if (!connected) { reconect(); } - Thread.sleep(3*1000); + Thread.sleep(5*1000); } catch (Exception e) { - LogUtils.e("瀹氭椂杩炴帴澶辫触:" + e.getMessage()); + } } } @@ -184,14 +214,14 @@ public void run() { while (true) { try { - //10绉� - if (connected && 10 * 1000 < (System.currentTimeMillis() - time)) { + //5绉� + if (connected && 5 * 1000 < (System.currentTimeMillis() - time)) { time = System.currentTimeMillis(); //蹇冭烦妫�娴� if (iHeartbeat != null) iHeartbeat.heartbeat(); } - Thread.sleep(1000); + Thread.sleep(10); } catch (Exception e) { LogUtils.e("瀹氭椂蹇冭烦妫�娴嬬綉鍏冲紓甯革細" + e.getMessage()); } @@ -200,8 +230,6 @@ }); } } - - /** * 閲嶆柊杩炴帴 */ @@ -230,7 +258,7 @@ } mMessageQueue.put(request); } catch (Exception e) { - LogUtils.e("鍙戦�佹暟鎹け璐ワ細" + e.getMessage()); + } } -- Gitblit v1.8.0