From 822b1e09337fded4d63baab0db3e0a86724b46eb Mon Sep 17 00:00:00 2001 From: Gavin Conway Date: Tue, 22 Aug 2017 17:17:03 +1000 Subject: [PATCH] Bubbled up reusePort to the API as an option on createSocket --- UdpSocket.js | 3 ++- ios/UdpSocketClient.h | 3 ++- ios/UdpSocketClient.m | 6 ++++-- ios/UdpSockets.m | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/UdpSocket.js b/UdpSocket.js index c26a2c4..2bf86a4 100644 --- a/UdpSocket.js +++ b/UdpSocket.js @@ -43,6 +43,7 @@ function UdpSocket(options, onmessage) { } this.type = options.type + this.reusePort = options && options.reusePort this._ipv = Number(this.type.slice(3)) this._ipRegex = ipRegex['v' + this._ipv]({ exact: true }) this._id = instances++ @@ -89,7 +90,7 @@ UdpSocket.prototype.bind = function(port, address, callback) { this._state = STATE.BINDING this._debug('binding, address:', address, 'port:', port) - Sockets.bind(this._id, port, address, function(err, addr) { + Sockets.bind(this._id, port, address, {reusePort: this.reusePort }, function(err, addr) { err = normalizeError(err) if (err) { // questionable: may want to self-destruct and diff --git a/ios/UdpSocketClient.h b/ios/UdpSocketClient.h index c40b735..7c43af6 100644 --- a/ios/UdpSocketClient.h +++ b/ios/UdpSocketClient.h @@ -61,9 +61,10 @@ typedef enum RCTUDPError RCTUDPError; * * @param port * @param host ip address + * @param options (such as 'reusePort') * @return true if bound, false if there was an error */ -- (BOOL)bind:(u_int16_t) port address:(NSString*) address error:(NSError**)error; +- (BOOL)bind:(u_int16_t) port address:(NSString*)address options:(NSDictionary *)options error:(NSError**)error; /** * Join multicast groupt diff --git a/ios/UdpSocketClient.m b/ios/UdpSocketClient.m index 918964e..82e3288 100644 --- a/ios/UdpSocketClient.m +++ b/ios/UdpSocketClient.m @@ -78,7 +78,7 @@ NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain"; } } -- (BOOL) bind:(u_int16_t)port address:(NSString *)address error:(NSError **) error +- (BOOL)bind:(u_int16_t)port address:(NSString *)address options:(NSDictionary *)options error:(NSError **) error { if (_port) { @@ -96,7 +96,9 @@ NSString *const RCTUDPErrorDomain = @"RCTUDPErrorDomain"; [_udpSocket setMaxReceiveIPv4BufferSize:UINT16_MAX]; [_udpSocket setMaxReceiveIPv6BufferSize:UINT16_MAX]; - [_udpSocket enableReusePort:true error:error]; + + BOOL reusePort = options[@"reusePort"] ?: NO; + [_udpSocket enableReusePort:reusePort error:error]; BOOL result; if (address) { diff --git a/ios/UdpSockets.m b/ios/UdpSockets.m index 5d60e20..a007f21 100644 --- a/ios/UdpSockets.m +++ b/ios/UdpSockets.m @@ -53,13 +53,14 @@ RCT_EXPORT_METHOD(createSocket:(nonnull NSNumber*)cId withOptions:(NSDictionary* RCT_EXPORT_METHOD(bind:(nonnull NSNumber*)cId port:(int)port address:(NSString *)address + options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { UdpSocketClient* client = [self findClient:cId callback:callback]; if (!client) return; NSError *error = nil; - if (![client bind:port address:address error:&error]) + if (![client bind:port address:address options:options error:&error]) { NSString *msg = error.localizedFailureReason ?: error.localizedDescription; callback(@[msg ?: @"unknown error when binding"]);