hxb
2021-12-13 27a0768b0e0a042911b7f299fcc599d2da4e7fc0
HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/udp/UdpSocketBoot.java
@@ -13,7 +13,9 @@
import com.hdl.sdk.socket.listener.SendListener;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
@@ -33,6 +35,8 @@
    private final AtomicInteger resendCount = new AtomicInteger(0);
    private ExecutorService receiveThread;
    private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>();
    public UdpSocketBoot(IUdpClient client) {
@@ -44,38 +48,63 @@
     * @throws Exception 可能端口冲突
     */
    public void bind() throws Exception {
        if (null != client) {
            client.bind();
        }
        client.bind();
        initReceiveThread();
    }
    /**
     * 初始化接收线程
     */
    private void initReceiveThread() {
        if(null!=receiveThread){
            return;
        }
        receiveThread = ThreadToolUtils.getInstance().newFixedThreadPool(1);
        receiveThread.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        client.onHandleResponse();
                    } catch (Exception e) {
                       LogUtils.i("接收线程异常:"+e.getMessage());
                    }
                }
            }
        });
    }
    /**
     * 发送数据
     * @param inetSocketAddress 目的的IP地址
     * @param ipAddress 目的的IP地址
     * @param port 端口
     * @param msg 发送数据
     * @param listener 发送回调
     */
    public void sendMsg(InetSocketAddress inetSocketAddress,byte[] msg, SendListener listener) {
        sendMsg(inetSocketAddress, msg, true, listener);
    public void sendMsg(String ipAddress,int port,byte[] msg, SendListener listener) {
        sendMsg(ipAddress,port, msg, true, listener);
    }
    /**
     * 发送数据
     * @param inetSocketAddress 目的的IP地址
     * @param ipAddress 目的的IP地址
     * @param port 端口
     * @param msg 发送数据
     */
    public void sendMsg(InetSocketAddress inetSocketAddress,byte[] msg) {
        sendMsg(inetSocketAddress, msg, true, null);
    public void sendMsg(String ipAddress,int port,byte[] msg) {
        sendMsg(ipAddress,port, msg, true, null);
    }
    /**
     * 发送数据
     * @param inetSocketAddress 目的IP地址
     * @param ipAddress 目的IP地址
     * @param port 端口
     * @param msg 发送的数据
     * @param isRefreshRetry 是否要重发
     * @param listener 发送回调
     */
    public void sendMsg(InetSocketAddress inetSocketAddress, byte[] msg, boolean isRefreshRetry, SendListener listener) {
    public void sendMsg(String ipAddress,int port, byte[] msg, boolean isRefreshRetry, SendListener listener) {
        if (isRefreshRetry) {
            //重置连接次数
            resendCount.set(0);
@@ -85,7 +114,7 @@
            if (listener != null && !TextUtils.isEmpty(request.getAction())) {
                sendMap.put(request.getAction(), listener);
            }
            client.sendMsg(inetSocketAddress, msg);
            client.sendMsg(ipAddress,port, msg);
        } catch (Exception e) {
            LogUtils.i("发送失败:" + e.getMessage());
        }
@@ -96,8 +125,9 @@
     */
    public synchronized void close() {
        isOpenRetry.set(false);
        sendMap.clear();
        receiveThread.shutdown();
        receiveThread=null;
        client.close();
    }
}