hxb
2021-12-12 d8bf4f4d66715f002d024cae92862c1d83daa425
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
package com.hdl.sdk.socket.udp;
 
import android.text.TextUtils;
 
import androidx.collection.ArrayMap;
 
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.annotation.ConnectStatus;
import com.hdl.sdk.socket.client.IClient;
import com.hdl.sdk.socket.client.IUdpClient;
import com.hdl.sdk.socket.listener.SendListener;
 
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
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 final ArrayMap<String, SendListener> sendMap = new ArrayMap<>();
 
    public UdpSocketBoot(IUdpClient client) {
        this.client = client;
    }
 
    /**
     * 绑定 socket
     * @throws Exception 可能端口冲突
     */
    public void bind() throws Exception {
        if (null != client) {
            client.bind();
        }
    }
 
    /**
     * 发送数据
     * @param inetSocketAddress 目的的IP地址
     * @param msg 发送数据
     * @param listener 发送回调
     */
    public void sendMsg(InetSocketAddress inetSocketAddress,byte[] msg, SendListener listener) {
        sendMsg(inetSocketAddress, msg, true, listener);
    }
 
    /**
     * 发送数据
     * @param inetSocketAddress 目的的IP地址
     * @param msg 发送数据
     */
    public void sendMsg(InetSocketAddress inetSocketAddress,byte[] msg) {
        sendMsg(inetSocketAddress, msg, true, null);
    }
 
    /**
     * 发送数据
     * @param inetSocketAddress 目的IP地址
     * @param msg 发送的数据
     * @param isRefreshRetry 是否要重发
     * @param listener 发送回调
     */
    public void sendMsg(InetSocketAddress inetSocketAddress, 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);
            }
            client.sendMsg(inetSocketAddress, msg);
        } catch (Exception e) {
            LogUtils.i("发送失败:" + e.getMessage());
        }
    }
 
    /**
     * 关闭当前socket
     */
    public synchronized void close() {
        isOpenRetry.set(false);
 
        sendMap.clear();
        client.close();
    }
}