562935844@qq.com
2023-08-31 fdcf461fbfa3bcd650685743e891ad3357898f0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.hdl.sdk.socket.udp;
 
import android.text.TextUtils;
 
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.socket.SocketRequest;
import com.hdl.sdk.socket.client.IUdpClient;
import com.hdl.sdk.socket.listener.SendListener;
 
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * Created by hxb on 2021/12/12.
 */
public class UdpSocketBoot {
 
    private final IUdpClient client;
 
    private final AtomicBoolean isOpenRetry = new AtomicBoolean(false);
 
    private final AtomicInteger resendCount = new AtomicInteger(0);
 
    private ExecutorService receiveThread;
 
    private final ConcurrentMap<String, SendListener> sendMap = new ConcurrentHashMap<>();
 
    public UdpSocketBoot(IUdpClient client) {
        this.client = client;
    }
 
    /**
     * 绑定 socket
     * @throws Exception 可能端口冲突
     */
    public void bind() throws Exception {
        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 ipAddress 目的的IP地址
     * @param port 端口
     * @param msg 发送数据
     * @param listener 发送回调
     */
    public void sendMsg(String ipAddress,int port,byte[] msg, SendListener listener) {
        sendMsg(ipAddress,port, msg, true, listener);
    }
 
    /**
     * 发送数据
     * @param ipAddress 目的的IP地址
     * @param port 端口
     * @param msg 发送数据
     */
    public void sendMsg(String ipAddress,int port,byte[] msg) {
        sendMsg(ipAddress,port, msg, true, null);
    }
 
    /**
     * 发送数据
     * @param ipAddress 目的IP地址
     * @param port 端口
     * @param msg 发送的数据
     * @param isRefreshRetry 是否要重发
     * @param listener 发送回调
     */
    public void sendMsg(String ipAddress,int port, byte[] msg, boolean isRefreshRetry, SendListener listener) {
        if (isRefreshRetry) {
            //重置连接次数
            resendCount.set(0);
        }
        try {
            SocketRequest request = new SocketRequest(msg);
            if (listener != null && !TextUtils.isEmpty(request.getAction())) {
                sendMap.put(request.getAction(), listener);
            }
            LogUtils.i("发送目标IP和端口", ipAddress + ":" + port);
            client.sendMsg(ipAddress, port, msg);
        } catch (Exception e) {
            LogUtils.i("发送失败:" + e.getMessage());
        }
    }
 
    /**
     * 关闭当前socket
     */
    public synchronized void close() {
        isOpenRetry.set(false);
        sendMap.clear();
        receiveThread.shutdown();
        receiveThread=null;
        client.close();
    }
}