node-insim
Version:
An InSim library for NodeJS with TypeScript support
139 lines (138 loc) • 5.67 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UDP = void 0;
var dgram = __importStar(require("dgram"));
var log_1 = require("../log");
var Protocol_1 = require("./Protocol");
var log = log_1.log.extend('udp');
/** @internal */
var UDP = /** @class */ (function (_super) {
__extends(UDP, _super);
function UDP(_a) {
var host = _a.host, port = _a.port, socketInitialisationMode = _a.socketInitialisationMode, _b = _a.packetSizeMultiplier, packetSizeMultiplier = _b === void 0 ? 1 : _b, _c = _a.timeout, timeout = _c === void 0 ? 0 : _c;
var _this = _super.call(this, { host: host, port: port, packetSizeMultiplier: packetSizeMultiplier }) || this;
_this.socket = null;
_this.timeout = 0;
_this.timeoutTimer = null;
_this.connect = function () {
_this.socket = dgram.createSocket('udp4');
log("Connecting to ".concat(_this.host, ":").concat(_this.port, "..."));
if (_this.socketInitialisationMode === 'bind') {
_this.socket.bind({
address: _this.host,
port: _this.port,
});
_this.socket.on('listening', function () {
log('Listening');
_this.emit('connect');
});
}
else if (_this.socketInitialisationMode === 'connect') {
_this.socket.connect(_this.port, _this.host);
_this.socket.on('connect', function () {
log('Connected');
_this.emit('connect');
});
}
if (_this.timeout > 0) {
_this.timeoutTimer = setTimeout(_this.handleTimeout, _this.timeout);
}
_this.socket.on('close', function () {
log('Connection closed');
_this.emit('disconnect');
});
_this.socket.on('error', function (error) {
log('Error', error);
_this.emit('error', error);
});
_this.socket.on('message', function (data) {
log('Data received:', data.join());
_this.emit('data', data);
if (_this.timeoutTimer) {
clearTimeout(_this.timeoutTimer);
}
if (_this.timeout) {
_this.timeoutTimer = setTimeout(_this.handleTimeout, _this.timeout);
}
});
};
_this.disconnect = function () {
if (_this.socket === null) {
log('Cannot disconnect - not connected');
return;
}
_this.socket.close();
if (_this.timeoutTimer) {
clearTimeout(_this.timeoutTimer);
_this.timeoutTimer = null;
}
};
_this.send = function (data) {
if (_this.socket === null) {
log('Cannot send - not connected');
return;
}
log('Send data:', data.join());
_this.socket.send(data);
};
_this.handleTimeout = function () {
log('Connection timed out');
_this.emit('timeout');
_this.timeoutTimer = null;
_this.disconnect();
};
_this.timeout = timeout;
_this.socketInitialisationMode = socketInitialisationMode;
return _this;
}
return UDP;
}(Protocol_1.Protocol));
exports.UDP = UDP;