From 3f41182984d69d7fae703776edd1591f48dff93f Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期三, 22 十一月 2023 17:13:05 +0800 Subject: [PATCH] 2023年11月22日17:12:40 --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java | 71 +++++++++++++++++++++++++---------- 1 files changed, 51 insertions(+), 20 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java index 5a098d9..6705fe2 100644 --- a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java @@ -30,17 +30,19 @@ private final IClient client; private IHeartbeat iHeartbeat; - public void SetHeartbeat(IHeartbeat iHeartbeat){ - this.iHeartbeat=iHeartbeat; + + public void SetHeartbeat(IHeartbeat iHeartbeat) { + this.iHeartbeat = iHeartbeat; } + /** * 褰撳墠鎺ユ敹鍒版暟鎹殑鏃堕棿 */ - private long time=System.currentTimeMillis(); + private long time = System.currentTimeMillis(); /** * tcp鏄惁宸茬粡杩炴帴 */ - private boolean connected=false; + private boolean connected = false; public IClient getClient() { return client; @@ -54,7 +56,7 @@ private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>(); - public TcpSocketBoot(IClient client) { + public TcpSocketBoot(IClient client) { TCP_SOCKET_BOOT_LIST.add(this); this.client = client; initConnectThread(); @@ -66,25 +68,25 @@ /** * 璁板綍鎵�鏈塖ocketBoot */ - final static List<TcpSocketBoot> TCP_SOCKET_BOOT_LIST = new ArrayList(); + 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; + 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) - { + for (TcpSocketBoot tcpSocketBoot : TCP_SOCKET_BOOT_LIST) { + if (ipAddress.equals(tcpSocketBoot.getClient().getOptions().getIp()) && tcpSocketBoot.getClient().getOptions().getPort() == port) { return tcpSocketBoot; } } - return null; + return null; } /** @@ -92,14 +94,14 @@ */ private synchronized void connect() { try { - LogUtils.i("TCP杩炴帴锛�"+this.getClient().getOptions().getIp()); + LogUtils.i("TCP杩炴帴锛�" + this.getClient().getOptions().getIp()); client.onConnectStatus(ConnectStatus.CONNECTING); // Thread.sleep(700); client.connect(); - LogUtils.i("TCP杩炴帴鎴愬姛锛�"+this.getClient().getOptions().getIp()); - connected=true; + LogUtils.i("TCP杩炴帴鎴愬姛锛�" + this.getClient().getOptions().getIp()); + connected = true; client.onConnectStatus(ConnectStatus.CONNECTED); - }catch(Exception e) { + } catch (Exception e) { LogUtils.e(e.getMessage()); } } @@ -116,7 +118,7 @@ public void run() { while (true) { try { - if(connected==false){ + if (!connected) { Thread.sleep(100); continue; } @@ -163,7 +165,7 @@ if (connected) { //璇诲彇鏁版嵁 client.onHandleResponse(); - time= System.currentTimeMillis(); + time = System.currentTimeMillis(); } else { try { Thread.sleep(1000); @@ -196,7 +198,7 @@ if (!connected) { reconect(); } - Thread.sleep(5*1000); + Thread.sleep(5 * 1000); } catch (Exception e) { LogUtils.e("瀹氭椂杩炴帴绾跨▼寮傚父:" + e.getMessage()); @@ -234,6 +236,7 @@ }); } } + /** * 閲嶆柊杩炴帴 */ @@ -244,6 +247,7 @@ /** * 鍙戦�佹棤闇�鍥炶皟 + * * @param msg 鍙戦�佺殑鏁版嵁 */ public void sendMsg(byte[] msg) { @@ -278,4 +282,31 @@ } } + +// /** +// * 鏂紑鍏ㄩ儴鐨凩ink缃戝叧杩炴帴 +// */ +// public static void stopAllConnectLinkGateway() { +// for (TcpSocketBoot data : TCP_SOCKET_BOOT_LIST) { +// //鏂紑鎸囧畾鐨刲ink缃戝叧杩炴帴 +// stopConnectLinkGateway(data); +// } +// +// } +// +// /** +// * 鏂紑鎸囧畾鐨刲ink缃戝叧杩炴帴 +// * +// * @param tcpSocketBoot tcp瀵硅薄 +// */ +// public static void stopConnectLinkGateway(TcpSocketBoot tcpSocketBoot) { +// synchronized (TCP_SOCKET_BOOT_LIST) { +// try { +// TCP_SOCKET_BOOT_LIST.remove(tcpSocketBoot); +// tcpSocketBoot.client.disconnect(); +// } catch (Exception ignored) { +// } +// } +// } + } -- Gitblit v1.8.0