2015-12-04 17:44:16 +00:00
|
|
|
/**
|
2015-12-23 18:56:33 +00:00
|
|
|
* Copyright (c) 2015-present, Peel Technologies, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2015-12-04 17:44:16 +00:00
|
|
|
* @providesModule TcpSocket
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2015-12-15 00:16:45 +00:00
|
|
|
var ipRegex = require('ip-regex');
|
2015-12-04 17:44:16 +00:00
|
|
|
var {
|
|
|
|
DeviceEventEmitter,
|
|
|
|
NativeModules
|
|
|
|
} = require('react-native');
|
|
|
|
var Sockets = NativeModules.TcpSockets;
|
|
|
|
var base64 = require('base64-js');
|
2015-12-17 16:10:00 +00:00
|
|
|
var Base64Str = require('./base64-str');
|
2015-12-04 17:44:16 +00:00
|
|
|
var noop = function () {};
|
2015-12-28 19:12:21 +00:00
|
|
|
var instances = 0;
|
2015-12-04 17:44:16 +00:00
|
|
|
var STATE = {
|
|
|
|
DISCONNECTED: 0,
|
|
|
|
CONNECTING: 1,
|
|
|
|
CONNECTED: 2
|
|
|
|
};
|
|
|
|
|
2015-12-23 23:14:41 +00:00
|
|
|
function TcpSocket(options: ?{ id: ?number }) {
|
2015-12-28 19:12:21 +00:00
|
|
|
if (!(this instanceof TcpSocket)) {
|
|
|
|
return new TcpSocket(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EventEmitter instanceof Function) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
}
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-23 23:14:41 +00:00
|
|
|
if (options && options.id) {
|
|
|
|
// e.g. incoming server connections
|
|
|
|
this._id = Number(options.id);
|
2015-12-27 06:34:17 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
if (this._id <= instances) {
|
2015-12-27 06:34:17 +00:00
|
|
|
throw new Error('Socket id ' + this._id + 'already in use');
|
|
|
|
}
|
2015-12-23 23:14:41 +00:00
|
|
|
} else {
|
2015-12-22 23:53:08 +00:00
|
|
|
// javascript generated sockets range from 1-1000
|
2015-12-28 19:12:21 +00:00
|
|
|
this._id = instances++;
|
2015-12-22 23:53:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 17:44:16 +00:00
|
|
|
// ensure compatibility with node's EventEmitter
|
|
|
|
if (!this.on) {
|
|
|
|
this.on = this.addListener.bind(this);
|
|
|
|
}
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
// these will be set once there is a connection
|
|
|
|
this.writable = this.readable = false;
|
|
|
|
|
2015-12-27 06:34:17 +00:00
|
|
|
this._state = STATE.DISCONNECTED;
|
2015-12-04 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inherits(TcpSocket, EventEmitter);
|
|
|
|
|
|
|
|
TcpSocket.prototype._debug = function() {
|
|
|
|
if (__DEV__) {
|
|
|
|
var args = [].slice.call(arguments);
|
|
|
|
args.unshift('socket-' + this._id);
|
|
|
|
console.log.apply(console, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
// TODO : determine how to properly overload this with flow
|
|
|
|
TcpSocket.prototype.connect = function(options, callback) : TcpSocket {
|
|
|
|
this._registerEvents();
|
|
|
|
|
|
|
|
if (options === null || typeof options !== 'object') {
|
|
|
|
// Old API:
|
|
|
|
// connect(port, [host], [cb])
|
|
|
|
var args = this._normalizeConnectArgs(arguments);
|
|
|
|
return TcpSocket.prototype.connect.apply(this, args);
|
2015-12-04 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
if (typeof callback === 'function') {
|
2015-12-22 23:53:08 +00:00
|
|
|
this.once('connect', callback);
|
2015-12-15 00:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var host = options.host || 'localhost';
|
2015-12-24 19:18:51 +00:00
|
|
|
var port = options.port || 0;
|
2015-12-15 00:16:45 +00:00
|
|
|
var localAddress = options.localAddress;
|
|
|
|
var localPort = options.localPort;
|
|
|
|
|
|
|
|
if (localAddress && !ipRegex({exact: true}).test(localAddress)) {
|
|
|
|
throw new TypeError('"localAddress" option must be a valid IP: ' + localAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localPort && typeof localPort !== 'number') {
|
|
|
|
throw new TypeError('"localPort" option should be a number: ' + localPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof port !== 'undefined') {
|
|
|
|
if (typeof port !== 'number' && typeof port !== 'string') {
|
|
|
|
throw new TypeError('"port" option should be a number or string: ' + port);
|
|
|
|
}
|
2015-12-24 19:18:51 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
port = +port;
|
2015-12-24 19:18:51 +00:00
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
if (!isLegalPort(port)) {
|
|
|
|
throw new RangeError('"port" option should be >= 0 and < 65536: ' + port);
|
|
|
|
}
|
2015-12-04 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._state = STATE.CONNECTING;
|
|
|
|
this._debug('connecting, host:', host, 'port:', port);
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
this._destroyed = false;
|
2015-12-15 00:16:45 +00:00
|
|
|
Sockets.connect(this._id, host, Number(port), options);
|
2015-12-28 19:12:21 +00:00
|
|
|
|
|
|
|
return this;
|
2015-12-04 17:44:16 +00:00
|
|
|
};
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
// Check that the port number is not NaN when coerced to a number,
|
|
|
|
// is an integer and that it falls within the legal range of port numbers.
|
2015-12-23 18:56:33 +00:00
|
|
|
function isLegalPort(port: number) : boolean {
|
2015-12-15 00:16:45 +00:00
|
|
|
if (typeof port === 'string' && port.trim() === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2015-12-23 18:56:33 +00:00
|
|
|
TcpSocket.prototype.setTimeout = function(msecs: number, callback: () => void) {
|
2015-12-17 16:10:00 +00:00
|
|
|
var self = this;
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msecs > 0) {
|
|
|
|
if (callback) {
|
|
|
|
this.once('timeout', callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
2015-12-23 18:56:33 +00:00
|
|
|
this._timeout = setTimeout(function() {
|
2015-12-15 00:16:45 +00:00
|
|
|
self.emit('timeout');
|
2015-12-17 16:10:00 +00:00
|
|
|
self._timeout = null;
|
|
|
|
self.destroy();
|
2015-12-23 18:56:33 +00:00
|
|
|
}, msecs);
|
2015-12-15 00:16:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-23 23:14:41 +00:00
|
|
|
TcpSocket.prototype.address = function() : { port: number, address: string, family: string } {
|
2015-12-24 19:18:51 +00:00
|
|
|
return this._address;
|
2015-12-15 00:16:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TcpSocket.prototype.end = function(data, encoding) {
|
2015-12-04 17:44:16 +00:00
|
|
|
if (this._destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
if (data) {
|
|
|
|
this.write(data, encoding);
|
|
|
|
}
|
|
|
|
|
2015-12-04 17:44:16 +00:00
|
|
|
this._destroyed = true;
|
2015-12-31 19:02:57 +00:00
|
|
|
this._debug('ending');
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-31 19:02:57 +00:00
|
|
|
Sockets.end(this._id);
|
2015-12-04 17:44:16 +00:00
|
|
|
};
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
TcpSocket.prototype.destroy = function() {
|
2015-12-17 16:10:00 +00:00
|
|
|
if (!this._destroyed) {
|
|
|
|
this._destroyed = true;
|
|
|
|
this._debug('destroying');
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-31 19:02:57 +00:00
|
|
|
Sockets.destroy(this._id);
|
2015-12-17 16:10:00 +00:00
|
|
|
}
|
2015-12-15 00:16:45 +00:00
|
|
|
};
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-24 19:18:51 +00:00
|
|
|
TcpSocket.prototype._registerEvents = function(): void {
|
2015-12-28 19:12:21 +00:00
|
|
|
if (this._subs && this._subs.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-24 19:18:51 +00:00
|
|
|
this._subs = [
|
|
|
|
DeviceEventEmitter.addListener(
|
|
|
|
'tcp-' + this._id + '-connect', this._onConnect.bind(this)
|
|
|
|
),
|
|
|
|
DeviceEventEmitter.addListener(
|
|
|
|
'tcp-' + this._id + '-connection', this._onConnection.bind(this)
|
|
|
|
),
|
|
|
|
DeviceEventEmitter.addListener(
|
|
|
|
'tcp-' + this._id + '-data', this._onData.bind(this)
|
|
|
|
),
|
|
|
|
DeviceEventEmitter.addListener(
|
|
|
|
'tcp-' + this._id + '-close', this._onClose.bind(this)
|
|
|
|
),
|
|
|
|
DeviceEventEmitter.addListener(
|
|
|
|
'tcp-' + this._id + '-error', this._onError.bind(this)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
};
|
2015-12-15 00:16:45 +00:00
|
|
|
|
2015-12-24 19:18:51 +00:00
|
|
|
TcpSocket.prototype._unregisterEvents = function(): void {
|
|
|
|
this._subs.forEach(function(listener) {
|
|
|
|
listener.remove();
|
|
|
|
});
|
|
|
|
this._subs = [];
|
|
|
|
};
|
2015-12-23 23:14:41 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
TcpSocket.prototype._onConnect = function(address: { port: number, address: string, family: string }): void {
|
2015-12-24 19:18:51 +00:00
|
|
|
this._debug('received', 'connect');
|
2015-12-15 00:16:45 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
setConnected(this, address);
|
2015-12-24 19:18:51 +00:00
|
|
|
this.emit('connect');
|
|
|
|
};
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
TcpSocket.prototype._onConnection = function(info: { id: number, address: { port: number, address: string, family: string } }): void {
|
2015-12-24 19:18:51 +00:00
|
|
|
this._debug('received', 'connection');
|
|
|
|
|
2015-12-27 06:34:17 +00:00
|
|
|
var socket = new TcpSocket({ id: info.id });
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
socket._registerEvents();
|
|
|
|
setConnected(socket, info.address);
|
2015-12-24 19:18:51 +00:00
|
|
|
this.emit('connection', socket);
|
|
|
|
};
|
|
|
|
|
|
|
|
TcpSocket.prototype._onData = function(data: string): void {
|
|
|
|
this._debug('received', 'data');
|
|
|
|
|
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = null;
|
2015-12-15 00:16:45 +00:00
|
|
|
}
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-24 19:18:51 +00:00
|
|
|
// from base64 string
|
|
|
|
var buffer = typeof Buffer === 'undefined'
|
|
|
|
? base64.toByteArray(data)
|
|
|
|
: new global.Buffer(data, 'base64');
|
|
|
|
|
|
|
|
this.emit('data', buffer);
|
|
|
|
};
|
|
|
|
|
|
|
|
TcpSocket.prototype._onClose = function(hadError: boolean): void {
|
|
|
|
this._debug('received', 'close');
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
setDisconnected(this, hadError);
|
2015-12-24 19:18:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TcpSocket.prototype._onError = function(error: string): void {
|
|
|
|
this._debug('received', 'error');
|
|
|
|
|
|
|
|
this.emit('error', normalizeError(error));
|
2015-12-28 19:12:21 +00:00
|
|
|
this.destroy();
|
2015-12-04 17:44:16 +00:00
|
|
|
};
|
|
|
|
|
2015-12-23 23:29:15 +00:00
|
|
|
TcpSocket.prototype.write = function(buffer: any, callback: ?(err: ?Error) => void) : boolean {
|
2015-12-04 17:44:16 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (this._state === STATE.DISCONNECTED) {
|
|
|
|
throw new Error('Socket is not connected.');
|
|
|
|
} else if (this._state === STATE.CONNECTING) {
|
|
|
|
// we're ok, GCDAsyncSocket handles queueing internally
|
|
|
|
}
|
|
|
|
|
2015-12-23 23:29:15 +00:00
|
|
|
var cb = callback || noop;
|
2015-12-04 17:44:16 +00:00
|
|
|
var str;
|
|
|
|
if (typeof buffer === 'string') {
|
2015-12-23 23:29:15 +00:00
|
|
|
self._debug('socket.WRITE(): encoding as base64');
|
2015-12-17 16:10:00 +00:00
|
|
|
str = Base64Str.encode(buffer);
|
2015-12-04 17:44:16 +00:00
|
|
|
} else if (typeof Buffer !== 'undefined' && global.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');
|
|
|
|
}
|
|
|
|
|
2015-12-23 23:29:15 +00:00
|
|
|
Sockets.write(this._id, str, function(err) {
|
2015-12-17 16:10:00 +00:00
|
|
|
if (self._timeout) {
|
|
|
|
clearTimeout(self._timeout);
|
|
|
|
self._timeout = null;
|
|
|
|
}
|
|
|
|
|
2015-12-04 17:44:16 +00:00
|
|
|
err = normalizeError(err);
|
|
|
|
if (err) {
|
2015-12-17 16:10:00 +00:00
|
|
|
self._debug('write failed', err);
|
2015-12-23 23:29:15 +00:00
|
|
|
return cb(err);
|
2015-12-04 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 23:29:15 +00:00
|
|
|
cb();
|
2015-12-04 17:44:16 +00:00
|
|
|
});
|
2015-12-23 23:29:15 +00:00
|
|
|
|
|
|
|
return true;
|
2015-12-04 17:44:16 +00:00
|
|
|
};
|
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
function setConnected(socket: TcpSocket, address: { port: number, address: string, family: string } ) {
|
|
|
|
socket.writable = socket.readable = true;
|
|
|
|
socket._state = STATE.CONNECTED;
|
|
|
|
socket._address = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDisconnected(socket: TcpSocket, hadError: boolean): void {
|
|
|
|
if (socket._state === STATE.DISCONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket._unregisterEvents();
|
|
|
|
socket._state = STATE.DISCONNECTED;
|
|
|
|
socket.emit('close', hadError);
|
|
|
|
}
|
|
|
|
|
2015-12-23 18:56:33 +00:00
|
|
|
function normalizeError(err) {
|
2015-12-04 17:44:16 +00:00
|
|
|
if (err) {
|
|
|
|
if (typeof err === 'string') {
|
|
|
|
err = new Error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 23:53:08 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
// Returns an array [options] or [options, cb]
|
|
|
|
// It is the same as the argument of Socket.prototype.connect().
|
|
|
|
TcpSocket.prototype._normalizeConnectArgs = function(args) {
|
|
|
|
var options = {};
|
|
|
|
|
|
|
|
if (args[0] !== null && typeof args[0] === 'object') {
|
|
|
|
// connect(options, [cb])
|
|
|
|
options = args[0];
|
|
|
|
} else {
|
|
|
|
// connect(port, [host], [cb])
|
|
|
|
options.port = args[0];
|
|
|
|
if (typeof args[1] === 'string') {
|
|
|
|
options.host = args[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cb = args[args.length - 1];
|
|
|
|
return typeof cb === 'function' ? [options, cb] : [options];
|
|
|
|
};
|
|
|
|
|
2015-12-31 21:40:43 +00:00
|
|
|
// unimplemented net.Socket apis
|
|
|
|
TcpSocket.prototype.pause =
|
|
|
|
TcpSocket.prototype.resume =
|
|
|
|
TcpSocket.prototype.ref =
|
|
|
|
TcpSocket.prototype.unref =
|
|
|
|
TcpSocket.prototype.setNoDelay =
|
|
|
|
TcpSocket.prototype.setKeepAlive =
|
|
|
|
TcpSocket.prototype.setEncoding = function() { /* nop */ };
|
|
|
|
|
2015-12-23 18:56:33 +00:00
|
|
|
module.exports = TcpSocket;
|