2020-03-30 11:44:18 +00:00
|
|
|
import {CommandHelper} from "tc-shared/connection/CommandHelper";
|
|
|
|
import {HandshakeHandler} from "tc-shared/connection/HandshakeHandler";
|
|
|
|
import {CommandResult} from "tc-shared/connection/ServerConnectionDeclaration";
|
|
|
|
import {ServerAddress} from "tc-shared/ui/server";
|
|
|
|
import {ConnectionHandler, ConnectionState} from "tc-shared/ConnectionHandler";
|
|
|
|
import {AbstractCommandHandlerBoss} from "tc-shared/connection/AbstractCommandHandler";
|
2020-08-10 12:41:34 +00:00
|
|
|
import {Registry} from "tc-shared/events";
|
|
|
|
import {AbstractVoiceConnection} from "tc-shared/connection/VoiceConnection";
|
2020-03-30 11:44:18 +00:00
|
|
|
|
|
|
|
export interface CommandOptions {
|
|
|
|
flagset?: string[]; /* default: [] */
|
|
|
|
process_result?: boolean; /* default: true */
|
|
|
|
|
|
|
|
timeout?: number /* default: 1000 */;
|
|
|
|
}
|
|
|
|
export const CommandOptionDefaults: CommandOptions = {
|
|
|
|
flagset: [],
|
|
|
|
process_result: true,
|
|
|
|
timeout: 1000
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:41:34 +00:00
|
|
|
export interface ServerConnectionEvents {
|
|
|
|
notify_connection_state_changed: {
|
|
|
|
oldState: ConnectionState,
|
|
|
|
newState: ConnectionState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
export type ConnectionStateListener = (old_state: ConnectionState, new_state: ConnectionState) => any;
|
|
|
|
export abstract class AbstractServerConnection {
|
2020-08-10 12:41:34 +00:00
|
|
|
readonly events: Registry<ServerConnectionEvents>;
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
readonly client: ConnectionHandler;
|
|
|
|
readonly command_helper: CommandHelper;
|
2020-08-10 12:41:34 +00:00
|
|
|
protected connectionState: ConnectionState = ConnectionState.UNCONNECTED;
|
2020-03-30 11:44:18 +00:00
|
|
|
|
|
|
|
protected constructor(client: ConnectionHandler) {
|
2020-08-10 12:41:34 +00:00
|
|
|
this.events = new Registry<ServerConnectionEvents>();
|
2020-03-30 11:44:18 +00:00
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
this.command_helper = new CommandHelper(this);
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
/* resolved as soon a connection has been established. This does not means that the authentication had yet been done! */
|
|
|
|
abstract connect(address: ServerAddress, handshake: HandshakeHandler, timeout?: number) : Promise<void>;
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
abstract connected() : boolean;
|
|
|
|
abstract disconnect(reason?: string) : Promise<void>;
|
2019-04-04 19:47:52 +00:00
|
|
|
|
2020-08-10 12:41:34 +00:00
|
|
|
abstract getVoiceConnection() : AbstractVoiceConnection;
|
2019-04-15 13:33:51 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
abstract command_handler_boss() : AbstractCommandHandlerBoss;
|
|
|
|
abstract send_command(command: string, data?: any | any[], options?: CommandOptions) : Promise<CommandResult>;
|
2019-08-21 08:00:01 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
abstract remote_address() : ServerAddress; /* only valid when connected */
|
2020-07-25 11:56:30 +00:00
|
|
|
connectionProxyAddress() : ServerAddress | undefined { return undefined; };
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
abstract handshake_handler() : HandshakeHandler; /* only valid when connected */
|
2020-03-29 10:54:15 +00:00
|
|
|
|
2020-04-09 13:10:14 +00:00
|
|
|
//FIXME: Remove this this is currently only some kind of hack
|
2020-04-11 09:43:41 +00:00
|
|
|
updateConnectionState(state: ConnectionState) {
|
2020-08-10 12:41:34 +00:00
|
|
|
if(state === this.connectionState) return;
|
2020-04-21 14:17:21 +00:00
|
|
|
|
2020-08-10 12:41:34 +00:00
|
|
|
const oldState = this.connectionState;
|
|
|
|
this.connectionState = state;
|
|
|
|
this.events.fire("notify_connection_state_changed", { oldState: oldState, newState: state });
|
2020-04-11 09:43:41 +00:00
|
|
|
}
|
2020-04-09 13:10:14 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
abstract ping() : {
|
|
|
|
native: number,
|
|
|
|
javascript?: number
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ServerCommand {
|
|
|
|
command: string;
|
|
|
|
arguments: any[];
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
export interface SingleCommandHandler {
|
|
|
|
name?: string;
|
2020-04-04 10:03:48 +00:00
|
|
|
command?: string | string[];
|
2020-03-30 11:44:18 +00:00
|
|
|
timeout?: number;
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
/* if the return is true then the command handler will be removed */
|
|
|
|
function: (command: ServerCommand) => boolean;
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|