From 044ab6e57a3bfb46da351224b77d800857673348 Mon Sep 17 00:00:00 2001 From: James Cowan Date: Thu, 10 Nov 2016 17:49:33 +0000 Subject: [PATCH] add multicast --- ios/UdpSocketClient.h | 16 ++++++++++++++++ ios/UdpSocketClient.m | 18 ++++++++++++++++++ ios/UdpSockets.m | 14 ++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/ios/UdpSocketClient.h b/ios/UdpSocketClient.h index 5e72a63..db92994 100644 --- a/ios/UdpSocketClient.h +++ b/ios/UdpSocketClient.h @@ -65,6 +65,22 @@ typedef enum RCTUDPError RCTUDPError; */ - (BOOL)bind:(u_int16_t) port address:(NSString*) address error:(NSError**)error; +/** + * Join multicast groupt + * + * @param address + * @return true if joined, false if there was an error + */ +- (BOOL)joinMulticastGroup:(NSString *)address error:(NSError **) error; + +/** + * leave multicast groupt + * + * @param address + * @return true if joined, false if there was an error + */ +- (BOOL)leaveMulticastGroup:(NSString *)address error:(NSError **) error; + /** * send data to another host and port * diff --git a/ios/UdpSocketClient.m b/ios/UdpSocketClient.m index 956053a..c814ad8 100644 --- a/ios/UdpSocketClient.m +++ b/ios/UdpSocketClient.m @@ -110,6 +110,24 @@ NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain"; return result && [_udpSocket beginReceiving:error]; } +- (BOOL)joinMulticastGroup:(NSString *)address error:(NSError **) error +{ + if(![_udpSocket joinMulticastGroup:address error:&error]){ + NSLog(@"Error joining multicast group: %@", error); + return false; + } + return true; +} + +- (BOOL)leaveMulticastGroup:(NSString *)address error:(NSError **) error +{ + if(![_udpSocket leaveMulticastGroup:address error:&error]){ + NSLog(@"Error leaving multicast group: %@", error); + return false; + } + return true; +} + - (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)msgTag { NSNumber* tagNum = [NSNumber numberWithLong:msgTag]; diff --git a/ios/UdpSockets.m b/ios/UdpSockets.m index 16559e8..6f0f8ad 100644 --- a/ios/UdpSockets.m +++ b/ios/UdpSockets.m @@ -106,12 +106,22 @@ RCT_EXPORT_METHOD(setBroadcast:(nonnull NSNumber*)cId RCT_EXPORT_METHOD(addMembership:(nonnull NSNumber*)cId multicastAddress:(NSString *)address) { - /* nop */ + UdpSocketClient *client = _clients[cId]; + + if (!client) return; + + NSError *error = nil; + [client joinMulticastGroup:address error:&error]; } RCT_EXPORT_METHOD(dropMembership:(nonnull NSNumber*)cId multicastAddress:(NSString *)address) { - /* nop */ + UdpSocketClient *client = _clients[cId]; + + if (!client) return; + + NSError *error = nil; + [client leaveMulticastGroup:address error:&error]; } - (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port