src/vs/workbench/api/common/extHostManagedSockets.ts
121 LOC · 57 covered · 64 uncovered · 10 ranges · 38 concepts · 1 introducers · 33 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.
/*---------------------------------------------------------------------------------------------
extHostExtensionService.ts ×64
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ExtHostManagedSocketsShape, MainContext, MainThreadManagedSocketsShape } from './extHost.protocol.js';
import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
import * as vscode from 'vscode';
import { Disposable, DisposableStore, toDisposable } from '../../../base/common/lifecycle.js';
import { IExtHostRpcService } from './extHostRpcService.js';
import { VSBuffer } from '../../../base/common/buffer.js';
export interface IExtHostManagedSockets extends ExtHostManagedSocketsShape {
setFactory(socketFactoryId: number, makeConnection: () => Thenable<vscode.ManagedMessagePassing>): void;
/**
* Opens a managed connection in-process using the currently registered
* factory. Used by consumers that live inside the extension host (e.g. the
* browser tunnel proxy). There is only ever one active remote per window, so
* the latest factory is the correct one to dial; this avoids depending on a
* factory id that can lag connection-data updates by a renderer round-trip.
*/
makeConnection(): Promise<vscode.ManagedMessagePassing>;
readonly _serviceBrand: undefined;
}
export const IExtHostManagedSockets = createDecorator<IExtHostManagedSockets>('IExtHostManagedSockets');
export class ExtHostManagedSockets implements IExtHostManagedSockets {
declare readonly _serviceBrand: undefined;
private readonly _proxy: MainThreadManagedSocketsShape;
private _remoteSocketIdCounter = 0;
private _factory: ManagedSocketFactory | null = null;
private readonly _managedRemoteSockets: Map<number, ManagedSocket> = new Map();
constructor(
@IExtHostRpcService extHostRpc: IExtHostRpcService,
) {
this._proxy = extHostRpc.getProxy(MainContext.MainThreadManagedSockets);
}
setFactory(socketFactoryId: number, makeConnection: () => Thenable<vscode.ManagedMessagePassing>): void {
// Terminate all previous sockets
for (const socket of this._managedRemoteSockets.values()) {
// calling dispose() will lead to it removing itself from the map
socket.dispose();
}
// Unregister previous factory
if (this._factory) {
this._proxy.$unregisterSocketFactory(this._factory.socketFactoryId);
}
this._factory = new ManagedSocketFactory(socketFactoryId, makeConnection);
this._proxy.$registerSocketFactory(this._factory.socketFactoryId);
}
makeConnection(): Promise<vscode.ManagedMessagePassing> {
if (!this._factory) {
throw new Error('No managed socket factory registered');
}
return Promise.resolve(this._factory.makeConnection());
}
async $openRemoteSocket(socketFactoryId: number): Promise<number> {
if (!this._factory || this._factory.socketFactoryId !== socketFactoryId) {
throw new Error(`No socket factory with id ${socketFactoryId}`);
}
const id = (++this._remoteSocketIdCounter);
const socket = await this._factory.makeConnection();
const disposable = new DisposableStore();
this._managedRemoteSockets.set(id, new ManagedSocket(id, socket, disposable));
disposable.add(toDisposable(() => this._managedRemoteSockets.delete(id)));
disposable.add(socket.onDidEnd(() => {
this._proxy.$onDidManagedSocketEnd(id);
disposable.dispose();
}));
disposable.add(socket.onDidClose(e => {
this._proxy.$onDidManagedSocketClose(id, e?.stack ?? e?.message);
disposable.dispose();
}));
disposable.add(socket.onDidReceiveMessage(e => this._proxy.$onDidManagedSocketHaveData(id, VSBuffer.wrap(e))));
return id;
}
$remoteSocketWrite(socketId: number, buffer: VSBuffer): void {
this._managedRemoteSockets.get(socketId)?.actual.send(buffer.buffer);
}
$remoteSocketEnd(socketId: number): void {
const socket = this._managedRemoteSockets.get(socketId);
if (socket) {
socket.actual.end();
socket.dispose();
}
}
async $remoteSocketDrain(socketId: number): Promise<void> {
await this._managedRemoteSockets.get(socketId)?.actual.drain?.();
}
class ManagedSocketFactory {
constructor(
public readonly socketFactoryId: number,
public readonly makeConnection: () => Thenable<vscode.ManagedMessagePassing>,
) { }
class ManagedSocket extends Disposable {
constructor(
public readonly socketId: number,
public readonly actual: vscode.ManagedMessagePassing,
disposer: DisposableStore,
) {
super();
this._register(disposer);
}