UNPKG

steam-condenser

Version:
158 lines 6.17 kB
"use strict"; 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { 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 }); var net_1 = __importDefault(require("net")); var Socket_1 = __importDefault(require("./Socket")); var SteamPacket_1 = __importDefault(require("./Servers/Packets/SteamPacket")); var asyncTimer_1 = require("./asyncTimer"); var TCPSocket = /** @class */ (function (_super) { __extends(TCPSocket, _super); function TCPSocket() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.buffer = Buffer.alloc(4096); _this.receivedBytes = 0; return _this; } TCPSocket.prototype.connect = function () { var _this = this; return new Promise(function (resolve, reject) { _this.socket = net_1.default.createConnection({ host: _this.ipAddress, port: _this.port, }); _this.socket.on('connect', function () { _this.open = true; resolve(); }); _this.socket.on('error', function () { reject(); }); _this.socket.on('data', function (data) { _this.buffer = Buffer.concat([_this.buffer.slice(0, _this.receivedBytes), data]); _this.receivedBytes += data.length; }); }); }; TCPSocket.prototype.close = function () { var _this = this; return new Promise(function (resolve) { if (typeof _this.socket === 'undefined') { throw new Error('Socket is undefined'); } _this.socket.on('close', function () { return resolve(); }); _this.socket.end(); }); }; TCPSocket.prototype.send = function (data) { var _this = this; if (!this.isOpen) { throw new Error('Socket not open'); } var buffer; if (data instanceof SteamPacket_1.default) buffer = data.toBuffer(); else buffer = data; return new Promise(function (resolve, reject) { if (typeof _this.socket === 'undefined') { throw new Error('Socket is undefined'); } _this.socket.write(buffer, function (err) { if (err) { reject(err); } else { resolve(); } }); }); }; TCPSocket.prototype.recvBytes = function (bytes) { var _this = this; if (bytes === void 0) { bytes = 0; } var received = Buffer.alloc(bytes); var stored = 0; return asyncTimer_1.doWithin(new Promise(function (resolve) { var dataFn = function () { if (_this.receivedBytes > 0) { if (bytes === 0) { if (typeof _this.socket !== 'undefined') { _this.socket.off('data', dataFn); } _this.receivedBytes = 0; resolve(_this.buffer); } else { var readBytes = Math.min(_this.receivedBytes, bytes - stored); _this.buffer.copy(received, stored, 0, readBytes); _this.buffer.copyWithin(0, readBytes, _this.receivedBytes); _this.receivedBytes -= readBytes; stored += readBytes; if (stored >= bytes) { if (typeof _this.socket !== 'undefined') { _this.socket.off('data', dataFn); } resolve(received); } } } }; if (typeof _this.socket === 'undefined') { throw new Error('Socket not ready'); } _this.socket.on('data', dataFn); dataFn(); }), this.timeout); }; TCPSocket.prototype.recv = function (fn) { var _this = this; var returned = false; return asyncTimer_1.doWithin(new Promise(function (resolve, reject) { if (typeof _this.socket === 'undefined') { throw new Error('Socket is undefined'); } var dataFn = function (data) { if (returned) { return; } var done = fn(data); if (done !== false) { returned = true; resolve(done); if (typeof _this.socket !== 'undefined') { _this.socket.off('data', dataFn); // eslint-disable-next-line @typescript-eslint/no-use-before-define _this.socket.off('error', errorFn); } } }; var errorFn = function (err) { reject(err); if (typeof _this.socket !== 'undefined') { _this.socket.off('data', dataFn); _this.socket.off('error', errorFn); } }; _this.socket.on('data', dataFn); _this.socket.on('error', errorFn); }), this.timeout); }; return TCPSocket; }(Socket_1.default)); exports.default = TCPSocket; //# sourceMappingURL=TCPSocket.js.map