diff --git a/ios/TcpSocketClient.h b/ios/TcpSocketClient.h index 6b1de9d..a4eff2a 100644 --- a/ios/TcpSocketClient.h +++ b/ios/TcpSocketClient.h @@ -31,6 +31,10 @@ typedef enum RCTTCPError RCTTCPError; - (void)onData:(NSNumber *)clientID data:(NSData *)data; - (void)onClose:(TcpSocketClient*)client withError:(NSError *)err; - (void)onError:(TcpSocketClient*)client withError:(NSError *)err; +- (NSNumber*)getNextTag; +- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key; +- (RCTResponseSenderBlock)getPendingSend:(NSNumber *)key; +- (void)dropPendingSend:(NSNumber *)key; - (NSNumber*)getNextId; @end diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index 009e5af..12852f1 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -15,9 +15,6 @@ @interface TcpSocketClient() { @private GCDAsyncSocket *_tcpSocket; - NSMutableDictionary *_pendingSends; - NSLock *_lock; - long _sendTag; } - (id)initWithClientId:(NSNumber *)clientID andConfig:(id)aDelegate; @@ -43,8 +40,6 @@ - (id)initWithClientId:(NSNumber *)clientID andConfig:(id) if (self) { _id = clientID; _clientDelegate = aDelegate; - _pendingSends = [NSMutableDictionary dictionary]; - _lock = [[NSLock alloc] init]; _tcpSocket = tcpSocket; [_tcpSocket setUserData: clientID]; } @@ -134,58 +129,24 @@ - (BOOL)listen:(NSString *)host port:(int)port error:(NSError **)error return isListening; } -- (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 = [self getPendingSend:tagNum]; + RCTResponseSenderBlock callback = [_clientDelegate getPendingSend:tagNum]; if (callback) { callback(@[]); - [self dropPendingSend:tagNum]; + [_clientDelegate dropPendingSend:tagNum]; } } - (void) writeData:(NSData *)data callback:(RCTResponseSenderBlock)callback { + NSNumber *sendTag = [_clientDelegate getNextTag]; if (callback) { - [self setPendingSend:callback forKey:@(_sendTag)]; + [_clientDelegate setPendingSend:callback forKey:sendTag]; } - [_tcpSocket writeData:data withTimeout:-1 tag:_sendTag]; - - _sendTag++; + [_tcpSocket writeData:data withTimeout:-1 tag:sendTag.longValue]; [_tcpSocket readDataWithTimeout:-1 tag:_id.longValue]; } diff --git a/ios/TcpSockets.m b/ios/TcpSockets.m index eee1fac..39e337b 100644 --- a/ios/TcpSockets.m +++ b/ios/TcpSockets.m @@ -14,6 +14,15 @@ // offset native ids by 5000 #define COUNTER_OFFSET 5000 +@interface TcpSockets() { + +@private + NSMutableDictionary *_pendingSends; + NSLock *_lock; + long _tag; +} +@end + @implementation TcpSockets { NSMutableDictionary *_clients; @@ -22,6 +31,19 @@ @implementation TcpSockets RCT_EXPORT_MODULE() +- (id)init { + self = [super init]; + if (self) { + _pendingSends = [NSMutableDictionary dictionary]; + _lock = [[NSLock alloc] init]; + } + return self; +} + +- (NSNumber*)getNextTag { + return [NSNumber numberWithLong:_tag++]; +} + - (NSArray *)supportedEvents { return @[@"connect", @@ -200,4 +222,37 @@ -(NSNumber*)getNextId { return @(_counter++ + COUNTER_OFFSET); } +- (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]; + } +} + @end