add locks to _pendingSends dictionary
This commit is contained in:
Andy Prock 2015-12-17 09:07:43 -08:00
parent 3eb82c2fde
commit a742459272
2 changed files with 39 additions and 4 deletions

View File

@ -20,6 +20,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
GCDAsyncSocket *_tcpSocket; GCDAsyncSocket *_tcpSocket;
id<SocketClientDelegate> _clientDelegate; id<SocketClientDelegate> _clientDelegate;
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends; NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
NSLock *_lock;
long _sendTag; long _sendTag;
} }
@ -41,6 +42,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
_id = clientID; _id = clientID;
_clientDelegate = aDelegate; _clientDelegate = aDelegate;
_pendingSends = [NSMutableDictionary dictionary]; _pendingSends = [NSMutableDictionary dictionary];
_lock = [[NSLock alloc] init];
} }
return self; return self;
@ -80,13 +82,46 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
return result; 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 - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)msgTag
{ {
NSNumber* tagNum = [NSNumber numberWithLong:msgTag]; NSNumber* tagNum = [NSNumber numberWithLong:msgTag];
RCTResponseSenderBlock callback = [_pendingSends objectForKey:tagNum]; RCTResponseSenderBlock callback = [self getPendingSend:tagNum];
if (callback) { if (callback) {
callback(@[]); callback(@[]);
[_pendingSends removeObjectForKey:tagNum]; [self dropPendingSend:tagNum];
} }
} }
@ -95,7 +130,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
{ {
[_tcpSocket writeData:data withTimeout:-1 tag:_sendTag]; [_tcpSocket writeData:data withTimeout:-1 tag:_sendTag];
if (callback) { if (callback) {
[_pendingSends setObject:callback forKey:[NSNumber numberWithLong:_sendTag]]; [self setPendingSend:callback forKey:[NSNumber numberWithLong:_sendTag]];
} }
_sendTag++; _sendTag++;

View File

@ -1,6 +1,6 @@
{ {
"name": "react-native-tcp", "name": "react-native-tcp",
"version": "0.0.1", "version": "0.0.2",
"description": "node's dgram API for react-native", "description": "node's dgram API for react-native",
"main": "TcpSockets.js", "main": "TcpSockets.js",
"scripts": { "scripts": {