From e8050d3fe8e4cde48307ec56909dd932e83ac7e7 Mon Sep 17 00:00:00 2001 From: Andy Prock Date: Mon, 21 Dec 2015 19:45:38 -0800 Subject: [PATCH] Fixes #15 Refactor the UdpSockets module _clients array to be a member variable. Make the UdpSocketClient's delegate a weak reference. remove reload notificaiton, and rely on dealloc to close all open sockets. --- ios/UdpSocketClient.h | 1 + ios/UdpSocketClient.m | 1 - ios/UdpSockets.h | 2 -- ios/UdpSockets.m | 65 +++++++++++++++---------------------------- 4 files changed, 24 insertions(+), 45 deletions(-) diff --git a/ios/UdpSocketClient.h b/ios/UdpSocketClient.h index 34562e2..5e72a63 100644 --- a/ios/UdpSocketClient.h +++ b/ios/UdpSocketClient.h @@ -38,6 +38,7 @@ typedef enum RCTUDPError RCTUDPError; @property (nonatomic, retain) NSString* id; @property (nonatomic, retain) NSString* host; @property (nonatomic) u_int16_t port; +@property (nonatomic, weak) id clientDelegate; ///--------------------------------------------------------------------------------------- /// @name Class Methods diff --git a/ios/UdpSocketClient.m b/ios/UdpSocketClient.m index a3134d5..5f1caba 100644 --- a/ios/UdpSocketClient.m +++ b/ios/UdpSocketClient.m @@ -20,7 +20,6 @@ NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain"; uint16_t _port; NSString* _address; GCDAsyncUdpSocket *_udpSocket; - id _clientDelegate; NSMutableDictionary *_pendingSends; NSLock *_lock; long tag; diff --git a/ios/UdpSockets.h b/ios/UdpSockets.h index 6d5e7a8..b28da9a 100644 --- a/ios/UdpSockets.h +++ b/ios/UdpSockets.h @@ -16,6 +16,4 @@ @interface UdpSockets : NSObject -+(NSMutableDictionary *)clients; - @end diff --git a/ios/UdpSockets.m b/ios/UdpSockets.m index dd66c35..1eee5e7 100644 --- a/ios/UdpSockets.m +++ b/ios/UdpSockets.m @@ -15,49 +15,39 @@ #import "UdpSocketClient.h" @implementation UdpSockets +{ + NSMutableDictionary *_clients; +} RCT_EXPORT_MODULE() @synthesize bridge = _bridge; -+ (void) initialize { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(closeAllSockets) - name:RCTReloadNotification - object:nil]; -} - -+(NSMutableDictionary*) clients +- (void)dealloc { - static NSMutableDictionary* c = nil; - - static dispatch_once_t oncePredicate; - - dispatch_once(&oncePredicate, ^{ - c = [[NSMutableDictionary alloc] init]; - }); - - return c; + for (NSNumber *cId in _clients.allKeys) { + [self closeClient:cId callback:nil]; + } } + RCT_EXPORT_METHOD(createSocket:(nonnull NSNumber*)cId withOptions:(NSDictionary*)options) { -// if (!UdpSockets._clients) UdpSockets._clients = [[NSMutableDictionary alloc] init]; - - NSMutableDictionary *_clients = [UdpSockets clients]; if (!cId) { RCTLogError(@"%@.createSocket called with nil id parameter.", [self class]); return; } - UdpSocketClient *client = [_clients objectForKey:cId]; - if (client) { + if (!_clients) { + _clients = [NSMutableDictionary new]; + } + + if (_clients[cId]) { RCTLogError(@"%@.createSocket called twice with the same id.", [self class]); return; } - client = [UdpSocketClient socketClientWithConfig:self]; - [_clients setObject:client forKey:cId]; + _clients[cId] = [UdpSocketClient socketClientWithConfig:self]; } RCT_EXPORT_METHOD(bind:(nonnull NSNumber*)cId @@ -65,7 +55,7 @@ RCT_EXPORT_METHOD(bind:(nonnull NSNumber*)cId address:(NSString *)address callback:(RCTResponseSenderBlock)callback) { - UdpSocketClient* client = [UdpSockets findClient:cId callback:callback]; + UdpSocketClient* client = [self findClient:cId callback:callback]; if (!client) return; NSError *error = nil; @@ -84,7 +74,7 @@ RCT_EXPORT_METHOD(send:(nonnull NSNumber*)cId port:(int)port address:(NSString*)address callback:(RCTResponseSenderBlock)callback) { - UdpSocketClient* client = [UdpSockets findClient:cId callback:callback]; + UdpSocketClient* client = [self findClient:cId callback:callback]; if (!client) return; // iOS7+ @@ -95,13 +85,13 @@ RCT_EXPORT_METHOD(send:(nonnull NSNumber*)cId RCT_EXPORT_METHOD(close:(nonnull NSNumber*)cId callback:(RCTResponseSenderBlock)callback) { - [UdpSockets closeClient:cId callback:callback]; + [self closeClient:cId callback:callback]; } RCT_EXPORT_METHOD(setBroadcast:(nonnull NSNumber*)cId flag:(BOOL)flag callback:(RCTResponseSenderBlock)callback) { - UdpSocketClient* client = [UdpSockets findClient:cId callback:callback]; + UdpSocketClient* client = [self findClient:cId callback:callback]; if (!client) return; NSError *error = nil; @@ -126,7 +116,6 @@ RCT_EXPORT_METHOD(dropMembership:(nonnull NSNumber*)cId - (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port { - NSMutableDictionary *_clients = [UdpSockets clients]; NSNumber *clientID = [[_clients allKeysForObject:client] objectAtIndex:0]; NSString *base64String = [data base64EncodedStringWithOptions:0]; [self.bridge.eventDispatcher sendDeviceEventWithName:[NSString stringWithFormat:@"udp-%@-data", clientID] @@ -138,10 +127,9 @@ RCT_EXPORT_METHOD(dropMembership:(nonnull NSNumber*)cId ]; } -+(UdpSocketClient*)findClient:(nonnull NSNumber*)cId callback:(RCTResponseSenderBlock)callback +-(UdpSocketClient*)findClient:(nonnull NSNumber*)cId callback:(RCTResponseSenderBlock)callback { - NSMutableDictionary *_clients = [UdpSockets clients]; - UdpSocketClient *client = [_clients objectForKey:cId]; + UdpSocketClient *client = _clients[cId]; if (!client) { if (!callback) { RCTLogError(@"%@.missing callback parameter.", [self class]); @@ -156,24 +144,17 @@ RCT_EXPORT_METHOD(dropMembership:(nonnull NSNumber*)cId return client; } -+(void) closeClient:(nonnull NSNumber*)cId +-(void) closeClient:(nonnull NSNumber*)cId callback:(RCTResponseSenderBlock)callback { - NSMutableDictionary *_clients = [UdpSockets clients]; - UdpSocketClient* client = [UdpSockets findClient:cId callback:callback]; + UdpSocketClient* client = [self findClient:cId callback:callback]; if (!client) return; + client.clientDelegate = nil; [client close]; [_clients removeObjectForKey:cId]; if (callback) callback(@[]); } -+(void) closeAllSockets { - NSMutableDictionary *_clients = [UdpSockets clients]; - for (NSNumber* cId in _clients) { - [UdpSockets closeClient:cId callback:nil]; - } -} - @end