From d8bf4f4d66715f002d024cae92862c1d83daa425 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期日, 12 十二月 2021 22:06:35 +0800 Subject: [PATCH] 更改了udp的机制 --- HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java | 236 ++++++++++++++++++++++++---------------------------------- 1 files changed, 99 insertions(+), 137 deletions(-) diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java index 1f3dab9..c2a75a4 100644 --- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java +++ b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java @@ -1,135 +1,138 @@ package com.hdl.sdk.socket.client; -import android.util.Log; +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.common.utils.ThreadToolUtils; import com.hdl.sdk.socket.SocketBoot; import com.hdl.sdk.socket.SocketOptions; import com.hdl.sdk.socket.SocketPool; -import com.hdl.sdk.socket.annotation.ConnectStatus; +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.socket.listener.ConnectStatusListener; 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.List; import java.util.concurrent.atomic.AtomicBoolean; /** - * Created by Tong on 2021/9/15. - * 缁勬挱闇�瑕乤ndroid.permission.CHANGE_WIFI_MULTICAST_STATE鏉冮檺 - * MulticastSocket + * Created by hxb on 2021/12/12. */ -public class UdpClient implements IClient { - - private static DatagramSocket mSocket; - - private DatagramPacket receivePacket; - - private final int BUFFER = 4 * 1024; - - private final byte[] receiveByte; - - private final String ip; - - private final int port; - - private int monitorPort; - private int sendPort; - - private SocketOptions socketOptions; - - private final AtomicBoolean isConnect = new AtomicBoolean(false); +public class UdpClient implements IUdpClient { /** - * @param sendPort -1 琛ㄧず闅忔満绔彛 + * 褰撳墠socket */ - private UdpClient(String ip, int port, int monitorPort, int sendPort, SocketOptions socketOptions) { + 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; - this.ip = ip; - this.port = port; - this.sendPort = sendPort; - this.monitorPort = monitorPort; - this.receiveByte = new byte[BUFFER]; + byte[] receiveByte = new byte[BUFFER]; + receivePacket = new DatagramPacket(receiveByte, receiveByte.length); } - public UdpClient(String ip, int port) { - this.ip = ip; - this.port = port; - this.receiveByte = new byte[BUFFER]; + /** + * 鍒濆鍖栧弬鏁� + * @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)); } - public static SocketBoot init(String ip, int port, int monitorPort, int sendPort, SocketOptions options) { - return new SocketBoot(new UdpClient(ip, port, monitorPort, sendPort, options)); + /** + * 鍒濆鍖栧弬鏁� + * @param port 鏈湴鐩戝惉绔彛 + * @param options + * @return + */ + public static UdpSocketBoot init(int port, UdpSocketOptions options) { + return init(null, port, options); } - public static SocketBoot init(String ip, int port, int monitorPort, SocketOptions options) { - return init(ip, port, monitorPort, -1, options); - } - - public static SocketBoot init(String ip, int port, SocketOptions options) { - return init(ip, port, port, -1, options); - } @Override - public void connect() throws Exception { + public void bind() throws Exception { try { - mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(monitorPort)); - + //宸茬粡缁戝畾杩囧氨涓嶇敤鍐嶇粦瀹� + if (null != mSocket) { + return; + } + if (TextUtils.isEmpty(ipAddress)) { + mSocket = SocketPool.getInstance().getUdpSocket1(new InetSocketAddress(port)); + } mSocket.setBroadcast(true); mSocket.setReuseAddress(true); - isConnect.set(true); - if (receivePacket == null) { - receivePacket = new DatagramPacket(receiveByte, BUFFER); - } + } catch (Exception e) { - isConnect.set(false); + LogUtils.e("鍒濆鍖朣ocket 澶辫触锛�" + e.getMessage()); throw e; } - - } @Override - public void disconnect() { - if (mSocket != null) { + public boolean close() { + try { mSocket.close(); + } catch (Exception e) { + } - isConnect.set(false); + mSocket = null; + return true; } @Override - public boolean isConnect() { - return isConnect.get(); + public UdpSocketOptions getOptions() { + return null; } - @Override - public synchronized SocketOptions getOptions() { - if (socketOptions == null) { - socketOptions = new SocketOptions(); - } - return socketOptions; - } - - @Override public void onHandleResponse() throws Exception { - if (receivePacket == null || mSocket == null) { + if (mSocket == null) { return; } try { mSocket.receive(receivePacket); } catch (IOException e) { e.printStackTrace(); - isConnect.set(false); } if (receivePacket.getLength() == 0) { return; @@ -142,68 +145,27 @@ } - 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); - } - final String receive = new String(receivePacket.getData(), 0, receivePacket.getLength()); + try { + LogUtils.i( "鎺ユ敹鍒癠dp鏁版嵁鍖咃紝缃戠粶鍦板潃锛�" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort()); - LogUtils.d("---->", receive + " from " + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort()); - - //閲嶇疆闀垮害 - if (receivePacket != null) { - receivePacket.setLength(BUFFER); - } - } - - @Override - public void sendMsg(byte[] msg) throws Exception { - if (msg == null) { - msg = new byte[1]; - } - InetAddress serverAddress = InetAddress.getByName(ip); - final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, serverAddress, port); - if (sendPort < 0) { - final DatagramSocket sendSocket = new DatagramSocket(); - sendSocket.send(sendPacket); - sendSocket.close(); - } else if (sendPort == monitorPort) { - mSocket.send(sendPacket); - } else { - final DatagramSocket sendSocket = new DatagramSocket(sendPort); - sendSocket.send(sendPacket); - sendSocket.close(); - } - - } - - @Override - public void onConnectStatus(int status) { - ThreadToolUtils.getInstance().runOnUiThread(new Runnable() { - @Override - public void run() { - final List<ConnectStatusListener> list = getOptions().getConnectStatusListener(); - if (list != null && !list.isEmpty()) { - for (ConnectStatusListener listener : list) { - switch (status) { - case ConnectStatus - .CONNECTING: - listener.onConnecting(); - break; - case ConnectStatus - .CONNECTED: - listener.onConnected(); - break; - case ConnectStatus - .DISCONNECT: - listener.onConnectFailed(); - break; - } - } - } + 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); } - }); + + }catch (Exception e){ + + } + } + + @Override + public void sendMsg(InetSocketAddress inetSocketAddress, byte[] msg) throws Exception { + if (msg == null) { + return; + } + final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, inetSocketAddress); + mSocket.send(sendPacket); } } -- Gitblit v1.8.0