@qnext/iso-on-tcp
Version:
ISO-on-TCP Protocol implementation
93 lines (82 loc) • 3.39 kB
TypeScript
/// <reference types="node" />
import { Duplex } from 'stream';
export type ISOOnTCPOptions = Partial<{
host: string;
port: number;
tpduSize: string;
srcTSAP: number | string;
dstTSAP: number | string;
sourceRef: number;
forceClose: boolean;
validateConnection: (msg: object) => boolean;
}>;
/**
* Creates a TCP socket using
* {@linkcode https://nodejs.org/api/net.html#net_net_createconnection_port_host_connectlistener net.createConnection()}
* and passes it to a new instance of {@link ISOOnTCPClient}, calling
* {@link ISOOnTCPClient~connect} when the TCP socket emits the
* connect event. The <code>handleStreamEvents</code> option is set on the created client
*
* @param {object} opts options to the constructor
* @param {number} [opts.port=102] the destination TCP port it should connect to
* @param {string} [opts.host='localhost'] the destination host it should connect to
* @param {number} [opts.tpduSize=1024] the tpdu size. Must be a power of 2
* @param {number} [opts.srcTSAP=0] the source TSAP
* @param {number} [opts.dstTSAP=0] the destination TSAP
* @param {number} [opts.sourceRef] our reference. If not provided, an random one is generated
* @param {boolean} [opts.forceClose=false] skip sending Disconnect Requests on disconnecting, and forcibly closes the connection instead
* @param {function} [cb] an optional callback that will be added to the 'connect' event of the returned instance of {@link ISOOnTCPClient}
* @returns {ISOOnTCPClient}
*/
export declare function createConnection(
opts?: ISOOnTCPOptions,
cb?: Function
): ISOOnTCPClient;
/**
* Duplex stream that handles the lifecycle of an ISO-on-TCP connection
* as a client.
*
* @class
*/
export declare class ISOOnTCPClient extends Duplex {
/**
*
* @param {Duplex} stream the underlying stream used to
* @param {object} [opts] options to the constructor
* @param {number} [opts.tpduSize=1024] the tpdu size. Must be a power of 2
* @param {number|string} [opts.srcTSAP=0] the source TSAP
* @param {number|string} [opts.dstTSAP=0] the destination TSAP
* @param {number} [opts.sourceRef=random] our reference. If not provided, an random one is generated
* @param {boolean} [opts.forceClose=false] skip sending Disconnect Requests on disconnecting, and forcibly closes the connection instead
* @param {(msg: object) => boolean} [opts.validateConnection] a function that will be called to validate the connection parameters.
*/
constructor(stream: Duplex, opts?: ISOOnTCPOptions);
/**
* our reference code
*/
get sourceReference(): number;
/**
* the destination reference if we're connected, null otherwhise
*/
get destinationReference(): number | null;
/**
* the negotiated TPDU size, being the smallest of ours and theirs TPDU size
*/
get negotiatedTpduSize(): number;
/**
* whether we're currently connected or not
*/
get isConnected(): boolean;
/**
* Initiates the connection process
*
* @param {function} [cb] a callback that is added to the {@link ISOOnTCPClient#connect} event
* @throws an error if the client is not in a disconnected state
*/
connect(cb?: Function): void;
/**
* Closes the connection by sending a DR telegram. If forceClose was set
* to true, DR is not sent and the connection is abruptly disconnected instead
*/
close(): void;
}