constructor(frontend) { process.title = 'ndb/ndd_service'; this._disconnectPromise = new Promise(resolve => process.once('disconnect', () => resolve(DebugState.PROCESS_DISCONNECT))); this._connected = new Set(); this._frontend = frontend; const pipePrefix = process.platform === 'win32' ? '\\\\.\\pipe\\' : os.tmpdir(); const pipeName = `node-ndb.${process.pid}.sock`; this._pipe = path.join(pipePrefix, pipeName); const server = net.createServer(socket => { socket.on('data', async d => { const runSession = await this._startSession(JSON.parse(d), frontend); socket.write('run'); runSession(); }); socket.on('error', e => caughtErrorDebug(e)); }).listen(this._pipe); server.unref(); }
const methodCall = (connectionMethod, methodName, parameters) => new Promise((resolve, reject) => { const networkConfiguration = connectionMethod.socketPath != null ? {path: connectionMethod.socketPath} : {port: connectionMethod.port, host: connectionMethod.host}; const stream = net.connect(networkConfiguration); const xml = Serializer.serializeMethodCall(methodName, parameters); const xmlLength = Buffer.byteLength(xml, 'utf8'); stream.on('error', reject); stream.setEncoding('UTF8'); const headerItems = [`CONTENT_LENGTH${NULL_CHAR}${xmlLength}${NULL_CHAR}`, `SCGI${NULL_CHAR}1${NULL_CHAR}`]; const headerLength = headerItems.reduce((accumulator, headerItem) => accumulator + headerItem.length, 0); stream.end(`${headerLength}:${headerItems.join('')},${xml}`); bufferStream(stream) .then(data => { rTorrentDeserializer.deserialize(data, resolve, reject); }) .catch(reject); })
socket.lastUsed = Date.now(); socket.setNoDelay(true); socket.on("error", err => { this.removeSocket(nodeID); this.emit("error", err, nodeID); }); socket.on("end", () => { this.removeSocket(nodeID); this.emit("end", nodeID); }); socket.unref();
const TIMEOUT = 30000; const socket = net.createConnection(process.env.NDD_IPC, () => { socket.write(process.env.NDD_PUBLISH_DATA); const timeoutId = setTimeout(() => socket.destroy(), TIMEOUT); socket.on('data', () => { clearTimeout(timeoutId); socket.destroy(); }); }); socket.on('error', err => { process.stderr.write('\u001b[31mndb is not found:\u001b[0m\n'); process.stderr.write('please restart it and update env variables or unset NDD_IPC and NODE_OPTIONS.\n');
add(socket: net.Socket, data: Buffer) { if (this.state !== ST_STARTED) { return; } let tcpsocket = new TcpSocket(socket, { headSize: HEAD_SIZE, headHandler: utils.headHandler, closeMethod: this.closeMethod }); this.emit('connection', tcpsocket); socket.emit('data', data); }
onend(chunk: Buffer) { if (chunk) { this._socket.write(chunk); } this.state = ST_CLOSED; this.reset(); this.emit('end'); }
send(msg: any) { if (this.state !== ST_INITED) { return; } if (msg instanceof Buffer) { // if encoded, send directly this.socket.stream.write(msg); } else { this.adaptor.publish(this, msg); } }
/** * Sends a heartbeat ping to the device * @private */ async _sendPing() { debug(`Pinging ${this.device.ip}`); // Create byte buffer const buffer = this.device.parser.encode({ data: Buffer.allocUnsafe(0), commandByte: CommandType.HEART_BEAT, sequenceN: ++this._currentSequenceN }); // Send ping await this.client.write(buffer); }
add(socket: net.Socket, data: Buffer) { if (this.state !== ST_STARTED) { return; } this.httpServer.emit('connection', socket); if (typeof (socket as any).ondata === 'function') { // compatible with stream2 (socket as any).ondata(data, 0, data.length); } else { // compatible with old stream socket.emit('data', data); } }
/** * Disconnects from the device, use to * close the socket and exit gracefully. */ disconnect() { debug('Disconnect'); this._connected = false; // Clear timeouts clearTimeout(this._sendTimeout); clearTimeout(this._connectTimeout); clearTimeout(this._responseTimeout); clearTimeout(this.pingpongTimeout); if (!this.client) { return; } this.client.destroy(); }
send(msg: any, options: { binary?: boolean }, cb?: (err ?: Error) => void) { this._socket.write(msg, options as string, cb); }
disconnect() { if (this.state === ST_CLOSED) { return; } this.state = ST_CLOSED; this.socket.stream.destroy(); }