Skip to content

Commit 356e18c

Browse files
authored
Merge pull request #9 from eventengineering/bugfix/callback-queues
Maintain callback queues accordingly
2 parents 5c2b2a5 + 6e18e7d commit 356e18c

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed

ios/TcpSocketClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ typedef enum RCTTCPError RCTTCPError;
3131
- (void)onData:(NSNumber *)clientID data:(NSData *)data;
3232
- (void)onClose:(TcpSocketClient*)client withError:(NSError *)err;
3333
- (void)onError:(TcpSocketClient*)client withError:(NSError *)err;
34+
- (NSNumber*)getNextTag;
35+
- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key;
36+
- (RCTResponseSenderBlock)getPendingSend:(NSNumber *)key;
37+
- (void)dropPendingSend:(NSNumber *)key;
3438
- (NSNumber*)getNextId;
3539

3640
@end

ios/TcpSocketClient.m

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ @interface TcpSocketClient()
1515
{
1616
@private
1717
GCDAsyncSocket *_tcpSocket;
18-
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
19-
NSLock *_lock;
20-
long _sendTag;
2118
}
2219

2320
- (id)initWithClientId:(NSNumber *)clientID andConfig:(id<SocketClientDelegate>)aDelegate;
@@ -43,8 +40,6 @@ - (id)initWithClientId:(NSNumber *)clientID andConfig:(id<SocketClientDelegate>)
4340
if (self) {
4441
_id = clientID;
4542
_clientDelegate = aDelegate;
46-
_pendingSends = [NSMutableDictionary dictionary];
47-
_lock = [[NSLock alloc] init];
4843
_tcpSocket = tcpSocket;
4944
[_tcpSocket setUserData: clientID];
5045
}
@@ -134,58 +129,24 @@ - (BOOL)listen:(NSString *)host port:(int)port error:(NSError **)error
134129
return isListening;
135130
}
136131

137-
- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key
138-
{
139-
[_lock lock];
140-
@try {
141-
[_pendingSends setObject:callback forKey:key];
142-
}
143-
@finally {
144-
[_lock unlock];
145-
}
146-
}
147-
148-
- (RCTResponseSenderBlock)getPendingSend:(NSNumber *)key
149-
{
150-
[_lock lock];
151-
@try {
152-
return [_pendingSends objectForKey:key];
153-
}
154-
@finally {
155-
[_lock unlock];
156-
}
157-
}
158-
159-
- (void)dropPendingSend:(NSNumber *)key
160-
{
161-
[_lock lock];
162-
@try {
163-
[_pendingSends removeObjectForKey:key];
164-
}
165-
@finally {
166-
[_lock unlock];
167-
}
168-
}
169-
170132
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)msgTag
171133
{
172134
NSNumber* tagNum = [NSNumber numberWithLong:msgTag];
173-
RCTResponseSenderBlock callback = [self getPendingSend:tagNum];
135+
RCTResponseSenderBlock callback = [_clientDelegate getPendingSend:tagNum];
174136
if (callback) {
175137
callback(@[]);
176-
[self dropPendingSend:tagNum];
138+
[_clientDelegate dropPendingSend:tagNum];
177139
}
178140
}
179141

180142
- (void) writeData:(NSData *)data
181143
callback:(RCTResponseSenderBlock)callback
182144
{
145+
NSNumber *sendTag = [_clientDelegate getNextTag];
183146
if (callback) {
184-
[self setPendingSend:callback forKey:@(_sendTag)];
147+
[_clientDelegate setPendingSend:callback forKey:sendTag];
185148
}
186-
[_tcpSocket writeData:data withTimeout:-1 tag:_sendTag];
187-
188-
_sendTag++;
149+
[_tcpSocket writeData:data withTimeout:-1 tag:sendTag.longValue];
189150

190151
[_tcpSocket readDataWithTimeout:-1 tag:_id.longValue];
191152
}

ios/TcpSockets.m

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
// offset native ids by 5000
1515
#define COUNTER_OFFSET 5000
1616

17+
@interface TcpSockets() {
18+
19+
@private
20+
NSMutableDictionary<NSNumber *, RCTResponseSenderBlock> *_pendingSends;
21+
NSLock *_lock;
22+
long _tag;
23+
}
24+
@end
25+
1726
@implementation TcpSockets
1827
{
1928
NSMutableDictionary<NSNumber *,TcpSocketClient *> *_clients;
@@ -22,6 +31,19 @@ @implementation TcpSockets
2231

2332
RCT_EXPORT_MODULE()
2433

34+
- (id)init {
35+
self = [super init];
36+
if (self) {
37+
_pendingSends = [NSMutableDictionary dictionary];
38+
_lock = [[NSLock alloc] init];
39+
}
40+
return self;
41+
}
42+
43+
- (NSNumber*)getNextTag {
44+
return [NSNumber numberWithLong:_tag++];
45+
}
46+
2547
- (NSArray<NSString *> *)supportedEvents
2648
{
2749
return @[@"connect",
@@ -200,4 +222,37 @@ -(NSNumber*)getNextId {
200222
return @(_counter++ + COUNTER_OFFSET);
201223
}
202224

225+
- (void)setPendingSend:(RCTResponseSenderBlock)callback forKey:(NSNumber *)key
226+
{
227+
[_lock lock];
228+
@try {
229+
[_pendingSends setObject:callback forKey:key];
230+
}
231+
@finally {
232+
[_lock unlock];
233+
}
234+
}
235+
236+
- (RCTResponseSenderBlock)getPendingSend:(NSNumber *)key
237+
{
238+
[_lock lock];
239+
@try {
240+
return [_pendingSends objectForKey:key];
241+
}
242+
@finally {
243+
[_lock unlock];
244+
}
245+
}
246+
247+
- (void)dropPendingSend:(NSNumber *)key
248+
{
249+
[_lock lock];
250+
@try {
251+
[_pendingSends removeObjectForKey:key];
252+
}
253+
@finally {
254+
[_lock unlock];
255+
}
256+
}
257+
203258
@end

0 commit comments

Comments
 (0)