UNPKG

@foxglove/ros1

Version:

Standalone TypeScript implementation of the ROS 1 (Robot Operating System) protocol with a pluggable transport layer

84 lines 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TcpSocketNode = void 0; const eventemitter3_1 = __importDefault(require("eventemitter3")); const net_1 = __importDefault(require("net")); class TcpSocketNode extends eventemitter3_1.default { constructor(host, port, socket) { super(); this._host = host; this._port = port; this._socket = socket; socket.on("connect", () => this.emit("connect")); socket.on("close", () => this.emit("close")); socket.on("data", (chunk) => this.emit("data", chunk)); socket.on("end", () => this.emit("end")); socket.on("timeout", () => this.emit("timeout")); socket.on("error", (err) => this.emit("error", err)); } async remoteAddress() { return { port: this._port, family: this._socket.remoteFamily, address: this._host, }; } async localAddress() { if (this._socket.destroyed) { return undefined; } const port = this._socket.localPort; const family = this._socket.remoteFamily; // There is no localFamily const address = this._socket.localAddress; return port != undefined && family != undefined && address != undefined ? { port, family, address } : undefined; } async fd() { // There is no public node.js API for retrieving the file descriptor for a // socket. This is the only way of retrieving it from pure JS, on platforms // where sockets have file descriptors. See // <https://github.com/nodejs/help/issues/1312> // eslint-disable-next-line no-underscore-dangle return this._socket._handle?.fd; } async connected() { return !this._socket.destroyed && this._socket.localAddress != undefined; } async connect() { await new Promise((resolve, reject) => { const KEEPALIVE_MS = 60 * 1000; this._socket.on("error", reject).connect(this._port, this._host, () => { this._socket.removeListener("error", reject); this._socket.setKeepAlive(true, KEEPALIVE_MS); resolve(); }); }); } async close() { this._socket.destroy(); } async write(data) { await new Promise((resolve, reject) => { this._socket.write(data, (err) => { if (err != undefined) { reject(err); return; } resolve(); }); }); } // eslint-disable-next-line @foxglove/no-boolean-parameters async setNoDelay(noDelay) { this._socket.setNoDelay(noDelay); } static async Create({ host, port }) { return new TcpSocketNode(host, port, new net_1.default.Socket()); } } exports.TcpSocketNode = TcpSocketNode; //# sourceMappingURL=TcpSocketNode.js.map