From a742459272e32c0e6b49608314a5b7cf0bea704e Mon Sep 17 00:00:00 2001 From: Andy Prock Date: Thu, 17 Dec 2015 09:07:43 -0800 Subject: [PATCH] 0.0.2 add locks to _pendingSends dictionary --- ios/TcpSocketClient.m | 41 ++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index bed82f0..e2c4704 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -20,6 +20,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; GCDAsyncSocket *_tcpSocket; id _clientDelegate; NSMutableDictionary *_pendingSends; + NSLock *_lock; long _sendTag; } @@ -41,6 +42,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; _id = clientID; _clientDelegate = aDelegate; _pendingSends = [NSMutableDictionary dictionary]; + _lock = [[NSLock alloc] init]; } return self; @@ -80,13 +82,46 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; return result; } +- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key +{ + [_lock lock]; + @try { + [_pendingSends setObject:callback forKey:key]; + } + @finally { + [_lock unlock]; + } +} + +- (RCTResponseSenderBlock)getPendingSend:(NSNumber *)key +{ + [_lock lock]; + @try { + return [_pendingSends objectForKey:key]; + } + @finally { + [_lock unlock]; + } +} + +- (void)dropPendingSend:(NSNumber *)key +{ + [_lock lock]; + @try { + [_pendingSends removeObjectForKey:key]; + } + @finally { + [_lock unlock]; + } +} + - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)msgTag { NSNumber* tagNum = [NSNumber numberWithLong:msgTag]; - RCTResponseSenderBlock callback = [_pendingSends objectForKey:tagNum]; + RCTResponseSenderBlock callback = [self getPendingSend:tagNum]; if (callback) { callback(@[]); - [_pendingSends removeObjectForKey:tagNum]; + [self dropPendingSend:tagNum]; } } @@ -95,7 +130,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain"; { [_tcpSocket writeData:data withTimeout:-1 tag:_sendTag]; if (callback) { - [_pendingSends setObject:callback forKey:[NSNumber numberWithLong:_sendTag]]; + [self setPendingSend:callback forKey:[NSNumber numberWithLong:_sendTag]]; } _sendTag++; diff --git a/package.json b/package.json index d819d45..6bc07f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-tcp", - "version": "0.0.1", + "version": "0.0.2", "description": "node's dgram API for react-native", "main": "TcpSockets.js", "scripts": {