From f38894e1aaa2989c04c7507c483963278fb05ccf Mon Sep 17 00:00:00 2001 From: Andy Prock Date: Tue, 3 Jan 2017 10:22:39 -0800 Subject: [PATCH] ensure events correspond to generated socket ids --- examples/rctsockets/index.ios.js | 4 ++++ ios/TcpSocketClient.m | 35 +++++++++++++++++++++----------- ios/TcpSockets.m | 11 ++++++---- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/examples/rctsockets/index.ios.js b/examples/rctsockets/index.ios.js index 8e3fccb..f81cc04 100644 --- a/examples/rctsockets/index.ios.js +++ b/examples/rctsockets/index.ios.js @@ -48,6 +48,10 @@ class RctSockets extends Component { socket.on('error', (error) => { this.updateChatter('error ' + error); }); + + socket.on('close', (error) => { + this.updateChatter('server client closed ' + (error ? error : '')); + }); }).listen(serverPort, () => { this.updateChatter('opened server on ' + JSON.stringify(server.address())); }); diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index f60bb1c..4151960 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -7,6 +7,7 @@ #import #import "TcpSocketClient.h" #import "RCTBridgeModule.h" +#import "RCTLog.h" #import "GCDAsyncSocket.h" NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; @@ -18,7 +19,6 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; NSMutableDictionary *_pendingSends; NSLock *_lock; long _sendTag; - BOOL _isListening; } - (id)initWithClientId:(NSNumber *)clientID andConfig:(id)aDelegate; @@ -46,8 +46,8 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; _clientDelegate = aDelegate; _pendingSends = [NSMutableDictionary dictionary]; _lock = [[NSLock alloc] init]; - _isListening = NO; _tcpSocket = tcpSocket; + [_tcpSocket setUserData: clientID]; } return self; @@ -64,6 +64,8 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; } _tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]]; + [_tcpSocket setUserData: _id]; + BOOL result = false; NSString *localAddress = (options?options[@"localAddress"]:nil); @@ -118,18 +120,19 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; } _tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]]; + [_tcpSocket setUserData: _id]; // GCDAsyncSocket doesn't recognize 0.0.0.0 if ([@"0.0.0.0" isEqualToString: host]) { host = @"localhost"; } - _isListening = [_tcpSocket acceptOnInterface:host port:port error:error]; - if (_isListening == YES) { + BOOL isListening = [_tcpSocket acceptOnInterface:host port:port error:error]; + if (isListening == YES) { [_clientDelegate onConnect: self]; [_tcpSocket readDataWithTimeout:-1 tag:_id.longValue]; } - return _isListening; + return isListening; } - (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key @@ -190,18 +193,20 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; - (void)end { - _isListening = NO; [_tcpSocket disconnectAfterWriting]; } - (void)destroy { - _isListening = NO; [_tcpSocket disconnect]; } - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { - if (!_clientDelegate) return; + if (!_clientDelegate) { + RCTLogError(@"didReadData with nil clientDelegate for %@", [sock userData]) + return; + } + [_clientDelegate onData:@(tag) data:data]; [sock readDataWithTimeout:-1 tag:tag]; @@ -219,7 +224,11 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { - if (!_clientDelegate) return; + if (!_clientDelegate) { + RCTLogError(@"didConnectToHost with nil clientDelegate for %@", [sock userData]) + return; + } + [_clientDelegate onConnect:self]; [sock readDataWithTimeout:-1 tag:_id.longValue]; @@ -234,10 +243,12 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { - if (!_clientDelegate) return; - if (_isListening == NO) { - [_clientDelegate onClose:self withError:(!err || err.code == GCDAsyncSocketClosedError ? nil : err)]; + if (!_clientDelegate) { + RCTLogError(@"socketDidDisconnect with nil clientDelegate for %@", [sock userData]) + return; } + + [_clientDelegate onClose:[sock userData] withError:(!err || err.code == GCDAsyncSocketClosedError ? nil : err)]; } - (NSError *)badInvocationError:(NSString *)errMsg diff --git a/ios/TcpSockets.m b/ios/TcpSockets.m index b658bd5..c178933 100644 --- a/ios/TcpSockets.m +++ b/ios/TcpSockets.m @@ -127,16 +127,20 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId body:base64String]; } -- (void)onClose:(TcpSocketClient*) client withError:(NSError *)err +- (void)onClose:(NSNumber*) clientID withError:(NSError *)err { + TcpSocketClient* client = [self findClient:clientID]; + if (!client) { + RCTLogError(@"onClose: unrecognized client id %@", clientID) + } + if (err) { - [self onError:client withError:err]; + [self onError:client withError:err]; } [self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"tcp-%@-close", client.id] body:err == nil ? @NO : @YES]; - client.clientDelegate = nil; [_clients removeObjectForKey:client.id]; } @@ -174,7 +178,6 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId if (!client) return; [client destroy]; - [_clients removeObjectForKey:cId]; } -(NSNumber*)getNextId {