Skip to content

Commit

Permalink
Added support for localAddress option of net.Socket
Browse files Browse the repository at this point in the history
  • Loading branch information
peschuster committed Sep 2, 2019
1 parent 15f26b3 commit 8f0ca08
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/CasparCG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions src/lib/AMCPConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export namespace Options {
export interface IConnectionOptions {
host?: string
port?: number
localAddress?: string
autoConnect?: boolean
autoReconnect?: boolean
autoReconnectInterval?: number
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/lib/CasparCGSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 8f0ca08

Please sign in to comment.