2015-05-09 21:55:49 +00:00
|
|
|
//
|
|
|
|
// react-native-udp
|
|
|
|
//
|
|
|
|
// Created by Mark Vayngrib on 05/10/15.
|
|
|
|
// Copyright (c) 2015 Tradle, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
2015-05-10 14:26:29 +00:00
|
|
|
* @providesModule UdpSocket
|
2015-05-09 21:55:49 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-05-17 20:55:38 +00:00
|
|
|
var inherits = require('inherits')
|
|
|
|
var EventEmitter = require('events').EventEmitter
|
2015-07-29 14:46:21 +00:00
|
|
|
var {
|
|
|
|
DeviceEventEmitter,
|
|
|
|
NativeModules
|
|
|
|
} = require('react-native');
|
|
|
|
var Sockets = NativeModules.UdpSockets
|
2015-05-16 15:45:11 +00:00
|
|
|
var base64 = require('base64-js')
|
2015-05-09 21:55:49 +00:00
|
|
|
var noop = function () {}
|
|
|
|
var instances = 0
|
|
|
|
var STATE = {
|
|
|
|
UNBOUND: 0,
|
|
|
|
BINDING: 1,
|
|
|
|
BOUND: 2
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:13:36 +00:00
|
|
|
function UdpSocket(options, onmessage) {
|
2015-05-17 20:55:38 +00:00
|
|
|
EventEmitter.call(this)
|
|
|
|
|
2015-06-01 15:13:36 +00:00
|
|
|
if (typeof options === 'string') options = { type: options }
|
|
|
|
|
|
|
|
this.type = options.type
|
2015-10-11 13:17:45 +00:00
|
|
|
this._id = '' + instances++
|
2015-05-11 00:41:43 +00:00
|
|
|
this._state = STATE.UNBOUND
|
2015-05-15 21:52:43 +00:00
|
|
|
this._subscription = DeviceEventEmitter.addListener(
|
2015-05-11 00:41:43 +00:00
|
|
|
'udp-' + this._id + '-data', this._onReceive.bind(this)
|
|
|
|
);
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
// ensure compatibility with node's EventEmitter
|
|
|
|
if (!this.on) this.on = this.addListener.bind(this)
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-06-01 15:13:36 +00:00
|
|
|
if (onmessage) this.on('message', onmessage)
|
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
Sockets.createSocket(this._id, {
|
2015-06-01 15:13:36 +00:00
|
|
|
type: this.type
|
2015-05-11 00:41:43 +00:00
|
|
|
}) // later
|
|
|
|
}
|
2015-05-10 14:26:29 +00:00
|
|
|
|
2015-05-17 20:55:38 +00:00
|
|
|
inherits(UdpSocket, EventEmitter)
|
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
UdpSocket.prototype._debug = function() {
|
|
|
|
// for now
|
2015-05-18 14:37:00 +00:00
|
|
|
if (typeof DEBUG !== 'undefined' && DEBUG) {
|
|
|
|
var args = [].slice.call(arguments)
|
|
|
|
args.unshift('socket-' + this._id)
|
|
|
|
console.log.apply(console, args)
|
|
|
|
}
|
2015-05-11 00:41:43 +00:00
|
|
|
}
|
2015-05-10 14:26:29 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
UdpSocket.prototype.bind = function(port, address, callback) {
|
|
|
|
var self = this
|
|
|
|
|
|
|
|
if (this._state !== STATE.UNBOUND) throw new Error('Socket is already bound')
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
if (typeof address === 'function') {
|
|
|
|
callback = address
|
|
|
|
address = undefined
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
if (!address) address = '0.0.0.0'
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
if (!port) port = 0
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
if (callback) this.once('listening', callback.bind(this))
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
this._state = STATE.BINDING
|
|
|
|
this._debug('binding, address:', address, 'port:', port)
|
|
|
|
Sockets.bind(this._id, port, address, function(err, addr) {
|
|
|
|
if (err) {
|
|
|
|
// questionable: may want to self-destruct and
|
|
|
|
// force user to create a new socket
|
|
|
|
self._state = STATE.UNBOUND
|
|
|
|
self._debug('failed to bind', err)
|
|
|
|
return self.emit('error', err)
|
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
self._debug('bound to address:', addr.address, 'port:', addr.port)
|
|
|
|
self._address = addr.address
|
|
|
|
self._port = addr.port
|
|
|
|
self._state = STATE.BOUND
|
|
|
|
self.emit('listening')
|
|
|
|
})
|
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
UdpSocket.prototype.close = function() {
|
|
|
|
if (this._destroyed) return
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
this._destroyed = true
|
|
|
|
this._debug('closing')
|
|
|
|
this._subscription.remove();
|
|
|
|
|
|
|
|
Sockets.close(this._id, this._debug.bind(this, 'closed'))
|
|
|
|
this.emit('close')
|
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
UdpSocket.prototype._onReceive = function(info) {
|
|
|
|
this._debug('received', info)
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-16 17:06:21 +00:00
|
|
|
// from base64 string
|
2015-05-16 15:45:11 +00:00
|
|
|
var buf = base64.toByteArray(info.data)
|
2015-05-11 00:41:43 +00:00
|
|
|
var rinfo = {
|
|
|
|
address: info.address,
|
|
|
|
port: info.port,
|
|
|
|
family: 'IPv4', // not necessarily
|
|
|
|
size: buf.length
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 17:06:21 +00:00
|
|
|
if (typeof Buffer !== 'undefined') buf = new Buffer(buf, 'binary')
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
this.emit('message', buf, rinfo)
|
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
/**
|
|
|
|
* socket.send(buf, offset, length, port, address, [callback])
|
|
|
|
*
|
|
|
|
* For UDP sockets, the destination port and IP address must be
|
|
|
|
* specified. A string may be supplied for the address parameter, and it will
|
|
|
|
* be resolved with DNS. An optional callback may be specified to detect any
|
|
|
|
* DNS errors and when buf may be re-used. Note that DNS lookups will delay
|
|
|
|
* the time that a send takes place, at least until the next tick. The only
|
|
|
|
* way to know for sure that a send has taken place is to use the callback.
|
|
|
|
*
|
|
|
|
* If the socket has not been previously bound with a call to bind, it's
|
|
|
|
* assigned a random port number and bound to the "all interfaces" address
|
|
|
|
* (0.0.0.0 for udp4 sockets, ::0 for udp6 sockets).
|
|
|
|
*
|
|
|
|
* @param {Array|string} message to be sent
|
|
|
|
* @param {number} offset Offset in the buffer where the message starts.
|
|
|
|
* @param {number} length Number of bytes in the message.
|
|
|
|
* @param {number} port destination port
|
|
|
|
* @param {string} address destination IP
|
|
|
|
* @param {function} callback Callback when message is done being delivered.
|
|
|
|
* Optional.
|
|
|
|
*/
|
|
|
|
// UdpSocket.prototype.send = function (buf, host, port, cb) {
|
|
|
|
UdpSocket.prototype.send = function(buffer, offset, length, port, address, callback) {
|
|
|
|
var self = this
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
if (offset !== 0) throw new Error('Non-zero offset not supported yet')
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
if (this._state === STATE.UNBOUND) {
|
2015-05-16 15:45:26 +00:00
|
|
|
var args = [].slice.call(arguments)
|
|
|
|
return this.bind(0, function(err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
self.send.apply(self, args)
|
|
|
|
})
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
2015-05-11 00:41:43 +00:00
|
|
|
else if (this._state === STATE.BINDING) {
|
|
|
|
// we're ok, GCDAsync(Udp)Socket handles queueing internally
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
callback = callback || noop
|
2015-05-16 15:45:11 +00:00
|
|
|
var str
|
2015-05-11 00:41:43 +00:00
|
|
|
if (typeof buffer === 'string') {
|
2015-05-16 15:45:11 +00:00
|
|
|
console.warn('socket.send(): interpreting as base64')
|
|
|
|
str = buffer
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
2015-05-11 00:41:43 +00:00
|
|
|
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(buffer)) {
|
2015-05-16 15:45:11 +00:00
|
|
|
str = buffer.toString('base64')
|
|
|
|
}
|
|
|
|
else if (buffer instanceof Uint8Array || Array.isArray(buffer)) {
|
|
|
|
str = base64.fromByteArray(buffer)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error('invalid message format')
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 15:45:11 +00:00
|
|
|
self._debug('sending', buffer, str)
|
|
|
|
Sockets.send(this._id, str, +port, address, function(err) {
|
2015-05-11 00:41:43 +00:00
|
|
|
if (err) {
|
|
|
|
self._debug('send failed', err)
|
|
|
|
return callback(err)
|
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
self._debug('sent')
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
UdpSocket.prototype.address = function() {
|
|
|
|
if (this._state !== STATE.BOUND) {
|
|
|
|
throw new Error('socket is not bound yet')
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
return {
|
|
|
|
address: this._address,
|
|
|
|
port: this._port,
|
|
|
|
family: 'IPv4'
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
2015-05-11 00:41:43 +00:00
|
|
|
}
|
2015-05-09 21:55:49 +00:00
|
|
|
|
2015-05-11 00:41:43 +00:00
|
|
|
UdpSocket.prototype.setBroadcast = function(flag) {
|
2015-07-29 16:16:01 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
});
|
2015-05-11 00:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.setTTL = function(ttl) {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.setMulticastTTL = function(ttl, callback) {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.setMulticastLoopback = function(flag, callback) {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.addMembership = function(multicastAddress, multicastInterface, callback) {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.dropMembership = function(multicastAddress, multicastInterface, callback) {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.ref = function() {
|
|
|
|
// anything?
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket.prototype.unref = function() {
|
|
|
|
// anything?
|
2015-05-09 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 15:05:19 +00:00
|
|
|
module.exports = UdpSocket
|