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/client/UdpClient.java | 191 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 191 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java new file mode 100644 index 0000000..2e94009 --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/UdpClient.java @@ -0,0 +1,191 @@ +package com.hdl.sdk.link.socket.client; + + +import android.net.wifi.WifiManager; +import android.text.TextUtils; + +import com.hdl.sdk.link.common.utils.LogUtils; +import com.hdl.sdk.link.core.protocol.LinkMessageDecoder; +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; + + +/** + * 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; + + WifiManager.MulticastLock lock; + + /** + * 鍒濆鍖栧弬鏁� + * @param ipAddress 鏈湴鐩戝惉绔彛 + * @param port 鏈湴鐩戝惉绔彛 + * @param socketOptions + */ + public 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); + this.lock= socketOptions.getWifiManager().createMulticastLock("UDPwifi"); + } + + /** + * 鍒濆鍖栧弬鏁� + * @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 synchronized void bind() throws Exception { + try { + //宸茬粡缁戝畾杩囧氨涓嶇敤鍐嶇粦瀹� + if (null != mSocket) { + return; + } + + lock.acquire(); + if (TextUtils.isEmpty(ipAddress)) { + 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); + + } catch (Exception e) { + 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 (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 { + 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(new Packet(receivePacket.getAddress().getHostAddress(), data,null)); +// } + + byte[] data = new byte[receivePacket.getLength()]; + System.arraycopy(receivePacket.getData(), 0, data, 0, data.length); + socketOptions.getHandleMessage().read(new Packet(data)); + + } catch (Exception e) { + LogUtils.i("鎺ユ敹鍒癠dp鏁版嵁鍖咃紝澶勭悊寮傚父锛�" + e.getMessage()); + } + } + + + @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); + if (mSocket != null) { + mSocket.send(sendPacket); + } else { + DatagramSocket datagramSocket = new DatagramSocket(); + datagramSocket.send(sendPacket); + datagramSocket.close(); + } + } +} -- Gitblit v1.8.0