hxb
2022-03-21 0188dee359636723190f0f67a6b674b7b08f7bef
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.hdl.sdk.socket.client;
 
 
import android.text.TextUtils;
 
import com.hdl.sdk.common.utils.IpUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.socket.SocketBoot;
import com.hdl.sdk.socket.SocketOptions;
import com.hdl.sdk.socket.SocketPool;
import com.hdl.sdk.socket.udp.UdpSocketBoot;
import com.hdl.sdk.socket.udp.UdpSocketOptions;
import com.hdl.sdk.socket.codec.IHandleMessage;
 
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.concurrent.atomic.AtomicBoolean;
 
 
/**
 * 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;
 
    /**
     * 初始化参数
     * @param ipAddress 本地监听端口
     * @param port 本地监听端口
     * @param socketOptions
     */
    private 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);
    }
 
    /**
     * 初始化参数
     * @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(null, port, options);
    }
 
 
    @Override
    public void bind() throws Exception {
        try {
            //已经绑定过就不用再绑定
            if (null != mSocket) {
                return;
            }
            if (TextUtils.isEmpty(ipAddress)) {
                mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port));
            }
            mSocket.setBroadcast(true);
            mSocket.setReuseAddress(true);
 
        } catch (Exception e) {
            LogUtils.e("初始化Socket 失败:" + e.getMessage());
            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 (IOException e) {
            e.printStackTrace();
        }
        if (receivePacket.getLength() == 0) {
            return;
        }
        //排除自己发出去的
        try {
            if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
                return;
        } catch (Exception ignored) {
 
        }
 
        try {
            LogUtils.i("接收到Udp数据包,网络地址:" + 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(data,receivePacket.getAddress().getHostAddress());
            }
 
        } catch (Exception e) {
 
        }
    }
 
 
    @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);
        mSocket.send(sendPacket);
    }
}