4
4
import android .annotation .SuppressLint ;
5
5
import android .content .Context ;
6
6
import android .net .ConnectivityManager ;
7
+ import android .net .Network ;
7
8
import android .net .NetworkCapabilities ;
8
9
import android .net .NetworkRequest ;
9
10
import android .util .Base64 ;
10
- import android .net .Network ;
11
+
12
+ import androidx .annotation .NonNull ;
13
+ import androidx .annotation .Nullable ;
11
14
12
15
import com .facebook .react .bridge .ReactApplicationContext ;
13
16
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
22
25
import java .util .concurrent .ScheduledThreadPoolExecutor ;
23
26
import java .util .concurrent .TimeUnit ;
24
27
25
- import androidx .annotation .NonNull ;
26
- import androidx .annotation .Nullable ;
27
-
28
28
public class TcpSocketModule extends ReactContextBaseJavaModule {
29
- private static final String TAG = "TcpSockets" ;
29
+ public static final String TAG = "TcpSockets" ;
30
30
private static final int N_THREADS = 2 ;
31
31
private final ReactApplicationContext mReactContext ;
32
32
private final ConcurrentHashMap <Integer , TcpSocket > socketMap = new ConcurrentHashMap <>();
33
+ private final ConcurrentHashMap <Integer , ReadableMap > pendingTLS = new ConcurrentHashMap <>();
33
34
private final ConcurrentHashMap <String , Network > mNetworkMap = new ConcurrentHashMap <>();
34
35
private final CurrentNetwork currentNetwork = new CurrentNetwork ();
35
36
private final ExecutorService executorService = Executors .newFixedThreadPool (N_THREADS );
@@ -68,7 +69,7 @@ public void connect(@NonNull final Integer cId, @NonNull final String host, @Non
68
69
@ Override
69
70
public void run () {
70
71
if (socketMap .get (cId ) != null ) {
71
- tcpEvtListener .onError (cId , TAG + "createSocket called twice with the same id." );
72
+ tcpEvtListener .onError (cId , new Exception ( "connect() called twice with the same id.") );
72
73
return ;
73
74
}
74
75
try {
@@ -78,15 +79,33 @@ public void run() {
78
79
selectNetwork (iface , localAddress );
79
80
TcpSocketClient client = new TcpSocketClient (tcpEvtListener , cId , null );
80
81
socketMap .put (cId , client );
81
- client .connect (mReactContext , host , port , options , currentNetwork .getNetwork ());
82
+ ReadableMap tlsOptions = pendingTLS .get (cId );
83
+ client .connect (mReactContext , host , port , options , currentNetwork .getNetwork (), tlsOptions );
82
84
tcpEvtListener .onConnect (cId , client );
83
85
} catch (Exception e ) {
84
- tcpEvtListener .onError (cId , e . getMessage () );
86
+ tcpEvtListener .onError (cId , e );
85
87
}
86
88
}
87
89
});
88
90
}
89
91
92
+ @ SuppressLint ("StaticFieldLeak" )
93
+ @ SuppressWarnings ("unused" )
94
+ @ ReactMethod
95
+ public void startTLS (final int cId , @ NonNull final ReadableMap tlsOptions ) {
96
+ TcpSocketClient socketClient = (TcpSocketClient ) socketMap .get (cId );
97
+ // Not yet connected
98
+ if (socketClient == null ) {
99
+ pendingTLS .put (cId , tlsOptions );
100
+ } else {
101
+ try {
102
+ socketClient .startTLS (mReactContext , tlsOptions );
103
+ } catch (Exception e ) {
104
+ tcpEvtListener .onError (cId , e );
105
+ }
106
+ }
107
+ }
108
+
90
109
@ SuppressLint ("StaticFieldLeak" )
91
110
@ SuppressWarnings ("unused" )
92
111
@ ReactMethod
@@ -137,11 +156,11 @@ public void listen(final Integer cId, final ReadableMap options) {
137
156
@ Override
138
157
public void run () {
139
158
try {
140
- TcpSocketServer server = new TcpSocketServer (socketMap , tcpEvtListener , cId , options );
159
+ TcpSocketServer server = new TcpSocketServer (mReactContext , socketMap , tcpEvtListener , cId , options );
141
160
socketMap .put (cId , server );
142
161
tcpEvtListener .onListen (cId , server );
143
162
} catch (Exception uhe ) {
144
- tcpEvtListener .onError (cId , uhe . getMessage () );
163
+ tcpEvtListener .onError (cId , uhe );
145
164
}
146
165
}
147
166
});
@@ -154,7 +173,7 @@ public void setNoDelay(@NonNull final Integer cId, final boolean noDelay) {
154
173
try {
155
174
client .setNoDelay (noDelay );
156
175
} catch (IOException e ) {
157
- tcpEvtListener .onError (cId , e . getMessage () );
176
+ tcpEvtListener .onError (cId , e );
158
177
}
159
178
}
160
179
@@ -165,7 +184,7 @@ public void setKeepAlive(@NonNull final Integer cId, final boolean enable, final
165
184
try {
166
185
client .setKeepAlive (enable , initialDelay );
167
186
} catch (IOException e ) {
168
- tcpEvtListener .onError (cId , e . getMessage () );
187
+ tcpEvtListener .onError (cId , e );
169
188
}
170
189
}
171
190
@@ -182,7 +201,7 @@ public void resume(final int cId) {
182
201
TcpSocketClient client = getTcpClient (cId );
183
202
client .resume ();
184
203
}
185
-
204
+
186
205
@ SuppressWarnings ("unused" )
187
206
@ ReactMethod
188
207
public void addListener (String eventName ) {
@@ -260,21 +279,21 @@ private void selectNetwork(@Nullable final String iface, @Nullable final String
260
279
private TcpSocketClient getTcpClient (final int id ) {
261
280
TcpSocket socket = socketMap .get (id );
262
281
if (socket == null ) {
263
- throw new IllegalArgumentException (TAG + "No socket with id " + id );
282
+ throw new IllegalArgumentException ("No socket with id " + id );
264
283
}
265
284
if (!(socket instanceof TcpSocketClient )) {
266
- throw new IllegalArgumentException (TAG + "Socket with id " + id + " is not a client" );
285
+ throw new IllegalArgumentException ("Socket with id " + id + " is not a client" );
267
286
}
268
287
return (TcpSocketClient ) socket ;
269
288
}
270
289
271
290
private TcpSocketServer getTcpServer (final int id ) {
272
291
TcpSocket socket = socketMap .get (id );
273
292
if (socket == null ) {
274
- throw new IllegalArgumentException (TAG + "No socket with id " + id );
293
+ throw new IllegalArgumentException ("No server socket with id " + id );
275
294
}
276
295
if (!(socket instanceof TcpSocketServer )) {
277
- throw new IllegalArgumentException (TAG + "Socket with id " + id + " is not a server" );
296
+ throw new IllegalArgumentException ("Server socket with id " + id + " is not a server" );
278
297
}
279
298
return (TcpSocketServer ) socket ;
280
299
}
0 commit comments