Skip to content

Commit 0c504ea

Browse files
committed
Cleanup in RPCProtocol
1 parent b63e14f commit 0c504ea

7 files changed

Lines changed: 110 additions & 67 deletions

File tree

src/vs/workbench/node/extensionHostMain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import nls = require('vs/nls');
99
import pfs = require('vs/base/node/pfs');
1010
import { TPromise } from 'vs/base/common/winjs.base';
1111
import { join } from 'path';
12-
import { IRemoteCom } from 'vs/workbench/services/extensions/node/ipcRemoteCom';
12+
import { RPCProtocol } from 'vs/workbench/services/extensions/node/rpcProtocol';
1313
import { ExtHostExtensionService } from 'vs/workbench/api/node/extHostExtensionService';
1414
import { ExtHostThreadService } from 'vs/workbench/services/thread/node/extHostThreadService';
1515
import { QueryType, ISearchQuery } from 'vs/platform/search/common/search';
@@ -40,12 +40,12 @@ export class ExtensionHostMain {
4040
private _environment: IEnvironment;
4141
private _extensionService: ExtHostExtensionService;
4242

43-
constructor(remoteCom: IRemoteCom, initData: IInitData) {
43+
constructor(rpcProtocol: RPCProtocol, initData: IInitData) {
4444
this._environment = initData.environment;
4545
this._workspace = initData.workspace;
4646

4747
// services
48-
const threadService = new ExtHostThreadService(remoteCom);
48+
const threadService = new ExtHostThreadService(rpcProtocol);
4949
const telemetryService = new RemoteTelemetryService('pluginHostTelemetry', threadService);
5050
this._extensionService = new ExtHostExtensionService(initData, threadService, telemetryService);
5151

src/vs/workbench/node/extensionHostProcess.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { onUnexpectedError } from 'vs/base/common/errors';
99
import { TPromise } from 'vs/base/common/winjs.base';
1010
import { ExtensionHostMain, exit } from 'vs/workbench/node/extensionHostMain';
11-
import { IRemoteCom, createProxyProtocol } from 'vs/workbench/services/extensions/node/ipcRemoteCom';
11+
import { RPCProtocol } from 'vs/workbench/services/extensions/node/rpcProtocol';
1212
import { parse } from 'vs/base/common/marshalling';
1313
import { IInitData } from 'vs/workbench/api/node/extHost.protocol';
1414
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
@@ -17,7 +17,7 @@ import { createConnection } from 'net';
1717
import Event, { filterEvent } from 'vs/base/common/event';
1818

1919
interface IRendererConnection {
20-
remoteCom: IRemoteCom;
20+
rpcProtocol: RPCProtocol;
2121
initData: IInitData;
2222
}
2323

@@ -71,7 +71,7 @@ function connectToRenderer(protocol: IMessagePassingProtocol): TPromise<IRendere
7171
first.dispose();
7272

7373
const initData = parse(raw);
74-
const remoteCom = createProxyProtocol(protocol);
74+
const rpcProtocol = new RPCProtocol(protocol);
7575

7676
// Print a console message when rejection isn't handled within N seconds. For details:
7777
// see https://nodejs.org/api/process.html#process_event_unhandledrejection
@@ -112,7 +112,7 @@ function connectToRenderer(protocol: IMessagePassingProtocol): TPromise<IRendere
112112
// Tell the outside that we are initialized
113113
protocol.send('initialized');
114114

115-
c({ remoteCom, initData });
115+
c({ rpcProtocol, initData });
116116
});
117117

118118
// Tell the outside that we are ready to receive messages
@@ -125,7 +125,7 @@ createExtHostProtocol().then(protocol => {
125125
return connectToRenderer(protocol);
126126
}).then(renderer => {
127127
// setup things
128-
const extensionHostMain = new ExtensionHostMain(renderer.remoteCom, renderer.initData);
128+
const extensionHostMain = new ExtensionHostMain(renderer.rpcProtocol, renderer.initData);
129129
onTerminate = () => extensionHostMain.terminate();
130130
return extensionHostMain.start();
131131
}).done(null, err => console.error(err));

src/vs/workbench/services/extensions/node/ipcRemoteCom.ts renamed to src/vs/workbench/services/extensions/node/rpcProtocol.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@ import * as errors from 'vs/base/common/errors';
1010
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
1111
import { LazyPromise } from "vs/workbench/services/extensions/node/lazyPromise";
1212

13-
export interface IManyHandler {
14-
handle(rpcId: string, method: string, args: any[]): any;
13+
export interface IDispatcher {
14+
invoke(proxyId: string, methodName: string, args: any[]): any;
1515
}
1616

17-
export interface IRemoteCom {
18-
callOnRemote(proxyId: string, path: string, args: any[]): TPromise<any>;
19-
setManyHandler(handler: IManyHandler): void;
20-
}
21-
22-
export function createProxyProtocol(protocol: IMessagePassingProtocol): IRemoteCom {
23-
return new RPCManager(protocol);
24-
}
25-
26-
export class RPCManager implements IRemoteCom {
17+
export class RPCProtocol {
2718

28-
private _bigHandler: IManyHandler;
19+
private _bigHandler: IDispatcher;
2920
private _lastMessageId: number;
3021
private readonly _invokedHandlers: { [req: string]: TPromise<any>; };
3122
private readonly _pendingRPCReplies: { [msgId: string]: LazyPromise; };
@@ -97,28 +88,28 @@ export class RPCManager implements IRemoteCom {
9788
});
9889
}
9990

100-
private _invokeHandler(rpcId: string, method: string, args: any[]): TPromise<any> {
91+
private _invokeHandler(proxyId: string, methodName: string, args: any[]): TPromise<any> {
10192
try {
102-
return TPromise.as(this._bigHandler.handle(rpcId, method, args));
93+
return TPromise.as(this._bigHandler.invoke(proxyId, methodName, args));
10394
} catch (err) {
10495
return TPromise.wrapError(err);
10596
}
10697
}
10798

108-
public callOnRemote(proxyId: string, path: string, args: any[]): TPromise<any> {
99+
public callOnRemote(proxyId: string, methodName: string, args: any[]): TPromise<any> {
109100
let req = String(++this._lastMessageId);
110101
let result = new LazyPromise(() => {
111102
this._multiplexor.send(MessageFactory.cancel(req));
112103
});
113104

114105
this._pendingRPCReplies[req] = result;
115106

116-
this._multiplexor.send(MessageFactory.request(req, proxyId, path, args));
107+
this._multiplexor.send(MessageFactory.request(req, proxyId, methodName, args));
117108

118109
return result;
119110
}
120111

121-
public setManyHandler(handler: IManyHandler): void {
112+
public setDispatcher(handler: IDispatcher): void {
122113
this._bigHandler = handler;
123114
}
124115
}

src/vs/workbench/services/thread/electron-browser/threadService.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
'use strict';
77

88
import * as strings from 'vs/base/common/strings';
9-
import { TPromise } from 'vs/base/common/winjs.base';
10-
import { IRemoteCom, createProxyProtocol } from 'vs/workbench/services/extensions/node/ipcRemoteCom';
9+
import { RPCProtocol } from 'vs/workbench/services/extensions/node/rpcProtocol';
1110
import { AbstractThreadService } from 'vs/workbench/services/thread/node/abstractThreadService';
1211
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
1312
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
@@ -38,20 +37,11 @@ export class MainThreadService extends AbstractThreadService implements IThreadS
3837

3938
_serviceBrand: any;
4039

41-
private _remoteCom: IRemoteCom;
42-
4340
constructor(protocol: IMessagePassingProtocol, @IEnvironmentService environmentService: IEnvironmentService) {
44-
super(true);
45-
4641
if (logExtensionHostCommunication || environmentService.logExtensionHostCommunication) {
4742
protocol = asLoggingProtocol(protocol);
4843
}
4944

50-
this._remoteCom = createProxyProtocol(protocol);
51-
this._remoteCom.setManyHandler(this);
52-
}
53-
54-
protected _callOnRemote(proxyId: string, path: string, args: any[]): TPromise<any> {
55-
return this._remoteCom.callOnRemote(proxyId, path, args);
45+
super(new RPCProtocol(protocol), true);
5646
}
5747
}

src/vs/workbench/services/thread/node/abstractThreadService.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,34 @@
55
'use strict';
66

77
import { TPromise } from 'vs/base/common/winjs.base';
8-
import { IManyHandler } from 'vs/workbench/services/extensions/node/ipcRemoteCom';
8+
import { IDispatcher, RPCProtocol } from 'vs/workbench/services/extensions/node/rpcProtocol';
99
import { ProxyIdentifier } from 'vs/workbench/services/thread/common/threadService';
1010

1111
// declare var Proxy:any; // TODO@TypeScript
1212

13-
export abstract class AbstractThreadService implements IManyHandler {
13+
export abstract class AbstractThreadService implements IDispatcher {
1414

15-
private _isMain: boolean;
16-
protected _locals: { [id: string]: any; };
17-
private _proxies: { [id: string]: any; } = Object.create(null);
15+
private readonly _rpcProtocol: RPCProtocol;
16+
private readonly _isMain: boolean;
17+
protected readonly _locals: { [id: string]: any; };
18+
private readonly _proxies: { [id: string]: any; } = Object.create(null);
1819

19-
constructor(isMain: boolean) {
20+
constructor(rpcProtocol: RPCProtocol, isMain: boolean) {
21+
this._rpcProtocol = rpcProtocol;
2022
this._isMain = isMain;
2123
this._locals = Object.create(null);
2224
this._proxies = Object.create(null);
25+
this._rpcProtocol.setDispatcher(this);
2326
}
2427

25-
public handle(rpcId: string, methodName: string, args: any[]): any {
26-
if (!this._locals[rpcId]) {
27-
throw new Error('Unknown actor ' + rpcId);
28+
public invoke(proxyId: string, methodName: string, args: any[]): any {
29+
if (!this._locals[proxyId]) {
30+
throw new Error('Unknown actor ' + proxyId);
2831
}
29-
let actor = this._locals[rpcId];
32+
let actor = this._locals[proxyId];
3033
let method = actor[methodName];
3134
if (typeof method !== 'function') {
32-
throw new Error('Unknown method ' + methodName + ' on actor ' + rpcId);
35+
throw new Error('Unknown method ' + methodName + ' on actor ' + proxyId);
3336
}
3437
return method.apply(actor, args);
3538
}
@@ -41,12 +44,12 @@ export abstract class AbstractThreadService implements IManyHandler {
4144
return this._proxies[identifier.id];
4245
}
4346

44-
private _createProxy<T>(id: string, methodNames: string[]): T {
47+
private _createProxy<T>(proxyId: string, methodNames: string[]): T {
4548
// Check below how to switch to native proxies
4649
let result: any = {};
4750
for (let i = 0; i < methodNames.length; i++) {
4851
let methodName = methodNames[i];
49-
result[methodName] = this.createMethodProxy(id, methodName);
52+
result[methodName] = this._createMethodProxy(proxyId, methodName);
5053
}
5154
return result;
5255

@@ -60,9 +63,9 @@ export abstract class AbstractThreadService implements IManyHandler {
6063
// return new Proxy({}, handler);
6164
}
6265

63-
private createMethodProxy(id: string, methodName: string): (...myArgs: any[]) => TPromise<any> {
66+
private _createMethodProxy(proxyId: string, methodName: string): (...myArgs: any[]) => TPromise<any> {
6467
return (...myArgs: any[]) => {
65-
return this._callOnRemote(id, methodName, myArgs);
68+
return this._callOnRemote(proxyId, methodName, myArgs);
6669
};
6770
}
6871

@@ -73,5 +76,7 @@ export abstract class AbstractThreadService implements IManyHandler {
7376
this._locals[identifier.id] = value;
7477
}
7578

76-
protected abstract _callOnRemote(proxyId: string, path: string, args: any[]): TPromise<any>;
79+
private _callOnRemote(proxyId: string, methodName: string, args: any[]): TPromise<any> {
80+
return this._rpcProtocol.callOnRemote(proxyId, methodName, args);
81+
}
7782
}

src/vs/workbench/services/thread/node/extHostThreadService.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { IRemoteCom } from 'vs/workbench/services/extensions/node/ipcRemoteCom';
8-
import { TPromise } from 'vs/base/common/winjs.base';
7+
import { RPCProtocol } from 'vs/workbench/services/extensions/node/rpcProtocol';
98
import { AbstractThreadService } from 'vs/workbench/services/thread/node/abstractThreadService';
109
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
1110

1211
export class ExtHostThreadService extends AbstractThreadService implements IThreadService {
1312
public _serviceBrand: any;
14-
protected _remoteCom: IRemoteCom;
1513

16-
constructor(remoteCom: IRemoteCom) {
17-
super(false);
18-
this._remoteCom = remoteCom;
19-
this._remoteCom.setManyHandler(this);
20-
}
21-
22-
protected _callOnRemote(proxyId: string, path: string, args: any[]): TPromise<any> {
23-
return this._remoteCom.callOnRemote(proxyId, path, args);
14+
constructor(rpcProtocol: RPCProtocol) {
15+
super(rpcProtocol, false);
2416
}
2517
}

src/vs/workbench/test/electron-browser/api/testThreadService.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
'use strict';
77

88
import { TPromise } from 'vs/base/common/winjs.base';
9-
import { AbstractThreadService } from 'vs/workbench/services/thread/node/abstractThreadService';
109
import { IThreadService, ProxyIdentifier } from 'vs/workbench/services/thread/common/threadService';
1110

1211
export function OneGetThreadService(thing: any): IThreadService {
@@ -21,7 +20,73 @@ export function OneGetThreadService(thing: any): IThreadService {
2120
};
2221
}
2322

24-
export class TestThreadService extends AbstractThreadService implements IThreadService {
23+
export abstract class AbstractTestThreadService {
24+
25+
private _isMain: boolean;
26+
protected _locals: { [id: string]: any; };
27+
private _proxies: { [id: string]: any; } = Object.create(null);
28+
29+
constructor(isMain: boolean) {
30+
this._isMain = isMain;
31+
this._locals = Object.create(null);
32+
this._proxies = Object.create(null);
33+
}
34+
35+
public handle(rpcId: string, methodName: string, args: any[]): any {
36+
if (!this._locals[rpcId]) {
37+
throw new Error('Unknown actor ' + rpcId);
38+
}
39+
let actor = this._locals[rpcId];
40+
let method = actor[methodName];
41+
if (typeof method !== 'function') {
42+
throw new Error('Unknown method ' + methodName + ' on actor ' + rpcId);
43+
}
44+
return method.apply(actor, args);
45+
}
46+
47+
get<T>(identifier: ProxyIdentifier<T>): T {
48+
if (!this._proxies[identifier.id]) {
49+
this._proxies[identifier.id] = this._createProxy(identifier.id, identifier.methodNames);
50+
}
51+
return this._proxies[identifier.id];
52+
}
53+
54+
private _createProxy<T>(id: string, methodNames: string[]): T {
55+
// Check below how to switch to native proxies
56+
let result: any = {};
57+
for (let i = 0; i < methodNames.length; i++) {
58+
let methodName = methodNames[i];
59+
result[methodName] = this.createMethodProxy(id, methodName);
60+
}
61+
return result;
62+
63+
// let handler = {
64+
// get: (target, name) => {
65+
// return (...myArgs: any[]) => {
66+
// return this._callOnRemote(id, name, myArgs);
67+
// };
68+
// }
69+
// };
70+
// return new Proxy({}, handler);
71+
}
72+
73+
private createMethodProxy(id: string, methodName: string): (...myArgs: any[]) => TPromise<any> {
74+
return (...myArgs: any[]) => {
75+
return this._callOnRemote(id, methodName, myArgs);
76+
};
77+
}
78+
79+
set<T>(identifier: ProxyIdentifier<T>, value: T): void {
80+
if (identifier.isMain !== this._isMain) {
81+
throw new Error('Mismatch in object registration!');
82+
}
83+
this._locals[identifier.id] = value;
84+
}
85+
86+
protected abstract _callOnRemote(proxyId: string, path: string, args: any[]): TPromise<any>;
87+
}
88+
89+
export class TestThreadService extends AbstractTestThreadService implements IThreadService {
2590
public _serviceBrand: any;
2691

2792
constructor() {

0 commit comments

Comments
 (0)