From e7b8a808c2274e9c4329092bb752c7ea5cb035fc Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 13 十二月 2021 14:20:20 +0800
Subject: [PATCH] 2021-12-13 1.优化发送,和认证失败错误码处理

---
 HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/udp/UdpSocketBoot.java |  133 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 133 insertions(+), 0 deletions(-)

diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/udp/UdpSocketBoot.java b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/udp/UdpSocketBoot.java
new file mode 100644
index 0000000..44bbf86
--- /dev/null
+++ b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/udp/UdpSocketBoot.java
@@ -0,0 +1,133 @@
+package com.hdl.sdk.socket.udp;
+
+import android.text.TextUtils;
+
+import androidx.collection.ArrayMap;
+
+import com.hdl.sdk.common.utils.LogUtils;
+import com.hdl.sdk.common.utils.ThreadToolUtils;
+import com.hdl.sdk.socket.SocketRequest;
+import com.hdl.sdk.socket.annotation.ConnectStatus;
+import com.hdl.sdk.socket.client.IClient;
+import com.hdl.sdk.socket.client.IUdpClient;
+import com.hdl.sdk.socket.listener.SendListener;
+
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingDeque;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+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 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.i("鍙戦�佸け璐�:" + e.getMessage());
+        }
+    }
+
+    /**
+     * 鍏抽棴褰撳墠socket
+     */
+    public synchronized void close() {
+        isOpenRetry.set(false);
+        sendMap.clear();
+        receiveThread.shutdown();
+        receiveThread=null;
+        client.close();
+    }
+}

--
Gitblit v1.8.0