From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 17:14:51 +0800 Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc"" --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java | 281 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 281 insertions(+), 0 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 new file mode 100644 index 0000000..5a098d9 --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java @@ -0,0 +1,281 @@ +package com.hdl.sdk.link.socket; + +import android.text.TextUtils; + +import androidx.collection.ArrayMap; + +import com.hdl.sdk.link.common.utils.LogUtils; +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.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingDeque; + +/** + * Created by Tong on 2021/9/26. + * Tcp/Udp 鍚姩鍣� + */ +public class TcpSocketBoot { + + private ExecutorService connectThread; + private ExecutorService sendThread; + private ExecutorService receiveThread; + private ExecutorService heartbeatThread; + + private final IClient client; + private IHeartbeat iHeartbeat; + public void SetHeartbeat(IHeartbeat iHeartbeat){ + this.iHeartbeat=iHeartbeat; + } + /** + * 褰撳墠鎺ユ敹鍒版暟鎹殑鏃堕棿 + */ + private long time=System.currentTimeMillis(); + /** + * tcp鏄惁宸茬粡杩炴帴 + */ + private boolean connected=false; + + public IClient getClient() { + return client; + } + + public boolean isConnected() { + return connected; + } + + private final BlockingQueue<SocketRequest> mMessageQueue = new LinkedBlockingDeque<>(); + + private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>(); + + 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; + } + + /** + * 杩炴帴tcp锛屽唴閮ㄧ淮鎶ゆ帀锛屽彲浠ヤ笉鐢ㄥ紑鏀惧閮紝鏍规嵁杩欎釜涓氬姟鎴戠壒鎬у鐞嗗ソ + */ + private synchronized void connect() { + try { + 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; + client.onConnectStatus(ConnectStatus.CONNECTED); + }catch(Exception e) { + LogUtils.e(e.getMessage()); + } + } + + + /** + * 鍒濆鍖栧彂閫佺嚎绋嬶紝鍙渶瑕佸垵濮嬪寲涓�娆� + */ + private void initSendThread() { + if (sendThread == null) { + sendThread = ThreadToolUtils.getInstance().newFixedThreadPool(1); + sendThread.execute(new Runnable() { + @Override + public void run() { + while (true) { + try { + if(connected==false){ + Thread.sleep(100); + continue; + } + SocketRequest socketRequest = mMessageQueue.take(); + final String action = socketRequest.getAction(); + try { + client.sendMsg(socketRequest.getData()); + if (!TextUtils.isEmpty(action)) { + SendListener sendListener = sendMap.get(action); + if (sendListener != null) { + sendListener.onSucceed(); + } + } + } catch (Exception e) { + connected = false; + LogUtils.e("鍙戦�佸け璐�:" + e.getMessage()); + if (!TextUtils.isEmpty(action)) { + SendListener sendListener = sendMap.get(action); + if (sendListener != null) { + sendListener.onError(); + } + } + } + } catch (Exception e) { + LogUtils.e("鍙戦�佸け璐�1:" + e.getMessage()); + } + } + } + }); + } + } + + /** + * 鍒濆鍖栨帴鏀剁嚎绋嬶紝鍙渶瑕佸垵濮嬪寲涓�娆� + */ + public void initReceiveThread() { + if (receiveThread == null) { + receiveThread = ThreadToolUtils.getInstance().newFixedThreadPool(1); + receiveThread.execute(new Runnable() { + @Override + public void run() { + while (true) { + try { + if (connected) { + //璇诲彇鏁版嵁 + client.onHandleResponse(); + time= System.currentTimeMillis(); + } else { + try { + Thread.sleep(1000); + } catch (Exception ee) { + + } + } + } catch (Exception e) { + connected = false; + LogUtils.e("鎺ユ敹鏁版嵁绾跨▼寮傚父" + e.getMessage()); + } + } + } + }); + } + } + + /** + * 鍒濆鍖栭噸鏂拌繛鎺ョ嚎绋� + */ + private void initConnectThread() { + if (connectThread == null) { + connectThread = ThreadToolUtils.getInstance().newFixedThreadPool(1); + //涓�瀹氭椂闂存娴嬩竴娆¤繛鎺ユ儏鍐碉紝娌℃湁杩炴帴灏辨墽琛岃繛鎺ワ紝杩炴帴缁熶竴鐢辫繖閲岀淮鎶� + connectThread.execute(new Runnable() { + @Override + public void run() { + while (true) { + try { + if (!connected) { + reconect(); + } + Thread.sleep(5*1000); + } catch (Exception e) { + + LogUtils.e("瀹氭椂杩炴帴绾跨▼寮傚父:" + e.getMessage()); + } + } + } + }); + } + } + + /** + * 鍒濆鍖栭噸鏂板績璺崇嚎绋� + */ + private void initHeartbeat() { + if (heartbeatThread == null) { + heartbeatThread = ThreadToolUtils.getInstance().newFixedThreadPool(1); + heartbeatThread.execute(new Runnable() { + @Override + public void run() { + while (true) { + try { + //5绉� + if (connected && 5 * 1000 < (System.currentTimeMillis() - time)) { + time = System.currentTimeMillis(); + //蹇冭烦妫�娴� + if (iHeartbeat != null) + iHeartbeat.heartbeat(); + } + Thread.sleep(10); + } catch (Exception e) { + LogUtils.e("瀹氭椂蹇冭烦妫�娴嬬綉鍏冲紓甯革細" + e.getMessage()); + } + } + } + }); + } + } + /** + * 閲嶆柊杩炴帴 + */ + private void reconect() { + disconnect(); + connect(); + } + + /** + * 鍙戦�佹棤闇�鍥炶皟 + * @param msg 鍙戦�佺殑鏁版嵁 + */ + public void sendMsg(byte[] msg) { + sendMsg(msg, null); + } + + + /** + * @param listener 涓�鑸儏鍐垫棤闇�鐩戝惉 + */ + public void sendMsg(byte[] msg, SendListener listener) { + try { + SocketRequest request = new SocketRequest(msg); + if (listener != null && !TextUtils.isEmpty(request.getAction())) { + sendMap.put(request.getAction(), listener); + } + mMessageQueue.put(request); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 鍏抽棴杩炴帴 + */ + private synchronized void disconnect() { + try { + client.disconnect(); + //鏂紑杩炴帴 + client.onConnectStatus(ConnectStatus.DISCONNECT); + } catch (Exception e) { + + } + } +} -- Gitblit v1.8.0