ipc.cp.ts 6.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6
import { ChildProcess, fork } from 'child_process';
E
Erich Gamma 已提交
7
import { IDisposable } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
8
import { Promise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
9 10
import { Delayer } from 'vs/base/common/async';
import { clone, assign } from 'vs/base/common/objects';
J
Joao Moreno 已提交
11 12
import { Emitter } from 'vs/base/common/event';
import { fromEventEmitter } from 'vs/base/node/event';
R
roblou 已提交
13
import { createQueuedSender } from 'vs/base/node/processes';
J
Joao Moreno 已提交
14
import { ChannelServer as IPCServer, ChannelClient as IPCClient, IChannelClient, IChannel } from 'vs/base/parts/ipc/common/ipc';
E
Erich Gamma 已提交
15 16 17 18

export class Server extends IPCServer {
	constructor() {
		super({
J
Joao Moreno 已提交
19
			send: r => { try { process.send(r); } catch (e) { /* not much to do */ } },
J
Joao Moreno 已提交
20
			onMessage: fromEventEmitter(process, 'message', msg => msg)
E
Erich Gamma 已提交
21 22 23 24 25 26
		});

		process.once('disconnect', () => this.dispose());
	}
}

J
Joao Moreno 已提交
27
export interface IIPCOptions {
E
Erich Gamma 已提交
28 29 30 31 32 33 34

	/**
	 * A descriptive name for the server this connection is to. Used in logging.
	 */
	serverName: string;

	/**
J
Joao Moreno 已提交
35
	 * Time in millies before killing the ipc process. The next request after killing will start it again.
E
Erich Gamma 已提交
36
	 */
J
Johannes Rieken 已提交
37
	timeout?: number;
E
Erich Gamma 已提交
38 39 40 41

	/**
	 * Arguments to the module to execute.
	 */
J
Johannes Rieken 已提交
42
	args?: string[];
E
Erich Gamma 已提交
43 44

	/**
J
Joao Moreno 已提交
45
	 * Environment key-value pairs to be passed to the process that gets spawned for the ipc.
E
Erich Gamma 已提交
46
	 */
J
Johannes Rieken 已提交
47
	env?: any;
E
Erich Gamma 已提交
48 49 50 51

	/**
	 * Allows to assign a debug port for debugging the application executed.
	 */
J
Johannes Rieken 已提交
52
	debug?: number;
E
Erich Gamma 已提交
53 54 55 56

	/**
	 * Allows to assign a debug port for debugging the application and breaking it on the first line.
	 */
J
Johannes Rieken 已提交
57
	debugBrk?: number;
58 59 60 61 62 63

	/**
	 * Enables our createQueuedSender helper for this Client. Uses a queue when the internal Node.js queue is
	 * full of messages - see notes on that method.
	 */
	useQueue?: boolean;
E
Erich Gamma 已提交
64 65
}

J
Joao Moreno 已提交
66
export class Client implements IChannelClient, IDisposable {
E
Erich Gamma 已提交
67 68 69

	private disposeDelayer: Delayer<void>;
	private activeRequests: Promise[];
70 71 72
	private child: ChildProcess;
	private _client: IPCClient;
	private channels: { [name: string]: IChannel };
E
Erich Gamma 已提交
73

J
Joao Moreno 已提交
74 75
	constructor(private modulePath: string, private options: IIPCOptions) {
		const timeout = options && options.timeout ? options.timeout : 60000;
E
Erich Gamma 已提交
76 77
		this.disposeDelayer = new Delayer<void>(timeout);
		this.activeRequests = [];
78
		this.child = null;
E
Erich Gamma 已提交
79
		this._client = null;
J
Joao Moreno 已提交
80
		this.channels = Object.create(null);
E
Erich Gamma 已提交
81 82
	}

J
Joao Moreno 已提交
83
	getChannel<T extends IChannel>(channelName: string): T {
J
Joao Moreno 已提交
84
		const call = (command, arg) => this.request(channelName, command, arg);
J
Joao Moreno 已提交
85
		return { call } as T;
E
Erich Gamma 已提交
86 87
	}

J
Joao Moreno 已提交
88
	protected request(channelName: string, name: string, arg: any): Promise {
J
Joao Moreno 已提交
89 90 91 92
		if (!this.disposeDelayer) {
			return Promise.wrapError('disposed');
		}

E
Erich Gamma 已提交
93 94
		this.disposeDelayer.cancel();

95 96
		const channel = this.channels[channelName] || (this.channels[channelName] = this.client.getChannel(channelName));
		const request: Promise = channel.call(name, arg);
E
Erich Gamma 已提交
97 98 99 100

		// Progress doesn't propagate across 'then', we need to create a promise wrapper
		const result = new Promise((c, e, p) => {
			request.then(c, e, p).done(() => {
J
Joao Moreno 已提交
101 102 103 104
				if (!this.activeRequests) {
					return;
				}

E
Erich Gamma 已提交
105
				this.activeRequests.splice(this.activeRequests.indexOf(result), 1);
106 107 108 109

				if (this.activeRequests.length === 0) {
					this.disposeDelayer.trigger(() => this.disposeClient());
				}
E
Erich Gamma 已提交
110 111 112 113 114 115 116
			});
		}, () => request.cancel());

		this.activeRequests.push(result);
		return result;
	}

117
	private get client(): IPCClient {
E
Erich Gamma 已提交
118
		if (!this._client) {
119
			const args = this.options && this.options.args ? this.options.args : [];
J
Joao Moreno 已提交
120
			const forkOpts = Object.create(null);
E
Erich Gamma 已提交
121

J
Joao Moreno 已提交
122
			forkOpts.env = assign(clone(process.env), { 'VSCODE_PARENT_PID': String(process.pid) });
E
Erich Gamma 已提交
123

J
Joao Moreno 已提交
124 125 126
			if (this.options && this.options.env) {
				forkOpts.env = assign(forkOpts.env, this.options.env);
			}
E
Erich Gamma 已提交
127

J
Joao Moreno 已提交
128 129 130
			if (this.options && typeof this.options.debug === 'number') {
				forkOpts.execArgv = ['--nolazy', '--debug=' + this.options.debug];
			}
131

J
Joao Moreno 已提交
132 133
			if (this.options && typeof this.options.debugBrk === 'number') {
				forkOpts.execArgv = ['--nolazy', '--debug-brk=' + this.options.debugBrk];
134 135 136
			}

			this.child = fork(this.modulePath, args, forkOpts);
J
Joao Moreno 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

			const onMessageEmitter = new Emitter<any>();
			const onRawMessage = fromEventEmitter(this.child, 'message', msg => msg);

			onRawMessage(msg => {
				// Handle console logs specially
				if (msg && msg.type === '__$console') {
					let args = ['%c[IPC Library: ' + this.options.serverName + ']', 'color: darkgreen'];
					try {
						const parsed = JSON.parse(msg.arguments);
						args = args.concat(Object.getOwnPropertyNames(parsed).map(o => parsed[o]));
					} catch (error) {
						args.push(msg.arguments);
					}

					console[msg.severity].apply(console, args);
					return null;
				}

				// Anything else goes to the outside
				else {
					onMessageEmitter.fire(msg);
159 160
				}
			});
E
Erich Gamma 已提交
161

162 163
			const sender = this.options.useQueue ? createQueuedSender(this.child) : this.child;
			const send = r => this.child && this.child.connected && sender.send(r);
J
Joao Moreno 已提交
164 165 166 167 168
			const onMessage = onMessageEmitter.event;
			const protocol = { send, onMessage };

			this._client = new IPCClient(protocol);

169 170
			const onExit = () => this.disposeClient();
			process.once('exit', onExit);
E
Erich Gamma 已提交
171

172
			this.child.on('error', err => console.warn('IPC "' + this.options.serverName + '" errored with ' + err));
E
Erich Gamma 已提交
173

174 175
			this.child.on('exit', (code: any, signal: any) => {
				process.removeListener('exit', onExit);
E
Erich Gamma 已提交
176

177 178 179 180
				if (this.activeRequests) {
					this.activeRequests.forEach(req => req.cancel());
					this.activeRequests = [];
				}
E
Erich Gamma 已提交
181

J
Joao Moreno 已提交
182
				if (code !== 0 && signal !== 'SIGTERM') {
183 184 185 186
					console.warn('IPC "' + this.options.serverName + '" crashed with exit code ' + code);
					this.disposeDelayer.cancel();
					this.disposeClient();
				}
E
Erich Gamma 已提交
187 188 189 190 191 192 193 194
			});
		}

		return this._client;
	}

	private disposeClient() {
		if (this._client) {
195 196
			this.child.kill();
			this.child = null;
197
			this._client = null;
198
			this.channels = Object.create(null);
E
Erich Gamma 已提交
199 200 201 202 203 204 205 206 207 208
		}
	}

	dispose() {
		this.disposeDelayer.cancel();
		this.disposeDelayer = null;
		this.disposeClient();
		this.activeRequests = null;
	}
}