|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { MainContext, ExtHostContext, MainThreadManagedSocketsShape, ExtHostManagedSocketsShape } from 'vs/workbench/api/common/extHost.protocol'; |
| 7 | +import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers'; |
| 8 | +import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle'; |
| 9 | +import { ManagedRemoteConnection, RemoteConnectionType } from 'vs/platform/remote/common/remoteAuthorityResolver'; |
| 10 | +import { VSBuffer } from 'vs/base/common/buffer'; |
| 11 | +import { IRemoteSocketFactoryService, ISocketFactory } from 'vs/platform/remote/common/remoteSocketFactoryService'; |
| 12 | +import { ISocket, SocketCloseEvent, SocketCloseEventType, SocketDiagnostics, SocketDiagnosticsEventType } from 'vs/base/parts/ipc/common/ipc.net'; |
| 13 | +import { Emitter, Event } from 'vs/base/common/event'; |
| 14 | +import { makeRawSocketHeaders, socketRawEndHeaderSequence } from 'vs/platform/remote/common/managedSocket'; |
| 15 | + |
| 16 | +@extHostNamedCustomer(MainContext.MainThreadManagedSockets) |
| 17 | +export class MainThreadManagedSockets extends Disposable implements MainThreadManagedSocketsShape { |
| 18 | + |
| 19 | + private readonly _proxy: ExtHostManagedSocketsShape; |
| 20 | + private readonly _registrations = new Map<number, IDisposable>(); |
| 21 | + private readonly _remoteSockets = new Map<number, RemoteSocketHalf>(); |
| 22 | + |
| 23 | + constructor( |
| 24 | + extHostContext: IExtHostContext, |
| 25 | + @IRemoteSocketFactoryService private readonly _remoteSocketFactoryService: IRemoteSocketFactoryService, |
| 26 | + ) { |
| 27 | + super(); |
| 28 | + this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostManagedSockets); |
| 29 | + } |
| 30 | + |
| 31 | + async $registerSocketFactory(socketFactoryId: number): Promise<void> { |
| 32 | + const that = this; |
| 33 | + const scoketFactory = new class implements ISocketFactory<RemoteConnectionType.Managed> { |
| 34 | + |
| 35 | + supports(connectTo: ManagedRemoteConnection): boolean { |
| 36 | + return (connectTo.id === socketFactoryId); |
| 37 | + } |
| 38 | + |
| 39 | + connect(connectTo: ManagedRemoteConnection, path: string, query: string, debugLabel: string): Promise<ISocket> { |
| 40 | + return new Promise<ISocket>((resolve, reject) => { |
| 41 | + if (connectTo.id !== socketFactoryId) { |
| 42 | + return reject(new Error('Invalid connectTo')); |
| 43 | + } |
| 44 | + |
| 45 | + const factoryId = connectTo.id; |
| 46 | + that._proxy.$openRemoteSocket(factoryId).then(socketId => { |
| 47 | + const half: RemoteSocketHalf = { |
| 48 | + onClose: new Emitter(), |
| 49 | + onData: new Emitter(), |
| 50 | + onEnd: new Emitter(), |
| 51 | + }; |
| 52 | + that._remoteSockets.set(socketId, half); |
| 53 | + |
| 54 | + ManagedSocket.connect(socketId, that._proxy, path, query, debugLabel, half) |
| 55 | + .then( |
| 56 | + socket => { |
| 57 | + socket.onDidDispose(() => that._remoteSockets.delete(socketId)); |
| 58 | + resolve(socket); |
| 59 | + }, |
| 60 | + err => { |
| 61 | + that._remoteSockets.delete(socketId); |
| 62 | + reject(err); |
| 63 | + }); |
| 64 | + }).catch(reject); |
| 65 | + }); |
| 66 | + } |
| 67 | + }; |
| 68 | + this._registrations.set(socketFactoryId, this._remoteSocketFactoryService.register(RemoteConnectionType.Managed, scoketFactory)); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + async $unregisterSocketFactory(socketFactoryId: number): Promise<void> { |
| 73 | + this._registrations.get(socketFactoryId)?.dispose(); |
| 74 | + } |
| 75 | + |
| 76 | + $onDidManagedSocketHaveData(socketId: number, data: VSBuffer): void { |
| 77 | + this._remoteSockets.get(socketId)?.onData.fire(data); |
| 78 | + } |
| 79 | + |
| 80 | + $onDidManagedSocketClose(socketId: number, error: string | undefined): void { |
| 81 | + this._remoteSockets.get(socketId)?.onClose.fire({ |
| 82 | + type: SocketCloseEventType.NodeSocketCloseEvent, |
| 83 | + error: error ? new Error(error) : undefined, |
| 84 | + hadError: !!error |
| 85 | + }); |
| 86 | + this._remoteSockets.delete(socketId); |
| 87 | + } |
| 88 | + |
| 89 | + $onDidManagedSocketEnd(socketId: number): void { |
| 90 | + this._remoteSockets.get(socketId)?.onEnd.fire(); |
| 91 | + this._remoteSockets.delete(socketId); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +interface RemoteSocketHalf { |
| 96 | + onData: Emitter<VSBuffer>; |
| 97 | + onClose: Emitter<SocketCloseEvent>; |
| 98 | + onEnd: Emitter<void>; |
| 99 | +} |
| 100 | + |
| 101 | +export class ManagedSocket extends Disposable implements ISocket { |
| 102 | + public static connect( |
| 103 | + socketId: number, |
| 104 | + proxy: ExtHostManagedSocketsShape, |
| 105 | + path: string, query: string, debugLabel: string, |
| 106 | + |
| 107 | + half: { |
| 108 | + onClose: Emitter<SocketCloseEvent>; |
| 109 | + onData: Emitter<VSBuffer>; |
| 110 | + onEnd: Emitter<void>; |
| 111 | + } |
| 112 | + ): Promise<ManagedSocket> { |
| 113 | + const socket = new ManagedSocket(socketId, proxy, debugLabel, half.onClose, half.onData, half.onEnd); |
| 114 | + |
| 115 | + socket.write(VSBuffer.fromString(makeRawSocketHeaders(path, query, debugLabel))); |
| 116 | + |
| 117 | + const d = new DisposableStore(); |
| 118 | + return new Promise<ManagedSocket>((resolve, reject) => { |
| 119 | + d.add(socket.onData(d => { |
| 120 | + if (d.indexOf(socketRawEndHeaderSequence) !== -1) { |
| 121 | + resolve(socket); |
| 122 | + } |
| 123 | + })); |
| 124 | + |
| 125 | + d.add(socket.onClose(err => reject(err ?? new Error('socket closed')))); |
| 126 | + d.add(socket.onEnd(() => reject(new Error('socket ended')))); |
| 127 | + }).finally(() => d.dispose()); |
| 128 | + } |
| 129 | + |
| 130 | + public onData: Event<VSBuffer>; |
| 131 | + public onClose: Event<SocketCloseEvent>; |
| 132 | + public onEnd: Event<void>; |
| 133 | + |
| 134 | + private readonly didDisposeEmitter = this._register(new Emitter<void>()); |
| 135 | + public onDidDispose = this.didDisposeEmitter.event; |
| 136 | + |
| 137 | + private ended = false; |
| 138 | + |
| 139 | + private constructor( |
| 140 | + private readonly socketId: number, |
| 141 | + private readonly proxy: ExtHostManagedSocketsShape, |
| 142 | + private readonly debugLabel: string, |
| 143 | + onCloseEmitter: Emitter<SocketCloseEvent>, |
| 144 | + onDataEmitter: Emitter<VSBuffer>, |
| 145 | + onEndEmitter: Emitter<void>, |
| 146 | + ) { |
| 147 | + super(); |
| 148 | + this.onClose = this._register(onCloseEmitter).event; |
| 149 | + this.onData = this._register(onDataEmitter).event; |
| 150 | + this.onEnd = this._register(onEndEmitter).event; |
| 151 | + } |
| 152 | + |
| 153 | + write(buffer: VSBuffer): void { |
| 154 | + this.proxy.$remoteSocketWrite(this.socketId, buffer); |
| 155 | + } |
| 156 | + |
| 157 | + end(): void { |
| 158 | + this.ended = true; |
| 159 | + this.proxy.$remoteSocketEnd(this.socketId); |
| 160 | + } |
| 161 | + |
| 162 | + drain(): Promise<void> { |
| 163 | + return this.proxy.$remoteSocketDrain(this.socketId); |
| 164 | + } |
| 165 | + |
| 166 | + traceSocketEvent(type: SocketDiagnosticsEventType, data?: any): void { |
| 167 | + SocketDiagnostics.traceSocketEvent(this, this.debugLabel, type, data); |
| 168 | + } |
| 169 | + |
| 170 | + override dispose(): void { |
| 171 | + if (!this.ended) { |
| 172 | + this.proxy.$remoteSocketEnd(this.socketId); |
| 173 | + } |
| 174 | + |
| 175 | + this.didDisposeEmitter.fire(); |
| 176 | + super.dispose(); |
| 177 | + } |
| 178 | +} |
0 commit comments