From 8f0ca0808ac037a540a55b1c4af537c63a93d459 Mon Sep 17 00:00:00 2001 From: Peter Schuster Date: Sun, 19 May 2019 00:28:47 +0200 Subject: [PATCH 1/2] Added support for localAddress option of net.Socket --- src/CasparCG.ts | 26 +++++++++++++++++++++++++- src/lib/AMCPConnectionOptions.ts | 2 ++ src/lib/CasparCGSocket.ts | 6 ++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/CasparCG.ts b/src/CasparCG.ts index cf01df99..e98b5613 100644 --- a/src/CasparCG.ts +++ b/src/CasparCG.ts @@ -331,6 +331,7 @@ export class CasparCG extends EventEmitter implements ICasparCGConnection, Conne private _connected: boolean = false private _host: string private _port: number + private _localAddress: string private _autoReconnect: boolean private _autoReconnectInterval: number private _autoReconnectAttempts: number @@ -519,6 +520,29 @@ export class CasparCG extends EventEmitter implements ICasparCGConnection, Conne } } + /** + * + */ + public get localAddress(): string { + return this._localAddress + } + + /** + * Setting the `localAddress` will create a new [[CasparCGSocket]] connection. + * + * The new `CasparCGSocket` will `autoConnect` if the old socket was either successfully connected, or currently reconnecting. Changing the host resets the number of [[CasparCG.autoReconnectAttempts]]. + */ + public set localAddress(localAddress: string) { + if (this._localAddress !== localAddress) { + this._localAddress = localAddress + let shouldReconnect = this.connected + this._createNewSocket() + if (shouldReconnect) { + this.connect() + } + } + } + /** * Try to reconnect in case of unintentionally loss of connection, or in case of failed connection in the first place. */ @@ -2047,7 +2071,7 @@ export class CasparCG extends EventEmitter implements ICasparCGConnection, Conne this._socket.dispose() delete this._socket } - this._socket = new CasparCGSocket(this.host, this.port, this.autoReconnect, this.autoReconnectInterval, this.autoReconnectAttempts, this.queueMode) + this._socket = new CasparCGSocket(this.host, this.port, this.localAddress, this.autoReconnect, this.autoReconnectInterval, this.autoReconnectAttempts, this.queueMode) this._socket.on('error', (error: Error) => this._onSocketError(error)) this._socket.on(CasparCGSocketStatusEvent.STATUS, (event: CasparCGSocketStatusEvent) => this._onSocketStatusChange(event)) this._socket.on(CasparCGSocketStatusEvent.TIMEOUT, () => this._onSocketStatusTimeout()) diff --git a/src/lib/AMCPConnectionOptions.ts b/src/lib/AMCPConnectionOptions.ts index 8aecf7f8..9ffe79d5 100644 --- a/src/lib/AMCPConnectionOptions.ts +++ b/src/lib/AMCPConnectionOptions.ts @@ -36,6 +36,7 @@ export namespace Options { export interface IConnectionOptions { host?: string port?: number + localAddress?: string autoConnect?: boolean autoReconnect?: boolean autoReconnectInterval?: number @@ -58,6 +59,7 @@ export interface IConnectionOptions { export class ConnectionOptions implements IConnectionOptions { public host: string | undefined = 'localhost' public port: number | undefined = 5250 + public localAddress: string | undefined = '0.0.0.0' public autoConnect: boolean | undefined = true public autoReconnect: boolean | undefined = true public autoReconnectInterval: number | undefined = 1000 diff --git a/src/lib/CasparCGSocket.ts b/src/lib/CasparCGSocket.ts index efb67014..a996deb5 100644 --- a/src/lib/CasparCGSocket.ts +++ b/src/lib/CasparCGSocket.ts @@ -35,6 +35,7 @@ export class CasparCGSocket extends EventEmitter implements ICasparCGSocket { private _client: net.Socket private _host: string private _port: number + private _localAddress: string private _connected: boolean private _autoReconnect: boolean private _reconnectDelay: number @@ -50,10 +51,11 @@ export class CasparCGSocket extends EventEmitter implements ICasparCGSocket { /** * */ - public constructor (host: string, port: number, autoReconnect: boolean, autoReconnectInterval: number, autoReconnectAttempts: number, queueMode?: OptionsNS.QueueMode) { + public constructor (host: string, port: number, localAddress: string, autoReconnect: boolean, autoReconnectInterval: number, autoReconnectAttempts: number, queueMode?: OptionsNS.QueueMode) { super() this._host = host this._port = port + this._localAddress = localAddress; this._reconnectDelay = autoReconnectInterval this._autoReconnect = autoReconnect this._reconnectAttempts = autoReconnectAttempts @@ -107,7 +109,7 @@ export class CasparCGSocket extends EventEmitter implements ICasparCGSocket { // connects this.log('Socket attempting connection') - this._client.connect(this._port, this._host) + this._client.connect({ port: this._port, host: this._host, localAddress: this._localAddress }) this._shouldBeConnected = true this._lastConnectionAttempt = Date.now() } From 5347704aa25aae5f37b90eb3bf5c4936afef19cc Mon Sep 17 00:00:00 2001 From: Peter Schuster Date: Fri, 20 Sep 2019 13:17:18 +0200 Subject: [PATCH 2/2] Default value for localAddress should be undefined --- src/lib/AMCPConnectionOptions.ts | 2 +- src/lib/CasparCGSocket.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/AMCPConnectionOptions.ts b/src/lib/AMCPConnectionOptions.ts index 9ffe79d5..46687dc9 100644 --- a/src/lib/AMCPConnectionOptions.ts +++ b/src/lib/AMCPConnectionOptions.ts @@ -59,7 +59,7 @@ export interface IConnectionOptions { export class ConnectionOptions implements IConnectionOptions { public host: string | undefined = 'localhost' public port: number | undefined = 5250 - public localAddress: string | undefined = '0.0.0.0' + public localAddress: string | undefined = undefined public autoConnect: boolean | undefined = true public autoReconnect: boolean | undefined = true public autoReconnectInterval: number | undefined = 1000 diff --git a/src/lib/CasparCGSocket.ts b/src/lib/CasparCGSocket.ts index a996deb5..b96f6296 100644 --- a/src/lib/CasparCGSocket.ts +++ b/src/lib/CasparCGSocket.ts @@ -35,7 +35,7 @@ export class CasparCGSocket extends EventEmitter implements ICasparCGSocket { private _client: net.Socket private _host: string private _port: number - private _localAddress: string + private _localAddress: string | undefined private _connected: boolean private _autoReconnect: boolean private _reconnectDelay: number @@ -55,7 +55,7 @@ export class CasparCGSocket extends EventEmitter implements ICasparCGSocket { super() this._host = host this._port = port - this._localAddress = localAddress; + this._localAddress = localAddress this._reconnectDelay = autoReconnectInterval this._autoReconnect = autoReconnect this._reconnectAttempts = autoReconnectAttempts