node-insim
Version:
An InSim library for NodeJS with TypeScript support
106 lines (105 loc) • 4.07 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TCP = void 0;
var net_1 = __importDefault(require("net"));
var lfspack_1 = require("../lfspack");
var log_1 = require("../log");
var Protocol_1 = require("./Protocol");
var log = log_1.log.extend('tcp');
/** @internal */
var TCP = /** @class */ (function (_super) {
__extends(TCP, _super);
function TCP(host, port, packetSizeMultiplier) {
if (packetSizeMultiplier === void 0) { packetSizeMultiplier = 1; }
var _this = _super.call(this, { host: host, port: port, packetSizeMultiplier: packetSizeMultiplier }) || this;
_this.stream = null;
_this.tempBuf = null;
_this.connect = function () {
_this.stream = net_1.default.connect(_this.port, _this.host);
_this.stream.on('connect', function () {
_this.emit('connect');
});
_this.stream.on('close', function () {
log('Disconnected');
_this.emit('disconnect');
});
_this.stream.on('error', function (error) {
_this.emit('error', error);
});
_this.stream.on('data', function (data) {
log('Data received:', data.join());
// Set or append to temp buffer
if (_this.tempBuf === null) {
_this.tempBuf = data;
}
else {
_this.tempBuf = Buffer.concat([_this.tempBuf, data]);
}
_this.processBuf();
});
};
_this.send = function (data) {
if (_this.stream === null) {
log('Cannot send data - not connected');
return;
}
log('Send data:', data.join());
_this.stream.write(data);
};
_this.disconnect = function () {
if (_this.stream === null) {
log('Cannot disconnect - not connected');
return;
}
_this.stream.end();
};
return _this;
}
TCP.prototype.processBuf = function () {
if (this.tempBuf === null) {
log('No buffer to process');
return;
}
// Haven't got a full 32 bit header
if (this.tempBuf.length < 4) {
log('Got packet with <4 bytes');
return;
}
var size = this.tempBuf[0] * this.packetSizeMultiplier;
if (this.tempBuf.length === size) {
// We have at least one full packet
this.emit('data', (0, lfspack_1.copyBuffer)(this.tempBuf));
this.tempBuf = null;
}
else if (this.tempBuf.length > size) {
// Process first packet...
this.emit('data', (0, lfspack_1.copyBuffer)(this.tempBuf.slice(0, size)));
this.tempBuf = this.tempBuf.slice(size, this.tempBuf.length);
// Recurse on remaining buffer
this.processBuf();
}
else {
log('Got incomplete packet');
}
};
return TCP;
}(Protocol_1.Protocol));
exports.TCP = TCP;