2020-04-01 15:56:23 -04:00
|
|
|
import {AbstractCommandHandler, AbstractCommandHandlerBoss} from "tc-shared/connection/AbstractCommandHandler";
|
2019-10-25 19:51:40 -04:00
|
|
|
import {
|
2020-08-22 15:33:30 -04:00
|
|
|
AbstractServerConnection,
|
|
|
|
CommandOptionDefaults,
|
2020-12-13 08:51:10 -05:00
|
|
|
CommandOptions, ConnectionPing,
|
2020-12-02 15:00:51 -05:00
|
|
|
ConnectionStatistics,
|
2020-08-21 07:37:10 -04:00
|
|
|
ServerCommand
|
2020-04-01 15:56:23 -04:00
|
|
|
} from "tc-shared/connection/ConnectionBase";
|
|
|
|
import {CommandResult} from "tc-shared/connection/ServerConnectionDeclaration";
|
|
|
|
import {tr} from "tc-shared/i18n/localize";
|
|
|
|
import {ConnectionHandler, ConnectionState, DisconnectReason} from "tc-shared/ConnectionHandler";
|
2020-08-22 15:33:30 -04:00
|
|
|
import {
|
|
|
|
destroy_server_connection as destroy_native_server_connection,
|
|
|
|
NativeServerConnection,
|
|
|
|
ServerType,
|
|
|
|
spawn_server_connection as spawn_native_server_connection
|
|
|
|
} from "tc-native/connection";
|
2020-04-01 15:56:23 -04:00
|
|
|
import {ConnectionCommandHandler} from "tc-shared/connection/CommandHandler";
|
|
|
|
import {HandshakeHandler} from "tc-shared/connection/HandshakeHandler";
|
|
|
|
import {TeaSpeakHandshakeHandler} from "tc-shared/profiles/identities/TeamSpeakIdentity";
|
2020-09-24 16:06:52 -04:00
|
|
|
import {NativeVoiceConnectionWrapper} from "./VoiceConnection";
|
2020-08-21 07:37:10 -04:00
|
|
|
import {AbstractVoiceConnection} from "tc-shared/connection/VoiceConnection";
|
2020-08-22 15:33:30 -04:00
|
|
|
import {LogCategory, logDebug, logWarn} from "tc-shared/log";
|
|
|
|
import {ErrorCode} from "tc-shared/connection/ErrorCode";
|
2020-09-24 16:06:52 -04:00
|
|
|
import {ServerAddress} from "tc-shared/tree/Server";
|
2020-11-29 15:06:57 -05:00
|
|
|
import {VideoConnection} from "tc-shared/connection/VideoConnection";
|
|
|
|
import {RTCConnection} from "tc-shared/connection/rtc/Connection";
|
|
|
|
import {RtpVideoConnection} from "tc-shared/connection/rtc/video/Connection";
|
2020-08-22 15:33:30 -04:00
|
|
|
|
|
|
|
interface ErrorCodeListener {
|
|
|
|
callback: (result: CommandResult) => void;
|
|
|
|
code: string;
|
|
|
|
|
|
|
|
command: string;
|
|
|
|
timeout: number;
|
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
|
|
|
|
class ErrorCommandHandler extends AbstractCommandHandler {
|
2020-08-22 15:33:30 -04:00
|
|
|
private readonly handle: ServerConnection;
|
|
|
|
|
|
|
|
private errorCodeMapping: {[key: string]: ErrorCodeListener} = {};
|
|
|
|
private errorCodeHistory: ErrorCodeListener[] = [];
|
|
|
|
|
|
|
|
private errorCodeIndex = 0;
|
2020-04-01 15:56:23 -04:00
|
|
|
|
2020-06-11 07:50:37 -04:00
|
|
|
constructor(handle: ServerConnection) {
|
|
|
|
super(handle);
|
2020-08-22 15:33:30 -04:00
|
|
|
this.handle = handle;
|
|
|
|
}
|
|
|
|
|
2020-12-03 06:01:29 -05:00
|
|
|
generateReturnCode(command: string, callback: (result: CommandResult) => void, returnCode?: string) : string {
|
|
|
|
if(typeof returnCode === "undefined") {
|
|
|
|
returnCode = "rt-" + (++this.errorCodeIndex);
|
|
|
|
}
|
|
|
|
|
2020-08-22 15:33:30 -04:00
|
|
|
const listener = {
|
|
|
|
callback: callback,
|
2020-12-03 06:01:29 -05:00
|
|
|
code: returnCode,
|
2020-08-22 15:33:30 -04:00
|
|
|
timeout: 0,
|
|
|
|
command: command
|
|
|
|
} as ErrorCodeListener;
|
|
|
|
|
|
|
|
listener.timeout = setTimeout(() => {
|
|
|
|
delete this.errorCodeMapping[listener.code];
|
|
|
|
const index = this.errorCodeHistory.indexOf(listener);
|
|
|
|
if(index !== -1) {
|
|
|
|
this.errorCodeHistory.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
logWarn(LogCategory.NETWORKING, tr("Command %s timeout out."), command);
|
|
|
|
callback(new CommandResult([{ id: ErrorCode.COMMAND_TIMED_OUT, msg: "timeout" }]));
|
|
|
|
}, 5000) as any;
|
|
|
|
|
|
|
|
this.errorCodeMapping[listener.code] = listener;
|
|
|
|
this.errorCodeHistory.push(listener);
|
|
|
|
|
|
|
|
return listener.code;
|
2020-06-11 07:50:37 -04:00
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
|
2020-06-11 07:50:37 -04:00
|
|
|
handle_command(command: ServerCommand): boolean {
|
|
|
|
if(command.command === "error") {
|
|
|
|
const data = command.arguments[0];
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-22 15:33:30 -04:00
|
|
|
const returnCode = data["return_code"];
|
|
|
|
let codeListener: ErrorCodeListener;
|
|
|
|
|
|
|
|
if(!returnCode) {
|
|
|
|
const [ code ] = this.errorCodeHistory.splice(0, 1);
|
|
|
|
if(!code) {
|
|
|
|
logWarn(LogCategory.NETWORKING, tr("Received error without a return code and we're not expecting an error."));
|
|
|
|
return true;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2020-08-22 15:33:30 -04:00
|
|
|
logDebug(LogCategory.NETWORKING, tr("Received error without any error code. Using the first send command %s (%s)"), code.command, code.code);
|
2020-04-01 15:56:23 -04:00
|
|
|
|
2020-08-22 15:33:30 -04:00
|
|
|
codeListener = code;
|
2020-06-11 07:50:37 -04:00
|
|
|
} else {
|
2020-08-22 15:33:30 -04:00
|
|
|
let code = this.errorCodeMapping[returnCode];
|
|
|
|
if(!code) {
|
|
|
|
logWarn(LogCategory.NETWORKING, tr("Received error for invalid return code %s"), returnCode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = this.errorCodeHistory.indexOf(code);
|
|
|
|
if(index !== -1) this.errorCodeHistory.splice(index, 1);
|
|
|
|
|
|
|
|
codeListener = code;
|
2020-06-11 07:50:37 -04:00
|
|
|
}
|
2020-08-22 15:33:30 -04:00
|
|
|
|
|
|
|
delete this.errorCodeMapping[codeListener.code];
|
|
|
|
clearTimeout(codeListener.timeout);
|
|
|
|
|
|
|
|
codeListener.callback(new CommandResult(command.arguments));
|
2020-06-11 07:50:37 -04:00
|
|
|
return true;
|
|
|
|
} else if(command.command == "initivexpand") {
|
|
|
|
if(command.arguments[0]["teaspeak"] == true) {
|
2020-08-22 15:33:30 -04:00
|
|
|
this.handle.handshake_handler().startHandshake();
|
2021-02-15 13:13:54 -05:00
|
|
|
this.handle["serverType"] = "teaspeak";
|
2020-12-05 10:40:34 -05:00
|
|
|
} else {
|
2021-02-15 13:13:54 -05:00
|
|
|
this.handle["serverType"] = "teamspeak";
|
2020-06-11 07:50:37 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else if(command.command == "initivexpand2") {
|
|
|
|
/* its TeamSpeak or TeaSpeak with experimental 3.1 and not up2date */
|
2021-02-15 13:13:54 -05:00
|
|
|
this.handle["serverType"] = "teamspeak";
|
2020-06-11 07:50:37 -04:00
|
|
|
} else if(command.command == "initserver") {
|
2020-08-22 15:33:30 -04:00
|
|
|
/* just if clientinit error did not fired (TeamSpeak) */
|
|
|
|
while(this.errorCodeHistory.length > 0) {
|
|
|
|
const listener = this.errorCodeHistory.pop();
|
|
|
|
listener.callback(new CommandResult([{id: 0, message: ""}]));
|
|
|
|
clearTimeout(listener.timeout);
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2020-08-22 15:33:30 -04:00
|
|
|
|
2021-02-15 13:13:54 -05:00
|
|
|
if(this.handle.getServerType() === "teaspeak") {
|
2020-12-05 10:40:34 -05:00
|
|
|
this.handle.getRtcConnection().doInitialSetup();
|
|
|
|
} else {
|
|
|
|
this.handle.getRtcConnection().setNotSupported();
|
|
|
|
}
|
2020-08-22 15:33:30 -04:00
|
|
|
this.errorCodeMapping = {};
|
2020-06-11 07:50:37 -04:00
|
|
|
} else if(command.command == "notifyconnectioninforequest") {
|
2020-08-22 15:33:30 -04:00
|
|
|
this.handle.send_command("setconnectioninfo",
|
2020-06-11 07:50:37 -04:00
|
|
|
{
|
|
|
|
//TODO calculate
|
|
|
|
connection_ping: 0.0000,
|
|
|
|
connection_ping_deviation: 0.0,
|
|
|
|
|
|
|
|
connection_packets_sent_speech: 0,
|
|
|
|
connection_packets_sent_keepalive: 0,
|
|
|
|
connection_packets_sent_control: 0,
|
|
|
|
connection_bytes_sent_speech: 0,
|
|
|
|
connection_bytes_sent_keepalive: 0,
|
|
|
|
connection_bytes_sent_control: 0,
|
|
|
|
connection_packets_received_speech: 0,
|
|
|
|
connection_packets_received_keepalive: 0,
|
|
|
|
connection_packets_received_control: 0,
|
|
|
|
connection_bytes_received_speech: 0,
|
|
|
|
connection_bytes_received_keepalive: 0,
|
|
|
|
connection_bytes_received_control: 0,
|
|
|
|
connection_server2client_packetloss_speech: 0.0000,
|
|
|
|
connection_server2client_packetloss_keepalive: 0.0000,
|
|
|
|
connection_server2client_packetloss_control: 0.0000,
|
|
|
|
connection_server2client_packetloss_total: 0.0000,
|
|
|
|
connection_bandwidth_sent_last_second_speech: 0,
|
|
|
|
connection_bandwidth_sent_last_second_keepalive: 0,
|
|
|
|
connection_bandwidth_sent_last_second_control: 0,
|
|
|
|
connection_bandwidth_sent_last_minute_speech: 0,
|
|
|
|
connection_bandwidth_sent_last_minute_keepalive: 0,
|
|
|
|
connection_bandwidth_sent_last_minute_control: 0,
|
|
|
|
connection_bandwidth_received_last_second_speech: 0,
|
|
|
|
connection_bandwidth_received_last_second_keepalive: 0,
|
|
|
|
connection_bandwidth_received_last_second_control: 0,
|
|
|
|
connection_bandwidth_received_last_minute_speech: 0,
|
|
|
|
connection_bandwidth_received_last_minute_keepalive: 0,
|
|
|
|
connection_bandwidth_received_last_minute_control: 0
|
2021-04-19 14:27:12 -04:00
|
|
|
}, { process_result: false }
|
2020-06-11 07:50:37 -04:00
|
|
|
);
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2020-06-11 07:50:37 -04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
export class ServerConnection extends AbstractServerConnection {
|
2020-11-29 15:06:57 -05:00
|
|
|
private nativeHandle: NativeServerConnection;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
private readonly rtcConnection: RTCConnection;
|
|
|
|
private readonly voiceConnection: NativeVoiceConnectionWrapper;
|
|
|
|
private readonly videoConnection: VideoConnection;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
private connectTeamSpeak: boolean;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
private readonly commandHandler: NativeConnectionCommandBoss;
|
|
|
|
private readonly commandErrorHandler: ErrorCommandHandler;
|
|
|
|
private readonly defaultCommandHandler: ConnectionCommandHandler;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
private remoteAddress: ServerAddress;
|
|
|
|
private handshakeHandler: HandshakeHandler;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2021-02-15 13:13:54 -05:00
|
|
|
private serverType: "teaspeak" | "teamspeak";
|
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
constructor(props: ConnectionHandler) {
|
|
|
|
super(props);
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
this.commandHandler = new NativeConnectionCommandBoss(this);
|
|
|
|
this.commandErrorHandler = new ErrorCommandHandler(this);
|
|
|
|
this.defaultCommandHandler = new ConnectionCommandHandler(this);
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
this.rtcConnection = new RTCConnection(this, false);
|
|
|
|
this.videoConnection = new RtpVideoConnection(this.rtcConnection);
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2021-05-01 17:38:36 -04:00
|
|
|
this.commandHandler.registerHandler(this.commandErrorHandler);
|
|
|
|
this.commandHandler.registerHandler(this.defaultCommandHandler);
|
2020-11-29 15:06:57 -05:00
|
|
|
|
|
|
|
this.nativeHandle = spawn_native_server_connection();
|
|
|
|
this.nativeHandle.callback_disconnect = reason => {
|
2021-04-19 14:27:12 -04:00
|
|
|
switch (this.connectionState) {
|
|
|
|
case ConnectionState.CONNECTING:
|
|
|
|
case ConnectionState.AUTHENTICATING:
|
|
|
|
case ConnectionState.INITIALISING:
|
|
|
|
this.client.handleDisconnect(DisconnectReason.CONNECT_FAILURE, reason);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ConnectionState.CONNECTED:
|
|
|
|
this.client.handleDisconnect(DisconnectReason.CONNECTION_CLOSED, {
|
|
|
|
reason: reason
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ConnectionState.DISCONNECTING:
|
|
|
|
case ConnectionState.UNCONNECTED:
|
|
|
|
break;
|
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
};
|
2020-11-29 15:06:57 -05:00
|
|
|
this.nativeHandle.callback_command = (command, args, switches) => {
|
2020-04-01 15:56:23 -04:00
|
|
|
console.log("Received: %o %o %o", command, args, switches);
|
|
|
|
//FIXME catch error
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2021-05-01 17:38:36 -04:00
|
|
|
this.commandHandler.invokeCommand(new ServerCommand(command, args, switches));
|
2020-04-01 15:56:23 -04:00
|
|
|
};
|
2020-11-29 15:06:57 -05:00
|
|
|
|
|
|
|
this.voiceConnection = new NativeVoiceConnectionWrapper(this, this.nativeHandle._voice_connection);
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
this.command_helper.initialize();
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
native_handle() : NativeServerConnection {
|
2020-11-29 15:06:57 -05:00
|
|
|
return this.nativeHandle;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
finalize() {
|
2020-11-29 15:06:57 -05:00
|
|
|
if(this.nativeHandle) {
|
|
|
|
if(destroy_native_server_connection) {
|
|
|
|
/* currently not defined but may will be ;) */
|
|
|
|
destroy_native_server_connection(this.nativeHandle);
|
|
|
|
}
|
|
|
|
this.nativeHandle = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rtcConnection.destroy();
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
connect(address: ServerAddress, handshake: HandshakeHandler, timeout?: number): Promise<void> {
|
2020-04-17 18:05:39 -04:00
|
|
|
this.updateConnectionState(ConnectionState.CONNECTING);
|
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
this.remoteAddress = address;
|
|
|
|
this.handshakeHandler = handshake;
|
|
|
|
this.connectTeamSpeak = false;
|
2020-04-01 15:56:23 -04:00
|
|
|
handshake.setConnection(this);
|
|
|
|
handshake.initialize();
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2020-11-29 15:06:57 -05:00
|
|
|
this.nativeHandle.connect({
|
2020-04-01 15:56:23 -04:00
|
|
|
remote_host: address.host,
|
|
|
|
remote_port: address.port,
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
timeout: typeof(timeout) === "number" ? timeout : -1,
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
|
|
|
|
callback: error => {
|
|
|
|
if(error != 0) {
|
|
|
|
/* required to notify the handle, just a promise reject does not work */
|
|
|
|
this.client.handleDisconnect(DisconnectReason.CONNECT_FAILURE, error);
|
2020-04-17 18:05:39 -04:00
|
|
|
this.updateConnectionState(ConnectionState.UNCONNECTED);
|
2020-11-29 15:06:57 -05:00
|
|
|
reject(this.nativeHandle.error_message(error));
|
2020-04-01 15:56:23 -04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
2020-04-17 18:05:39 -04:00
|
|
|
this.updateConnectionState(ConnectionState.AUTHENTICATING);
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
console.log("Remote server type: %o (%s)", this.nativeHandle.server_type, ServerType[this.nativeHandle.server_type]);
|
|
|
|
if(this.nativeHandle.server_type == ServerType.TEAMSPEAK || this.connectTeamSpeak) {
|
2020-04-01 15:56:23 -04:00
|
|
|
console.log("Trying to use TeamSpeak's identity system");
|
|
|
|
this.handshake_handler().on_teamspeak();
|
|
|
|
}
|
|
|
|
},
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
identity_key: (handshake.get_identity_handler() as TeaSpeakHandshakeHandler).identity.private_key,
|
|
|
|
teamspeak: false
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
remote_address(): ServerAddress {
|
2020-11-29 15:06:57 -05:00
|
|
|
return this.remoteAddress;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
handshake_handler(): HandshakeHandler {
|
2020-11-29 15:06:57 -05:00
|
|
|
return this.handshakeHandler;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
connected(): boolean {
|
2020-11-29 15:06:57 -05:00
|
|
|
return typeof(this.nativeHandle) !== "undefined" && this.nativeHandle.connected();
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
disconnect(reason?: string): Promise<void> {
|
|
|
|
console.trace("Disconnect: %s",reason);
|
2020-11-29 15:06:57 -05:00
|
|
|
return new Promise<void>((resolve, reject) => this.nativeHandle.disconnect(reason || "", error => {
|
2020-04-01 15:56:23 -04:00
|
|
|
if(error == 0)
|
|
|
|
resolve();
|
|
|
|
else
|
2020-11-29 15:06:57 -05:00
|
|
|
reject(this.nativeHandle.error_message(error));
|
2020-04-01 15:56:23 -04:00
|
|
|
}));
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
support_voice(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-08-21 07:37:10 -04:00
|
|
|
getVoiceConnection(): AbstractVoiceConnection {
|
2020-11-29 15:06:57 -05:00
|
|
|
return this.voiceConnection;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2021-05-01 17:38:36 -04:00
|
|
|
getCommandHandler(): AbstractCommandHandlerBoss {
|
2020-11-29 15:06:57 -05:00
|
|
|
return this.commandHandler;
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
send_command(command: string, data?: any, _options?: CommandOptions): Promise<CommandResult> {
|
|
|
|
if(!this.connected()) {
|
|
|
|
console.warn(tr("Tried to send a command without a valid connection."));
|
|
|
|
return Promise.reject(tr("not connected"));
|
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
const options: CommandOptions = {};
|
|
|
|
Object.assign(options, CommandOptionDefaults);
|
|
|
|
Object.assign(options, _options);
|
|
|
|
|
2020-08-22 15:33:30 -04:00
|
|
|
data = Array.isArray(data) ? data : [data || {}];
|
|
|
|
if(data.length == 0) { /* we require min one arg to append return_code */
|
2020-04-01 15:56:23 -04:00
|
|
|
data.push({});
|
2020-08-22 15:33:30 -04:00
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
|
2020-08-22 15:33:30 -04:00
|
|
|
console.log("Send: %o %o", command, data);
|
2020-04-01 15:56:23 -04:00
|
|
|
const promise = new Promise<CommandResult>((resolve, reject) => {
|
2020-11-29 15:06:57 -05:00
|
|
|
data[0]["return_code"] = this.commandErrorHandler.generateReturnCode(command, result => {
|
2020-08-22 15:33:30 -04:00
|
|
|
if(result.success) {
|
|
|
|
resolve(result);
|
|
|
|
} else {
|
|
|
|
reject(result);
|
|
|
|
}
|
2020-12-03 06:01:29 -05:00
|
|
|
}, data[0]["return_code"]);
|
2020-04-01 15:56:23 -04:00
|
|
|
|
|
|
|
try {
|
2020-11-29 15:06:57 -05:00
|
|
|
this.nativeHandle.send_command(command, data, options.flagset || []);
|
2020-04-01 15:56:23 -04:00
|
|
|
} catch(error) {
|
2020-08-22 15:33:30 -04:00
|
|
|
reject(tr("failed to send command"));
|
2020-04-01 15:56:23 -04:00
|
|
|
console.warn(tr("Failed to send command: %o"), error);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
});
|
2020-11-29 15:06:57 -05:00
|
|
|
return this.defaultCommandHandler.proxy_command_promise(promise, options);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
2020-12-13 08:51:10 -05:00
|
|
|
ping(): ConnectionPing {
|
2020-04-01 15:56:23 -04:00
|
|
|
return {
|
2020-12-13 08:51:10 -05:00
|
|
|
native: this.nativeHandle ? (this.nativeHandle.current_ping() / 1000) : -2,
|
|
|
|
javascript: undefined
|
2020-04-01 15:56:23 -04:00
|
|
|
};
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-11-29 15:06:57 -05:00
|
|
|
|
|
|
|
getControlStatistics(): ConnectionStatistics {
|
2020-12-02 15:00:51 -05:00
|
|
|
const stats = this.nativeHandle?.statistics();
|
|
|
|
|
2020-11-29 15:06:57 -05:00
|
|
|
return {
|
2020-12-02 15:00:51 -05:00
|
|
|
bytesReceived: stats?.control_bytes_received ? stats?.control_bytes_received : 0,
|
|
|
|
bytesSend: stats?.control_bytes_send ? stats?.control_bytes_send : 0
|
2020-11-29 15:06:57 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getVideoConnection(): VideoConnection {
|
|
|
|
return this.videoConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRtcConnection() : RTCConnection {
|
|
|
|
return this.rtcConnection;
|
|
|
|
}
|
2021-02-15 13:13:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
getServerType() : "teaspeak" | "teamspeak" | "unknown" {
|
|
|
|
return this.serverType;
|
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2020-04-01 15:56:23 -04:00
|
|
|
export class NativeConnectionCommandBoss extends AbstractCommandHandlerBoss {
|
|
|
|
constructor(connection: AbstractServerConnection) {
|
|
|
|
super(connection);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2020-04-01 15:56:23 -04:00
|
|
|
}
|