parent
3eb82c2fde
commit
a742459272
|
@ -20,6 +20,7 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
|||
GCDAsyncSocket *_tcpSocket;
|
||||
id<SocketClientDelegate> _clientDelegate;
|
||||
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_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++;
|
||||
|
|
|
@ -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": {
|
||||
|
|
Loading…
Reference in New Issue