From 7c8ce9b9a7d3fc1aaa4a621e86415b25ad10a34f Mon Sep 17 00:00:00 2001
From: panlili2024 <14743743+panlili2024@user.noreply.gitee.com>
Date: 星期三, 19 三月 2025 09:36:28 +0800
Subject: [PATCH] 添加source屏扫码流程

---
 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/socket/client/UdpClient.java |   44 +++++++++++++++++++++++++++-----------------
 1 files changed, 27 insertions(+), 17 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
index c6a3aff..28db4dc 100644
--- 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
@@ -1,24 +1,23 @@
 package com.hdl.sdk.socket.client;
 
 
+import android.content.Context;
+import android.net.wifi.WifiManager;
 import android.text.TextUtils;
 
+import com.hdl.sdk.common.HDLSdk;
 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.codec.IHandleMessage;
 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;
 
 
 /**
@@ -29,13 +28,14 @@
     /**
      * 褰撳墠socket
      */
-    private  DatagramSocket mSocket;
+    private DatagramSocket mSocket;
 
     /**
      * 鎺ユ敹鏁版嵁鍖�
      */
     private DatagramPacket receivePacket;
 
+    private WifiManager.MulticastLock lock;
     /**
      * 缂撳啿鍖哄ぇ灏�
      */
@@ -57,22 +57,28 @@
 
     /**
      * 鍒濆鍖栧弬鏁�
-     * @param ipAddress 鏈湴鐩戝惉绔彛
-     * @param port 鏈湴鐩戝惉绔彛
+     *
+     * @param ipAddress     鏈湴鐩戝惉绔彛
+     * @param port          鏈湴鐩戝惉绔彛
      * @param socketOptions
      */
-    private UdpClient(String ipAddress,int port, UdpSocketOptions 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);
+
+        WifiManager manager = (WifiManager) HDLSdk.getInstance().getContext().getApplicationContext()
+                .getSystemService(Context.WIFI_SERVICE);
+        this.lock = manager.createMulticastLock("UDPwifi");
     }
 
     /**
      * 鍒濆鍖栧弬鏁�
+     *
      * @param ipAddress 鏈湴鐩戝惉IP鍦板潃
-     * @param port 鏈湴鐩戝惉绔彛
+     * @param port      鏈湴鐩戝惉绔彛
      * @param options
      * @return
      */
@@ -82,7 +88,8 @@
 
     /**
      * 鍒濆鍖栧弬鏁�
-     * @param port 鏈湴鐩戝惉绔彛
+     *
+     * @param port    鏈湴鐩戝惉绔彛
      * @param options
      * @return
      */
@@ -92,16 +99,17 @@
 
 
     @Override
-    public void bind() throws Exception {
+    public synchronized void bind() throws Exception {
         try {
             //宸茬粡缁戝畾杩囧氨涓嶇敤鍐嶇粦瀹�
             if (null != mSocket) {
                 return;
             }
+            lock.acquire();
             if (TextUtils.isEmpty(ipAddress)) {
                 mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port));
-            }else {
-                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(ipAddress,port));
+            } else {
+                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(ipAddress, port));
             }
             mSocket.setBroadcast(true);
 //            mSocket.setReuseAddress(true);
@@ -156,7 +164,7 @@
             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(data, receivePacket.getAddress().getHostAddress());
             }
 
         } catch (Exception e) {
@@ -166,11 +174,13 @@
 
 
     @Override
-    public void sendMsg(String ipAddress,int port, byte[] msg) throws Exception {
+    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);
+        if (mSocket != null && sendPacket != null) {
+            mSocket.send(sendPacket);
+        }
     }
 }

--
Gitblit v1.8.0