Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for localAddress option of net.Socket #141

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = undefined
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 | undefined
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