src/vs/platform/remote/common/managedSocket.ts
145 LOC · 57 covered · 88 uncovered · 9 ranges · 7 concepts · 1 introducers · 6 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
remoteAgentConnection.ts ×53
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer, encodeBase64 } from '../../../base/common/buffer.js';
import { Emitter, Event, PauseableEmitter } from '../../../base/common/event.js';
import { Disposable, DisposableStore } from '../../../base/common/lifecycle.js';
import { ISocket, SocketCloseEvent, SocketDiagnostics, SocketDiagnosticsEventType } from '../../../base/parts/ipc/common/ipc.net.js';
export const makeRawSocketHeaders = (path: string, query: string, deubgLabel: string) => {
// https://tools.ietf.org/html/rfc6455#section-4
const buffer = new Uint8Array(16);
for (let i = 0; i < 16; i++) {
buffer[i] = Math.round(Math.random() * 256);
}
const nonce = encodeBase64(VSBuffer.wrap(buffer));
const headers = [
`GET ws://localhost${path}?${query}&skipWebSocketFrames=true HTTP/1.1`,
`Connection: Upgrade`,
`Upgrade: websocket`,
`Sec-WebSocket-Key: ${nonce}`
];
return headers.join('\r\n') + '\r\n\r\n';
};
export const socketRawEndHeaderSequence = VSBuffer.fromString('\r\n\r\n');
export interface RemoteSocketHalf {
onData: Emitter<VSBuffer>;
onClose: Emitter<SocketCloseEvent>;
onEnd: Emitter<void>;
}
/** Should be called immediately after making a ManagedSocket to make it ready for data flow. */
export async function connectManagedSocket<T extends ManagedSocket>(
socket: T,
path: string, query: string, debugLabel: string,
half: RemoteSocketHalf
): Promise<T> {
socket.write(VSBuffer.fromString(makeRawSocketHeaders(path, query, debugLabel)));
const d = new DisposableStore();
try {
return await new Promise<T>((resolve, reject) => {
let dataSoFar: VSBuffer | undefined;
d.add(socket.onData(d_1 => {
if (!dataSoFar) {
dataSoFar = d_1;
} else {
dataSoFar = VSBuffer.concat([dataSoFar, d_1], dataSoFar.byteLength + d_1.byteLength);
}
const index = dataSoFar.indexOf(socketRawEndHeaderSequence);
if (index === -1) {
return;
}
resolve(socket);
// pause data events until the socket consumer is hooked up. We may
// immediately emit remaining data, but if not there may still be
// microtasks queued which would fire data into the abyss.
socket.pauseData();
const rest = dataSoFar.slice(index + socketRawEndHeaderSequence.byteLength);
if (rest.byteLength) {
half.onData.fire(rest);
}
}));
d.add(socket.onClose(err => reject(err ?? new Error('socket closed'))));
d.add(socket.onEnd(() => reject(new Error('socket ended'))));
});
} catch (e) {
socket.dispose();
throw e;
} finally {
d.dispose();
}
}
export abstract class ManagedSocket extends Disposable implements ISocket {
private readonly pausableDataEmitter = this._register(new PauseableEmitter<VSBuffer>());
public onData: Event<VSBuffer> = (...args) => {
if (this.pausableDataEmitter.isPaused) {
queueMicrotask(() => this.pausableDataEmitter.resume());
}
return this.pausableDataEmitter.event(...args);
};
public onClose: Event<SocketCloseEvent>;
public onEnd: Event<void>;
private readonly didDisposeEmitter = this._register(new Emitter<void>());
public onDidDispose = this.didDisposeEmitter.event;
private ended = false;
protected constructor(
private readonly debugLabel: string,
half: RemoteSocketHalf,
) {
super();
this._register(half.onData);
this._register(half.onData.event(data => this.pausableDataEmitter.fire(data)));
this.onClose = this._register(half.onClose).event;
this.onEnd = this._register(half.onEnd).event;
}
/** Pauses data events until a new listener comes in onData() */
public pauseData() {
this.pausableDataEmitter.pause();
}
/** Flushes data to the socket. */
public drain(): Promise<void> {
return Promise.resolve();
}
/** Ends the remote socket. */
public end(): void {
this.ended = true;
this.closeRemote();
}
public abstract write(buffer: VSBuffer): void;
protected abstract closeRemote(): void;
traceSocketEvent(type: SocketDiagnosticsEventType, data?: VSBuffer | Uint8Array | ArrayBuffer | ArrayBufferView | unknown): void {
SocketDiagnostics.traceSocketEvent(this, this.debugLabel, type, data);
}
override dispose(): void {
if (!this.ended) {
this.closeRemote();
}
this.didDisposeEmitter.fire();
super.dispose();
}