Merge pull request #5 from yesmeck/master

Implement setBroadcast()
This commit is contained in:
Mark Vayngrib 2015-07-29 15:27:06 -04:00
commit c762ba231b
4 changed files with 38 additions and 1 deletions

View File

@ -205,7 +205,18 @@ UdpSocket.prototype.address = function() {
} }
UdpSocket.prototype.setBroadcast = function(flag) { UdpSocket.prototype.setBroadcast = function(flag) {
// nothing yet var self = this
if (this._state !== STATE.BOUND) {
throw new Error('you must bind before setBroadcast()')
}
Sockets.setBroadcast(this._id, flag, function(err) {
if (err) {
self._debug('failed to set broadcast', err)
return self.emit('error', err)
}
});
} }
UdpSocket.prototype.setTTL = function(ttl) { UdpSocket.prototype.setTTL = function(ttl) {

View File

@ -82,4 +82,9 @@ typedef enum RCTUDPError RCTUDPError;
*/ */
- (void)close; - (void)close;
/**
* Enable broadcast
*/
- (BOOL)setBroadcast:(BOOL) flag error:(NSError **) error;
@end @end

View File

@ -126,6 +126,12 @@ remoteAddress:(NSString *)address
[_udpSocket close]; [_udpSocket close];
} }
- (BOOL) setBroadcast:(BOOL)flag
error:(NSError **)error
{
return [_udpSocket enableBroadcast:flag error:error];
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address fromAddress:(NSData *)address
withFilterContext:(id)filterContext withFilterContext:(id)filterContext

View File

@ -82,6 +82,21 @@ RCT_EXPORT_METHOD(close:(NSString*)cId
if (callback) callback(@[]); if (callback) callback(@[]);
} }
RCT_EXPORT_METHOD(setBroadcast:(NSString*)cId
flag:(BOOL)flag
callback:(RCTResponseSenderBlock)callback) {
UdpSocketClient* client = [self findClient:cId callback:callback];
if (!client) return;
NSError *error = nil;
if (![client setBroadcast:flag error:&error])
{
callback(@[error]);
return;
}
callback(@[[NSNull null]]);
}
- (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port - (void) onData:(UdpSocketClient*) client data:(NSData *)data host:(NSString *)host port:(uint16_t)port
{ {
NSString *clientID = [[_clients allKeysForObject:client] objectAtIndex:0]; NSString *clientID = [[_clients allKeysForObject:client] objectAtIndex:0];