forked from tradle/react-native-udp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpSocket.js
301 lines (245 loc) · 7.65 KB
/
UdpSocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// react-native-udp
//
// Created by Mark Vayngrib on 05/10/15.
// Copyright (c) 2015 Tradle, Inc. All rights reserved.
//
/**
* @providesModule UdpSocket
* @flow
*/
'use strict';
var inherits = require('inherits')
var EventEmitter = require('events').EventEmitter
var {
DeviceEventEmitter,
NativeModules,
Platform
} = require('react-native');
var Sockets = NativeModules.UdpSockets
var base64 = require('base64-js')
var ipRegex = require('ip-regex')
// RFC 952 hostname format
var hostnameRegex = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/;
var noop = function () {}
var instances = 0
var STATE = {
UNBOUND: 0,
BINDING: 1,
BOUND: 2
}
module.exports = UdpSocket
function UdpSocket(options, onmessage) {
EventEmitter.call(this)
if (typeof options === 'string') options = { type: options }
if (options.type !== 'udp4' && options.type !== 'udp6') {
throw new Error('invalid udp socket type')
}
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++
this._state = STATE.UNBOUND
this._subscription = DeviceEventEmitter.addListener(
'udp-' + this._id + '-data', this._onReceive.bind(this)
);
// ensure compatibility with node's EventEmitter
if (!this.on) this.on = this.addListener.bind(this)
if (onmessage) this.on('message', onmessage)
Sockets.createSocket(this._id, {
type: this.type
}) // later
}
inherits(UdpSocket, EventEmitter)
UdpSocket.prototype._debug = function() {
if (__DEV__) {
var args = [].slice.call(arguments)
args.unshift('socket-' + this._id)
console.log.apply(console, args)
}
}
UdpSocket.prototype.bind = function(port, address, callback) {
var self = this
if (this._state !== STATE.UNBOUND) throw new Error('Socket is already bound')
if (typeof address === 'function') {
callback = address
address = undefined
}
if (!address) address = '0.0.0.0'
if (!port) port = 0
if (callback) this.once('listening', callback.bind(this))
this._state = STATE.BINDING
this._debug('binding, address:', address, 'port:', port)
const bindArgs = [this._id, port, address]
if (Platform.OS === 'ios') {
bindArgs.push({ reusePort: this.reusePort })
}
Sockets.bind(...bindArgs, function(err, addr) {
err = normalizeError(err)
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)
if (callback) callback(err)
return self.emit('error', err)
}
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')
})
}
UdpSocket.prototype.close = function (callback=noop) {
if (this._destroyed) {
return setImmediate(callback)
}
this.once('close', callback)
if (this._destroying) return
this._destroying = true
this._debug('closing')
this._subscription.remove();
Sockets.close(this._id, err => {
if (err) return this.emit('error', err)
this._destroyed = true
this._debug('closed')
this.emit('close')
})
}
UdpSocket.prototype._onReceive = function(info) {
// from base64 string
var buf = typeof Buffer === 'undefined'
? base64.toByteArray(info.data)
: new Buffer(info.data, 'base64')
var rinfo = {
address: info.address,
port: info.port,
family: 'IPv4', // not necessarily
size: buf.length
}
this.emit('message', buf, rinfo)
}
/**
* 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
if (typeof port !== 'number') throw new Error('invalid port')
if (!isValidIpOrHostname(address, this._ipRegex)) throw new Error('invalid address')
if (offset !== 0) throw new Error('Non-zero offset not supported yet')
if (this._state === STATE.UNBOUND) {
var args = [].slice.call(arguments)
return this.bind(0, function(err) {
if (err) return callback(err)
self.send.apply(self, args)
})
}
else if (this._state === STATE.BINDING) {
// we're ok, GCDAsync(Udp)Socket handles queueing internally
}
callback = callback || noop
var str
if (typeof buffer === 'string') {
console.warn('socket.send(): interpreting as base64')
str = buffer
}
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(buffer)) {
str = buffer.toString('base64')
}
else if (buffer instanceof Uint8Array || Array.isArray(buffer)) {
str = base64.fromByteArray(buffer)
}
else {
throw new Error('invalid message format')
}
Sockets.send(this._id, str, +port, address, function(err) {
err = normalizeError(err)
if (err) {
self._debug('send failed', err)
return callback(err)
}
callback()
})
}
UdpSocket.prototype.address = function() {
if (this._state !== STATE.BOUND) {
throw new Error('socket is not bound yet')
}
return {
address: this._address,
port: this._port,
family: 'IPv4'
}
}
UdpSocket.prototype.setBroadcast = function(flag) {
var self = this
if (this._state !== STATE.BOUND) {
throw new Error('you must bind before setBroadcast()')
}
Sockets.setBroadcast(this._id, flag, function(err) {
err = normalizeError(err)
if (err) {
self._debug('failed to set broadcast', err)
return self.emit('error', err)
}
});
}
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) {
if (this._state !== STATE.BOUND) {
throw new Error('you must bind before addMembership()')
}
Sockets.addMembership(this._id, multicastAddress);
}
UdpSocket.prototype.dropMembership = function(multicastAddress) {
if (this._state !== STATE.BOUND) {
throw new Error('you must bind before addMembership()')
}
Sockets.dropMembership(this._id, multicastAddress);
}
UdpSocket.prototype.ref = function() {
// anything?
}
UdpSocket.prototype.unref = function() {
// anything?
}
function isValidIpOrHostname (address, ipRegex) {
if (typeof address !== 'string') return false
return ipRegex.test(address) || hostnameRegex.test(address);
}
function normalizeError (err) {
if (err) {
if (typeof err === 'string') err = new Error(err)
return err
}
}