-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathTcpSockets.m
357 lines (295 loc) · 10.6 KB
/
TcpSockets.m
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#import <React/RCTAssert.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import "TcpSocketClient.h"
#import "TcpSockets.h"
// offset native ids by 5000
#define COUNTER_OFFSET 5000
@implementation TcpSockets {
NSMutableDictionary<NSNumber *, TcpSocketClient *> *_clients;
NSMutableDictionary<NSNumber *, NSDictionary *> *_pendingTLS;
int _counter;
}
RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)supportedEvents {
return @[
@"connect", @"listening", @"connection", @"secureConnection", @"data",
@"close", @"error", @"written"
];
}
- (void)startObserving {
// Does nothing
}
- (void)stopObserving {
// Does nothing
}
- (void)dealloc {
for (NSNumber *cId in _clients.allKeys) {
[self destroyClient:cId];
}
}
- (TcpSocketClient *)createSocket:(nonnull NSNumber *)cId {
if (!cId) {
RCTLogWarn(@"%@.createSocket called with nil id parameter.",
[self class]);
return nil;
}
if (!_clients) {
_clients = [NSMutableDictionary new];
}
if (_clients[cId]) {
RCTLogWarn(@"%@.createSocket called twice with the same id.",
[self class]);
return nil;
}
_clients[cId] = [TcpSocketClient socketClientWithId:cId andConfig:self];
return _clients[cId];
}
RCT_EXPORT_METHOD(connect
: (nonnull NSNumber *)cId host
: (NSString *)host port
: (int)port withOptions
: (NSDictionary *)options) {
TcpSocketClient *client = _clients[cId];
if (!client) {
client = [self createSocket:cId];
}
NSDictionary *tlsOptions = _pendingTLS[cId];
NSError *error = nil;
if (![client connect:host
port:port
withOptions:options
tlsOptions:tlsOptions
error:&error]) {
[self onError:client withError:error];
return;
}
}
RCT_EXPORT_METHOD(write
: (nonnull NSNumber *)cId string
: (nonnull NSString *)base64String callback
: (nonnull NSNumber *)msgId) {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
// iOS7+
// TODO: use https://github.com/nicklockwood/Base64 for compatibility with
// earlier iOS versions
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String
options:0];
[client writeData:data msgId:msgId];
}
RCT_EXPORT_METHOD(end : (nonnull NSNumber *)cId) { [self endClient:cId]; }
RCT_EXPORT_METHOD(destroy : (nonnull NSNumber *)cId) {
[self destroyClient:cId];
}
RCT_EXPORT_METHOD(close : (nonnull NSNumber *)cId) { [self destroyClient:cId]; }
RCT_EXPORT_METHOD(listen
: (nonnull NSNumber *)cId withOptions
: (nonnull NSDictionary *)options) {
TcpSocketClient *client = _clients[cId];
if (!client) {
client = [self createSocket:cId];
}
NSError *error = nil;
if (![client listen:options error:&error]) {
[self onError:client withError:error];
return;
}
}
RCT_EXPORT_METHOD(startTLS
: (nonnull NSNumber *)cId tlsOptions
: (nonnull NSDictionary *)tlsOptions) {
TcpSocketClient *client = _clients[cId];
if (!client) {
if (!_pendingTLS) {
_pendingTLS = [NSMutableDictionary new];
}
_pendingTLS[cId] = tlsOptions;
} else {
[client startTLS:tlsOptions];
}
}
RCT_EXPORT_METHOD(setNoDelay
: (nonnull NSNumber *)cId noDelay
: (BOOL)noDelay) {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
[client setNoDelay:noDelay];
}
RCT_EXPORT_METHOD(setKeepAlive
: (nonnull NSNumber *)cId enable
: (BOOL)enable initialDelay
: (int)initialDelay) {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
[client setKeepAlive:enable initialDelay:initialDelay];
}
RCT_EXPORT_METHOD(pause : (nonnull NSNumber *)cId) {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
[client pause];
}
RCT_EXPORT_METHOD(resume : (nonnull NSNumber *)cId) {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
[client resume];
}
RCT_EXPORT_METHOD(hasIdentity:(nonnull NSDictionary *)aliases
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
// Since this is a static check of the keychain, we don't need a client instance
BOOL hasIdentity = [TcpSocketClient hasIdentity:aliases];
resolve(@(hasIdentity));
}
RCT_EXPORT_METHOD(getPeerCertificate:(nonnull NSNumber *)cId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
TcpSocketClient *client = [self findClient:cId];
if (!client) {
reject(@"NOT_FOUND", @"Client not found", nil);
return;
}
NSDictionary *cert = [client getPeerCertificate];
resolve(cert ?: [NSNull null]);
}
RCT_EXPORT_METHOD(getCertificate:(nonnull NSNumber *)cId
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
TcpSocketClient *client = [self findClient:cId];
if (!client) {
reject(@"NOT_FOUND", @"Client not found", nil);
return;
}
NSDictionary *cert = [client getCertificate];
resolve(cert ?: [NSNull null]);
}
- (void)onWrittenData:(TcpSocketClient *)client msgId:(NSNumber *)msgId {
[self sendEventWithName:@"written"
body:@{
@"id" : client.id,
@"msgId" : msgId,
}];
}
- (void)onConnect:(TcpSocketClient *)client {
GCDAsyncSocket *socket = [client getSocket];
[self sendEventWithName:@"connect"
body:@{
@"id" : client.id,
@"connection" : @{
@"localAddress" : [socket localHost],
@"localPort" :
[NSNumber numberWithInt:[socket localPort]],
@"remoteAddress" : [socket connectedHost],
@"remotePort" : [NSNumber
numberWithInt:[socket connectedPort]],
@"remoteFamily" : [socket isIPv4] ? @"IPv4"
: @"IPv6"
}
}];
}
- (void)onListen:(TcpSocketClient *)server {
GCDAsyncSocket *socket = [server getSocket];
[self sendEventWithName:@"listening"
body:@{
@"id" : server.id,
@"connection" : @{
@"localAddress" : [socket localHost],
@"localPort" :
[NSNumber numberWithInt:[socket localPort]],
@"localFamily" : [socket isIPv4] ? @"IPv4"
: @"IPv6"
}
}];
}
- (void)onConnection:(TcpSocketClient *)client toClient:(NSNumber *)clientID {
[self onSocketConnection:client
toClient:clientID
connectionType:@"connection"];
}
- (void)onSecureConnection:(TcpSocketClient *)client
toClient:(NSNumber *)clientID {
[self onSocketConnection:client
toClient:clientID
connectionType:@"secureConnection"];
}
- (void)addClient:(TcpSocketClient *)client {
_clients[client.id] = client;
}
- (void)onSocketConnection:(TcpSocketClient *)client
toClient:(NSNumber *)clientID
connectionType:(NSString *)connectionType {
GCDAsyncSocket *socket = [client getSocket];
[self sendEventWithName:connectionType
body:@{
@"id" : clientID,
@"info" : @{
@"id" : client.id,
@"connection" : @{
@"localAddress" : [socket localHost],
@"localPort" : [NSNumber
numberWithInt:[socket localPort]],
@"remoteAddress" : [socket connectedHost],
@"remotePort" : [NSNumber
numberWithInt:[socket connectedPort]],
@"remoteFamily" : [socket isIPv4] ? @"IPv4"
: @"IPv6"
}
}
}];
}
- (void)onData:(NSNumber *)clientID data:(NSData *)data {
NSString *base64String = [data base64EncodedStringWithOptions:0];
[self sendEventWithName:@"data"
body:@{@"id" : clientID, @"data" : base64String}];
}
- (void)onClose:(NSNumber *)clientID withError:(NSError *)err {
TcpSocketClient *client = [self findClient:clientID];
if (!client) {
RCTLogWarn(@"onClose: unrecognized client id %@", clientID);
}
if (err) {
[self onError:client withError:err];
}
[self sendEventWithName:@"close"
body:@{
@"id" : clientID,
@"hadError" : err == nil ? @NO : @YES
}];
[_clients removeObjectForKey:clientID];
}
- (void)onError:(TcpSocketClient *)client withError:(NSError *)err {
NSString *msg = err.localizedDescription ?: err.localizedFailureReason;
[self sendEventWithName:@"error" body:@{@"id" : client.id, @"error" : msg}];
}
- (TcpSocketClient *)findClient:(nonnull NSNumber *)cId {
TcpSocketClient *client = _clients[cId];
if (!client) {
NSString *msg =
[NSString stringWithFormat:@"no client found with id %@", cId];
[self sendEventWithName:@"error" body:@{@"id" : cId, @"error" : msg}];
return nil;
}
return client;
}
- (void)endClient:(nonnull NSNumber *)cId {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
[client end];
}
- (void)destroyClient:(nonnull NSNumber *)cId {
TcpSocketClient *client = [self findClient:cId];
if (!client)
return;
[client destroy];
}
- (NSNumber *)getNextId {
return @(_counter++ + COUNTER_OFFSET);
}
@end