Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #86 #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ios/TcpSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 5 additions & 44 deletions ios/TcpSocketClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ @interface TcpSocketClient()
{
@private
GCDAsyncSocket *_tcpSocket;
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
NSLock *_lock;
long _sendTag;
}

- (id)initWithClientId:(NSNumber *)clientID andConfig:(id<SocketClientDelegate>)aDelegate;
Expand All @@ -43,8 +40,6 @@ - (id)initWithClientId:(NSNumber *)clientID andConfig:(id<SocketClientDelegate>)
if (self) {
_id = clientID;
_clientDelegate = aDelegate;
_pendingSends = [NSMutableDictionary dictionary];
_lock = [[NSLock alloc] init];
_tcpSocket = tcpSocket;
[_tcpSocket setUserData: clientID];
}
Expand Down Expand Up @@ -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];
}
Expand Down
60 changes: 60 additions & 0 deletions ios/TcpSockets.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
// offset native ids by 5000
#define COUNTER_OFFSET 5000

@interface TcpSockets() {

@private
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
NSLock *_lock;
long _tag;
}
@end

@implementation TcpSockets
{
NSMutableDictionary<NSNumber *,TcpSocketClient *> *_clients;
Expand All @@ -22,6 +31,23 @@ @implementation TcpSockets

RCT_EXPORT_MODULE()

+ (BOOL)requiresMainQueueSetup {
return NO;
}

- (id)init {
self = [super init];
if (self) {
_pendingSends = [NSMutableDictionary dictionary];
_lock = [[NSLock alloc] init];
}
return self;
}

- (NSNumber*)getNextTag {
return [NSNumber numberWithLong:_tag++];
}

- (NSArray<NSString *> *)supportedEvents
{
return @[@"connect",
Expand Down Expand Up @@ -200,4 +226,38 @@ -(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