Skip to content

Commit

Permalink
Bubbled up reusePort to the API as an option on createSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
intmainvoid authored and mvayngrib committed Oct 25, 2017
1 parent 6f62be0 commit 822b1e0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion UdpSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ios/UdpSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions ios/UdpSocketClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ - (void)dropPendingSend:(NSNumber *)key
}
}

- (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) {
Expand All @@ -96,7 +96,9 @@ - (BOOL) bind:(u_int16_t)port address:(NSString *)address error:(NSError **) err

[_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) {
Expand Down
3 changes: 2 additions & 1 deletion ios/UdpSockets.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ - (void)dealloc
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"]);
Expand Down

0 comments on commit 822b1e0

Please sign in to comment.