UNPKG

int-cli

Version:

INT is the new generation of bottom-up created system of IoT and blockchain

79 lines (78 loc) 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const error_code_1 = require("../error_code"); const net_1 = require("../net"); class TcpConnection extends net_1.IConnection { constructor(options) { super(); this.m_nTimeDelta = 0; this.m_socket = options.socket; this.m_socket.on('drain', () => { this.m_pending = false; this.emit('drain'); }); this.m_socket.on('data', (data) => { this.emit('data', [data]); }); this.m_socket.on('error', (err) => { this.emit('error', this, error_code_1.ErrorCode.RESULT_EXCEPTION); }); this.m_pending = false; this.m_remote = options.remote; } send(data) { if (this.m_pending) { return 0; } else { this.m_pending = !this.m_socket.write(data); return data.length; } } close() { if (this.m_socket) { this.m_socket.removeAllListeners('drain'); this.m_socket.removeAllListeners('data'); this.m_socket.removeAllListeners('error'); this.m_socket.once('error', () => { // do nothing }); this.m_socket.end(); delete this.m_socket; } this.emit('close', this); return Promise.resolve(error_code_1.ErrorCode.RESULT_OK); } destroy() { if (this.m_socket) { this.m_socket.removeAllListeners('drain'); this.m_socket.removeAllListeners('data'); this.m_socket.removeAllListeners('error'); this.m_socket.once('error', () => { // do nothing }); this.m_socket.destroy(); delete this.m_socket; } return Promise.resolve(); } get remote() { return this.m_remote; } set remote(s) { this.m_remote = s; } get network() { return this.m_network; } set network(s) { this.m_network = s; } getTimeDelta() { return this.m_nTimeDelta; } setTimeDelta(n) { this.m_nTimeDelta = n; } } exports.TcpConnection = TcpConnection;