From 2a01ef5e49422cca49bc7476fc1b8be8c8556561 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期四, 08 九月 2022 10:46:57 +0800
Subject: [PATCH] 最新sdk java版本

---
 HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java |   67 +++++++++++++++++++--------------
 1 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java b/HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java
similarity index 67%
rename from HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java
rename to HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java
index 6fdaabb..045bd84 100644
--- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java
+++ b/HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java
@@ -1,22 +1,21 @@
-package com.hdl.sdk.socket.client;
+package com.hdl.sdk.link.socket.client;
 
-import com.hdl.sdk.common.utils.IpUtils;
-import com.hdl.sdk.common.utils.LogUtils;
-import com.hdl.sdk.common.utils.TextUtils;
-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 com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.common.utils.TextUtils;
+import com.hdl.sdk.link.socket.SocketPool;
+import com.hdl.sdk.link.socket.bean.Packet;
+import com.hdl.sdk.link.socket.udp.UdpSocketBoot;
+import com.hdl.sdk.link.socket.udp.UdpSocketOptions;
+import com.hdl.sdk.link.socket.codec.IHandleMessage;
 
 import java.io.IOException;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
+import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 
 /**
@@ -85,25 +84,27 @@
      * @return
      */
     public static UdpSocketBoot init(int port, UdpSocketOptions options) {
-        return init(null, port, options);
+        return init("0.0.0.0", port, options);
     }
 
-
     @Override
-    public void bind() throws Exception {
+    public synchronized void bind() throws Exception {
         try {
             //宸茬粡缁戝畾杩囧氨涓嶇敤鍐嶇粦瀹�
             if (null != mSocket) {
                 return;
             }
+
             if (TextUtils.isEmpty(ipAddress)) {
-                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port));
+                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port));
+            }
+            else{
+                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(Inet4Address.getByName(ipAddress), port));
             }
             mSocket.setBroadcast(true);
-            mSocket.setReuseAddress(true);
+//            mSocket.setReuseAddress(true);
 
         } catch (Exception e) {
-            LogUtils.e("鍒濆鍖朣ocket 澶辫触锛�" + e.getMessage());
             throw e;
         }
     }
@@ -131,19 +132,21 @@
         }
         try {
             mSocket.receive(receivePacket);
-        } catch (IOException e) {
-            e.printStackTrace();
+        } catch (Exception e) {
+            LogUtils.e("鎺ユ敹鍒癠dp鏁版嵁鍖呭紓甯革紝寮傚父淇℃伅锛�" + e.getMessage());
+            return;
         }
         if (receivePacket.getLength() == 0) {
+            LogUtils.i("鎺ユ敹鍒癠dp鏁版嵁鍖咃紝鏁版嵁鍖呴暱搴︿负0锛�" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
             return;
         }
         //鎺掗櫎鑷繁鍙戝嚭鍘荤殑
-        try {
-            if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
-                return;
-        } catch (Exception ignored) {
-
-        }
+//        try {
+//            if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
+//                return;
+//        } catch (Exception ignored) {
+//
+//        }
 
         try {
             LogUtils.i("鎺ユ敹鍒癠dp鏁版嵁鍖咃紝缃戠粶鍦板潃锛�" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
@@ -152,11 +155,11 @@
             if (handleMessage != null) {
                 byte[] data = new byte[receivePacket.getLength()];
                 System.arraycopy(receivePacket.getData(), 0, data, 0, data.length);
-                handleMessage.read(data,receivePacket.getAddress().getHostAddress());
+                handleMessage.read(new Packet(receivePacket.getAddress().getHostAddress(), data,null));
             }
 
         } catch (Exception e) {
-
+            LogUtils.i("鎺ユ敹鍒癠dp鏁版嵁鍖咃紝澶勭悊寮傚父锛�" + e.getMessage());
         }
     }
 
@@ -167,6 +170,12 @@
             return;
         }
         final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, InetAddress.getByName(ipAddress), port);
-        mSocket.send(sendPacket);
+        if (mSocket != null) {
+            mSocket.send(sendPacket);
+        } else {
+            DatagramSocket datagramSocket = new DatagramSocket();
+            datagramSocket.send(sendPacket);
+            datagramSocket.close();
+        }
     }
 }

--
Gitblit v1.8.0