mirror of
https://github.com/coder/code-server.git
synced 2026-05-18 18:27:26 +02:00
Add channels
This commit is contained in:
@@ -1,27 +1,68 @@
|
||||
import { ClientConnectionEvent } from "vs/base/parts/ipc/common/ipc";
|
||||
import { ConnectionType } from "vs/platform/remote/common/remoteAgentConnection";
|
||||
import { Emitter } from "vs/base/common/event";
|
||||
import { PersistentProtocol, ISocket } from "vs/base/parts/ipc/common/ipc.net";
|
||||
import { VSBuffer } from "vs/base/common/buffer";
|
||||
|
||||
export interface Server {
|
||||
readonly _onDidClientConnect: Emitter<ClientConnectionEvent>;
|
||||
readonly connections: Map<ConnectionType, Map<string, Connection>>;
|
||||
}
|
||||
|
||||
export abstract class Connection {
|
||||
protected readonly _onClose = new Emitter<void>();
|
||||
private readonly _onClose = new Emitter<void>();
|
||||
public readonly onClose = this._onClose.event;
|
||||
|
||||
public constructor(private readonly protocol: PersistentProtocol) {
|
||||
private timeout: NodeJS.Timeout | undefined;
|
||||
private readonly wait = 1000 * 60 * 60;
|
||||
|
||||
public constructor(
|
||||
protected readonly server: Server,
|
||||
private readonly protocol: PersistentProtocol,
|
||||
) {
|
||||
// onClose seems to mean we want to disconnect, so dispose immediately.
|
||||
this.protocol.onClose(() => this.dispose());
|
||||
|
||||
// If the socket closes, we want to wait before disposing so we can
|
||||
// reconnect.
|
||||
this.protocol.onSocketClose(() => {
|
||||
// TODO: eventually we'll want to clean up the connection if nothing
|
||||
// ever connects back to it
|
||||
this.timeout = setTimeout(() => {
|
||||
this.dispose();
|
||||
}, this.wait);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Completely close and clean up the connection. Should only do this once we
|
||||
* don't need or want the connection. It cannot be re-used after this.
|
||||
*/
|
||||
public dispose(): void {
|
||||
this.protocol.sendDisconnect();
|
||||
this.protocol.getSocket().end();
|
||||
this.protocol.dispose();
|
||||
this._onClose.fire();
|
||||
}
|
||||
|
||||
public reconnect(socket: ISocket, buffer: VSBuffer): void {
|
||||
clearTimeout(this.timeout as any); // Not sure why the type doesn't work.
|
||||
this.protocol.beginAcceptReconnection(socket, buffer);
|
||||
this.protocol.endAcceptReconnection();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The management connection is used for all the IPC channels.
|
||||
*/
|
||||
export class ManagementConnection extends Connection {
|
||||
// in here they accept the connection
|
||||
// to the ipc of the RemoteServer
|
||||
public constructor(server: Server, protocol: PersistentProtocol) {
|
||||
super(server, protocol);
|
||||
// This will communicate back to the IPCServer that a new client has
|
||||
// connected.
|
||||
this.server._onDidClientConnect.fire({
|
||||
protocol,
|
||||
onDidClientDisconnect: this.onClose,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ExtensionHostConnection extends Connection {
|
||||
|
||||
Reference in New Issue
Block a user