forked from Rapsssito/react-native-tcp-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpSocketClient.java
209 lines (187 loc) · 7.34 KB
/
TcpSocketClient.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.asterinet.react.tcpsocket;
import android.content.Context;
import android.net.Network;
import com.facebook.react.bridge.ReadableMap;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
class TcpSocketClient extends TcpSocket {
private final ExecutorService listenExecutor;
private final ExecutorService writeExecutor;
private final TcpEventListener receiverListener;
private TcpReceiverTask receiverTask;
private Socket socket;
TcpSocketClient(TcpEventListener receiverListener, Integer id, Socket socket) {
super(id);
listenExecutor = Executors.newSingleThreadExecutor();
writeExecutor = Executors.newSingleThreadExecutor();
this.socket = socket;
this.receiverListener = receiverListener;
}
public Socket getSocket() {
return socket;
}
public void connect(Context context, String address, final Integer port, ReadableMap options, Network network) throws IOException, GeneralSecurityException {
if (socket != null) throw new IOException("Already connected");
final boolean isTls = options.hasKey("tls") && options.getBoolean("tls");
if (isTls) {
SocketFactory sf;
if (options.hasKey("tlsCheckValidity") && !options.getBoolean("tlsCheckValidity")) {
sf = SSLCertificateHelper.createBlindSocketFactory();
} else {
final String customTlsCert = options.hasKey("tlsCert") ? options.getString("tlsCert") : null;
sf = customTlsCert != null ? SSLCertificateHelper.createCustomTrustedSocketFactory(context, customTlsCert) : SSLSocketFactory.getDefault();
}
final SSLSocket sslSocket = (SSLSocket) sf.createSocket();
sslSocket.setUseClientMode(true);
socket = sslSocket;
} else {
socket = new Socket();
}
// Get the addresses
final String localAddress = options.hasKey("localAddress") ? options.getString("localAddress") : "0.0.0.0";
final InetAddress localInetAddress = InetAddress.getByName(localAddress);
final InetAddress remoteInetAddress = InetAddress.getByName(address);
if (network != null)
network.bindSocket(socket);
// setReuseAddress
if (options.hasKey("reuseAddress")) {
boolean reuseAddress = options.getBoolean("reuseAddress");
socket.setReuseAddress(reuseAddress);
} else {
// Default to true
socket.setReuseAddress(true);
}
final int localPort = options.hasKey("localPort") ? options.getInt("localPort") : 0;
// bind
socket.bind(new InetSocketAddress(localInetAddress, localPort));
socket.connect(new InetSocketAddress(remoteInetAddress, port));
if (isTls) ((SSLSocket) socket).startHandshake();
startListening();
}
public void startListening() {
receiverTask = new TcpReceiverTask(this, receiverListener);
listenExecutor.execute(receiverTask);
}
/**
* Sends data from the socket
*
* @param data data to be sent
*/
public void write(final int msgId, final byte[] data) {
writeExecutor.execute(new Runnable() {
@Override
public void run() {
try {
if (socket != null) {
socket.getOutputStream().write(data);
receiverListener.onWritten(getId(), msgId, null);
});
} catch (IOException e) {
receiverListener.onWritten(getId(), msgId, e.toString());
receiverListener.onError(getId(), e.toString());
}
}
}
}
/**
* Shuts down the receiver task, closing the socket.
*/
public void destroy() {
try {
// close the socket
if (socket != null && !socket.isClosed()) {
socket.close();
receiverListener.onClose(getId(), null);
socket = null;
}
} catch (IOException e) {
receiverListener.onClose(getId(), e.getMessage());
}
}
/**
* @param noDelay `true` will disable Nagle's algorithm for the socket (enable TCP_NODELAY)
*/
public void setNoDelay(final boolean noDelay) throws IOException {
if (socket == null) {
throw new IOException("Socket is not connected.");
}
socket.setTcpNoDelay(noDelay);
}
/**
* @param enable `true` to enable keep-alive functionality
*/
public void setKeepAlive(final boolean enable, final int initialDelay) throws IOException {
if (socket == null) {
throw new IOException("Socket is not connected.");
}
// `initialDelay` is ignored
socket.setKeepAlive(enable);
}
public void pause() {
receiverTask.pause();
}
public void resume() {
receiverTask.resume();
}
/**
* This is a specialized Runnable that receives data from a socket in the background, and
* notifies it's listener when data is received. This is not threadsafe, the listener
* should handle synchronicity.
*/
private static class TcpReceiverTask implements Runnable {
private final TcpSocketClient clientSocket;
private final TcpEventListener receiverListener;
private boolean paused = false;
public TcpReceiverTask(TcpSocketClient clientSocket, TcpEventListener receiverListener) {
this.clientSocket = clientSocket;
this.receiverListener = receiverListener;
}
/**
* An infinite loop to block and read data from the socket.
*/
@Override
public void run() {
int socketId = clientSocket.getId();
Socket socket = clientSocket.getSocket();
byte[] buffer = new byte[16384];
try {
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
while (!socket.isClosed()) {
int bufferCount = in.read(buffer);
waitIfPaused();
if (bufferCount > 0) {
receiverListener.onData(socketId, Arrays.copyOfRange(buffer, 0, bufferCount));
} else if (bufferCount == -1) {
clientSocket.destroy();
}
}
} catch (IOException | InterruptedException ioe) {
if (receiverListener != null && !socket.isClosed()) {
receiverListener.onError(socketId, ioe.getMessage());
}
}
}
public synchronized void pause() {
paused = true;
}
public synchronized void resume() {
paused = false;
notify();
}
private synchronized void waitIfPaused() throws InterruptedException {
while (paused) {
wait();
}
}
}
}