import { Socket } from "net" ; import Adapter from "../Adapter" ; export interface IEndpoint { address : string ; port : number ; } export default class Network extends Adapter { private retrying : boolean ; private options : IEndpoint ; private device : Socket ; private retries : number ; private connected : boolean ; constructor ( address , port = 9100 , retries = 0 ) { super ( ) ; this . device = new Socket ( ) ; this . retrying = false ; this . retries = 0 ; this . options = { address , port } ; this . device . on ( "close" , ( ) => { this . connected = false ; if ( this . retrying && ( retries === 0 || this . retries < retries ) ) { this . retries ++ ; setTimeout ( ( ) => { this . device . connect ( this . options . port , this . options . address ) ; } , 5000 ) ; } else { this . retrying = false ; throw new Error ( `Cannot connect to ${ this . options . address }:${ this . options . port }` ) ; } } ) ; this . device . on ( "error" , ( ) => this . connected = false ) ; } public async open ( ) { return new Promise < void > ( resolve => { this . retrying = true ; this . device . connect ( this . options . port , this . options . address ) ; this . device . on ( "connect" , ( ) => { this . retrying = false ; this . connected = true ; resolve ( ) ; } ) ; } ) ; } public async write ( data ) { return new Promise < void > ( resolve => { this . throwIfNeeded ( ) ; this . device . write ( new Buffer ( data ) , resolve ) ; } ) ; } public async close ( ) { this . throwIfNeeded ( ) ; this . retrying = false ; this . connected = false ; this . device . destroy ( ) ; } private throwIfNeeded ( reason ? ) { if ( ! this . device || ! this . connected ) { throw new Error ( reason || "The network socket is not open" ) ; } } }