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/udp/UdpSocketBoot.java |  128 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 128 insertions(+), 0 deletions(-)

diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/udp/UdpSocketBoot.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/udp/UdpSocketBoot.java
new file mode 100644
index 0000000..4e039f7
--- /dev/null
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/udp/UdpSocketBoot.java
@@ -0,0 +1,128 @@
+package com.hdl.sdk.link.socket.udp;
+
+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.listener.SendListener;
+import com.hdl.sdk.link.socket.SocketRequest;
+import com.hdl.sdk.link.socket.client.IUdpClient;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Created by hxb on 2021/12/12.
+ */
+public class UdpSocketBoot {
+
+    private final IUdpClient client;
+
+    private final AtomicBoolean isOpenRetry = new AtomicBoolean(false);
+
+    private final AtomicInteger resendCount = new AtomicInteger(0);
+
+    private ExecutorService receiveThread;
+
+    private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>();
+
+
+    public IUdpClient getClient() {
+        return client;
+    }
+
+    public UdpSocketBoot(IUdpClient client) {
+        this.client = client;
+    }
+
+    /**
+     * 缁戝畾 socket
+     * @throws Exception 鍙兘绔彛鍐茬獊
+     */
+    public void bind() throws Exception {
+        client.bind();
+        initReceiveThread();
+    }
+
+    /**
+     * 鍒濆鍖栨帴鏀剁嚎绋�
+     */
+    private void initReceiveThread() {
+        if(null!=receiveThread){
+            return;
+        }
+        receiveThread = ThreadToolUtils.getInstance().newFixedThreadPool(1);
+        receiveThread.execute(new Runnable() {
+            @Override
+            public void run() {
+                while (true) {
+                    try {
+                        client.onHandleResponse();
+                    } catch (Exception e) {
+                       LogUtils.i("鎺ユ敹绾跨▼寮傚父锛�"+e.getMessage());
+                    }
+                }
+            }
+        });
+    }
+
+
+    /**
+     * 鍙戦�佹暟鎹�
+     * @param ipAddress 鐩殑鐨処P鍦板潃
+     * @param port 绔彛
+     * @param msg 鍙戦�佹暟鎹�
+     * @param listener 鍙戦�佸洖璋�
+     */
+    public void sendMsg(String ipAddress,int port,byte[] msg, SendListener listener) {
+        sendMsg(ipAddress,port, msg, true, listener);
+    }
+
+    /**
+     * 鍙戦�佹暟鎹�
+     * @param ipAddress 鐩殑鐨処P鍦板潃
+     * @param port 绔彛
+     * @param msg 鍙戦�佹暟鎹�
+     */
+    public void sendMsg(String ipAddress,int port,byte[] msg) {
+        sendMsg(ipAddress,port, msg, true, null);
+    }
+
+    /**
+     * 鍙戦�佹暟鎹�
+     * @param ipAddress 鐩殑IP鍦板潃
+     * @param port 绔彛
+     * @param msg 鍙戦�佺殑鏁版嵁
+     * @param isRefreshRetry 鏄惁瑕侀噸鍙�
+     * @param listener 鍙戦�佸洖璋�
+     */
+    public void sendMsg(String ipAddress,int port, byte[] msg, boolean isRefreshRetry, SendListener listener) {
+        if (isRefreshRetry) {
+            //閲嶇疆杩炴帴娆℃暟
+            resendCount.set(0);
+        }
+        try {
+            SocketRequest request = new SocketRequest(msg);
+            if (listener != null && !TextUtils.isEmpty(request.getAction())) {
+                sendMap.put(request.getAction(), listener);
+            }
+            client.sendMsg(ipAddress,port, msg);
+        } catch (Exception e) {
+            LogUtils.e("鍙戦�佸け璐�:" + e.getMessage());
+        }
+    }
+
+    /**
+     * 鍏抽棴褰撳墠socket
+     */
+    public synchronized void close() {
+        isOpenRetry.set(false);
+        sendMap.clear();
+        receiveThread.shutdown();
+        receiveThread=null;
+        client.close();
+    }
+}

--
Gitblit v1.8.0