ensure events correspond to generated socket ids
This commit is contained in:
parent
0b356bbaac
commit
f38894e1aa
|
@ -48,6 +48,10 @@ class RctSockets extends Component {
|
||||||
socket.on('error', (error) => {
|
socket.on('error', (error) => {
|
||||||
this.updateChatter('error ' + error);
|
this.updateChatter('error ' + error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('close', (error) => {
|
||||||
|
this.updateChatter('server client closed ' + (error ? error : ''));
|
||||||
|
});
|
||||||
}).listen(serverPort, () => {
|
}).listen(serverPort, () => {
|
||||||
this.updateChatter('opened server on ' + JSON.stringify(server.address()));
|
this.updateChatter('opened server on ' + JSON.stringify(server.address()));
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#import <arpa/inet.h>
|
#import <arpa/inet.h>
|
||||||
#import "TcpSocketClient.h"
|
#import "TcpSocketClient.h"
|
||||||
#import "RCTBridgeModule.h"
|
#import "RCTBridgeModule.h"
|
||||||
|
#import "RCTLog.h"
|
||||||
#import "GCDAsyncSocket.h"
|
#import "GCDAsyncSocket.h"
|
||||||
|
|
||||||
NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
|
@ -18,7 +19,6 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
|
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
|
||||||
NSLock *_lock;
|
NSLock *_lock;
|
||||||
long _sendTag;
|
long _sendTag;
|
||||||
BOOL _isListening;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)initWithClientId:(NSNumber *)clientID andConfig:(id<SocketClientDelegate>)aDelegate;
|
- (id)initWithClientId:(NSNumber *)clientID andConfig:(id<SocketClientDelegate>)aDelegate;
|
||||||
|
@ -46,8 +46,8 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
_clientDelegate = aDelegate;
|
_clientDelegate = aDelegate;
|
||||||
_pendingSends = [NSMutableDictionary dictionary];
|
_pendingSends = [NSMutableDictionary dictionary];
|
||||||
_lock = [[NSLock alloc] init];
|
_lock = [[NSLock alloc] init];
|
||||||
_isListening = NO;
|
|
||||||
_tcpSocket = tcpSocket;
|
_tcpSocket = tcpSocket;
|
||||||
|
[_tcpSocket setUserData: clientID];
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
|
@ -64,6 +64,8 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
}
|
}
|
||||||
|
|
||||||
_tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]];
|
_tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]];
|
||||||
|
[_tcpSocket setUserData: _id];
|
||||||
|
|
||||||
BOOL result = false;
|
BOOL result = false;
|
||||||
|
|
||||||
NSString *localAddress = (options?options[@"localAddress"]:nil);
|
NSString *localAddress = (options?options[@"localAddress"]:nil);
|
||||||
|
@ -118,18 +120,19 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
}
|
}
|
||||||
|
|
||||||
_tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]];
|
_tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:[self methodQueue]];
|
||||||
|
[_tcpSocket setUserData: _id];
|
||||||
|
|
||||||
// GCDAsyncSocket doesn't recognize 0.0.0.0
|
// GCDAsyncSocket doesn't recognize 0.0.0.0
|
||||||
if ([@"0.0.0.0" isEqualToString: host]) {
|
if ([@"0.0.0.0" isEqualToString: host]) {
|
||||||
host = @"localhost";
|
host = @"localhost";
|
||||||
}
|
}
|
||||||
_isListening = [_tcpSocket acceptOnInterface:host port:port error:error];
|
BOOL isListening = [_tcpSocket acceptOnInterface:host port:port error:error];
|
||||||
if (_isListening == YES) {
|
if (isListening == YES) {
|
||||||
[_clientDelegate onConnect: self];
|
[_clientDelegate onConnect: self];
|
||||||
[_tcpSocket readDataWithTimeout:-1 tag:_id.longValue];
|
[_tcpSocket readDataWithTimeout:-1 tag:_id.longValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
return _isListening;
|
return isListening;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key
|
- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key
|
||||||
|
@ -190,18 +193,20 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
|
|
||||||
- (void)end
|
- (void)end
|
||||||
{
|
{
|
||||||
_isListening = NO;
|
|
||||||
[_tcpSocket disconnectAfterWriting];
|
[_tcpSocket disconnectAfterWriting];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)destroy
|
- (void)destroy
|
||||||
{
|
{
|
||||||
_isListening = NO;
|
|
||||||
[_tcpSocket disconnect];
|
[_tcpSocket disconnect];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
|
- (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];
|
[_clientDelegate onData:@(tag) data:data];
|
||||||
|
|
||||||
[sock readDataWithTimeout:-1 tag:tag];
|
[sock readDataWithTimeout:-1 tag:tag];
|
||||||
|
@ -219,7 +224,11 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
|
|
||||||
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
|
- (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];
|
[_clientDelegate onConnect:self];
|
||||||
|
|
||||||
[sock readDataWithTimeout:-1 tag:_id.longValue];
|
[sock readDataWithTimeout:-1 tag:_id.longValue];
|
||||||
|
@ -234,10 +243,12 @@ NSString *const RCTTCPErrorDomain = @"RCTTCPErrorDomain";
|
||||||
|
|
||||||
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
|
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
|
||||||
{
|
{
|
||||||
if (!_clientDelegate) return;
|
if (!_clientDelegate) {
|
||||||
if (_isListening == NO) {
|
RCTLogError(@"socketDidDisconnect with nil clientDelegate for %@", [sock userData])
|
||||||
[_clientDelegate onClose:self withError:(!err || err.code == GCDAsyncSocketClosedError ? nil : err)];
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[_clientDelegate onClose:[sock userData] withError:(!err || err.code == GCDAsyncSocketClosedError ? nil : err)];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSError *)badInvocationError:(NSString *)errMsg
|
- (NSError *)badInvocationError:(NSString *)errMsg
|
||||||
|
|
|
@ -127,8 +127,13 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId
|
||||||
body:base64String];
|
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) {
|
if (err) {
|
||||||
[self onError:client withError:err];
|
[self onError:client withError:err];
|
||||||
}
|
}
|
||||||
|
@ -136,7 +141,6 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId
|
||||||
[self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"tcp-%@-close", client.id]
|
[self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"tcp-%@-close", client.id]
|
||||||
body:err == nil ? @NO : @YES];
|
body:err == nil ? @NO : @YES];
|
||||||
|
|
||||||
client.clientDelegate = nil;
|
|
||||||
[_clients removeObjectForKey:client.id];
|
[_clients removeObjectForKey:client.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +178,6 @@ RCT_EXPORT_METHOD(listen:(nonnull NSNumber*)cId
|
||||||
if (!client) return;
|
if (!client) return;
|
||||||
|
|
||||||
[client destroy];
|
[client destroy];
|
||||||
[_clients removeObjectForKey:cId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSNumber*)getNextId {
|
-(NSNumber*)getNextId {
|
||||||
|
|
Loading…
Reference in New Issue