From fdcf461fbfa3bcd650685743e891ad3357898f0c Mon Sep 17 00:00:00 2001
From: 562935844@qq.com
Date: 星期四, 31 八月 2023 17:36:50 +0800
Subject: [PATCH] 更新sdk

---
 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/socket/client/UdpClient.java |  176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 176 insertions(+), 0 deletions(-)

diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/socket/client/UdpClient.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/socket/client/UdpClient.java
new file mode 100644
index 0000000..c6a3aff
--- /dev/null
+++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/socket/client/UdpClient.java
@@ -0,0 +1,176 @@
+package com.hdl.sdk.socket.client;
+
+
+import android.text.TextUtils;
+
+import com.hdl.sdk.common.utils.IpUtils;
+import com.hdl.sdk.common.utils.LogUtils;
+import com.hdl.sdk.socket.SocketBoot;
+import com.hdl.sdk.socket.SocketOptions;
+import com.hdl.sdk.socket.SocketPool;
+import com.hdl.sdk.socket.udp.UdpSocketBoot;
+import com.hdl.sdk.socket.udp.UdpSocketOptions;
+import com.hdl.sdk.socket.codec.IHandleMessage;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+
+/**
+ * Created by hxb on 2021/12/12.
+ */
+public class UdpClient implements IUdpClient {
+
+    /**
+     * 褰撳墠socket
+     */
+    private  DatagramSocket mSocket;
+
+    /**
+     * 鎺ユ敹鏁版嵁鍖�
+     */
+    private DatagramPacket receivePacket;
+
+    /**
+     * 缂撳啿鍖哄ぇ灏�
+     */
+    private final int BUFFER = 2 * 1024;
+
+    /**
+     * 鏈湴鐩戝惉IP鍦板潃
+     */
+    private String ipAddress;
+    /**
+     * 鏈湴鐩戝惉绔彛
+     */
+    private int port;
+
+    /**
+     * socket閰嶇疆淇℃伅
+     */
+    private UdpSocketOptions socketOptions;
+
+    /**
+     * 鍒濆鍖栧弬鏁�
+     * @param ipAddress 鏈湴鐩戝惉绔彛
+     * @param port 鏈湴鐩戝惉绔彛
+     * @param socketOptions
+     */
+    private UdpClient(String ipAddress,int port, UdpSocketOptions socketOptions) {
+        this.ipAddress = ipAddress;
+        this.port = port;
+        this.socketOptions = socketOptions;
+        byte[] receiveByte = new byte[BUFFER];
+        receivePacket = new DatagramPacket(receiveByte, receiveByte.length);
+    }
+
+    /**
+     * 鍒濆鍖栧弬鏁�
+     * @param ipAddress 鏈湴鐩戝惉IP鍦板潃
+     * @param port 鏈湴鐩戝惉绔彛
+     * @param options
+     * @return
+     */
+    public static UdpSocketBoot init(String ipAddress, int port, UdpSocketOptions options) {
+        return new UdpSocketBoot(new UdpClient(ipAddress, port, options));
+    }
+
+    /**
+     * 鍒濆鍖栧弬鏁�
+     * @param port 鏈湴鐩戝惉绔彛
+     * @param options
+     * @return
+     */
+    public static UdpSocketBoot init(int port, UdpSocketOptions options) {
+        return init("0.0.0.0", port, options);
+    }
+
+
+    @Override
+    public void bind() throws Exception {
+        try {
+            //宸茬粡缁戝畾杩囧氨涓嶇敤鍐嶇粦瀹�
+            if (null != mSocket) {
+                return;
+            }
+            if (TextUtils.isEmpty(ipAddress)) {
+                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port));
+            }else {
+                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(ipAddress,port));
+            }
+            mSocket.setBroadcast(true);
+//            mSocket.setReuseAddress(true);
+
+        } catch (Exception e) {
+            LogUtils.e("鍒濆鍖朣ocket 澶辫触锛�" + e.getMessage());
+            throw e;
+        }
+    }
+
+    @Override
+    public boolean close() {
+        try {
+            mSocket.close();
+        } catch (Exception e) {
+
+        }
+        mSocket = null;
+        return true;
+    }
+
+    @Override
+    public UdpSocketOptions getOptions() {
+        return this.socketOptions;
+    }
+
+    @Override
+    public void onHandleResponse() throws Exception {
+        if (mSocket == null) {
+            return;
+        }
+        try {
+            mSocket.receive(receivePacket);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        if (receivePacket.getLength() == 0) {
+            return;
+        }
+        //鎺掗櫎鑷繁鍙戝嚭鍘荤殑
+        try {
+            if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
+                return;
+        } catch (Exception ignored) {
+
+        }
+
+        try {
+//            LogUtils.i("鎺ユ敹鍒癠dp鏁版嵁鍖咃紝缃戠粶鍦板潃锛�" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
+
+            IHandleMessage handleMessage = getOptions().getHandleMessage();
+            if (handleMessage != null) {
+                byte[] data = new byte[receivePacket.getLength()];
+                System.arraycopy(receivePacket.getData(), 0, data, 0, data.length);
+                handleMessage.read(data,receivePacket.getAddress().getHostAddress());
+            }
+
+        } catch (Exception e) {
+
+        }
+    }
+
+
+    @Override
+    public void sendMsg(String ipAddress,int port, byte[] msg) throws Exception {
+        if (msg == null) {
+            return;
+        }
+        final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, InetAddress.getByName(ipAddress), port);
+        mSocket.send(sendPacket);
+    }
+}

--
Gitblit v1.8.0