forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalDebugServer.ts
More file actions
88 lines (82 loc) · 3.89 KB
/
Copy pathLocalDebugServer.ts
File metadata and controls
88 lines (82 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
import * as net from 'net';
import { EOL } from 'os';
import { DebugSession, OutputEvent } from 'vscode-debugadapter';
import { IDebugServer, IPythonProcess, LaunchRequestArguments } from '../Common/Contracts';
import { BaseDebugServer } from './BaseDebugServer';
export class LocalDebugServer extends BaseDebugServer {
private debugSocketServer: net.Server | undefined;
constructor(debugSession: DebugSession, pythonProcess: IPythonProcess | undefined, private args: LaunchRequestArguments) {
super(debugSession, pythonProcess);
}
public Stop() {
if (!this.debugSocketServer) { return; }
try {
this.debugSocketServer.close();
// tslint:disable-next-line:no-empty
} catch { }
this.debugSocketServer = undefined;
}
public async Start(): Promise<IDebugServer> {
return new Promise<IDebugServer>((resolve, reject) => {
let connectedResolve = this.debugClientConnected.resolve.bind(this.debugClientConnected);
let connected = false;
let disconnected = false;
this.debugSocketServer = net.createServer(c => {
// "connection" listener
c.on('data', (buffer: Buffer) => {
if (connectedResolve) {
// The debug client has connected to the debug server
connectedResolve(true);
connectedResolve = null;
}
if (!connected) {
connected = this.pythonProcess.Connect(buffer, c, false);
} else {
this.pythonProcess.HandleIncomingData(buffer);
this.isRunning = true;
}
});
c.on('close', d => {
disconnected = true;
this.emit('detach', d);
});
c.on('timeout', d => {
const msg = `Debugger client timedout, ${d}`;
this.debugSession.sendEvent(new OutputEvent(`${msg}${EOL}`, 'stderr'));
});
c.on('error', ex => {
// Errors can be raised, when program has termined, but the adapter has
// sent an acknowledgement of the last message (LAST), however the socket client has ended.
if (connected || disconnected) {
return;
}
const msg = `There was an error in starting the debug server. Error = ${JSON.stringify(ex)}`;
this.debugSession.sendEvent(new OutputEvent(`${msg}${EOL}`, 'stderr'));
reject(msg);
});
});
this.debugSocketServer!.on('error', ex => {
const exMessage = JSON.stringify(ex);
let msg = '';
// tslint:disable-next-line:no-any
if ((ex as any).code === 'EADDRINUSE') {
msg = `The port used for debugging is in use, please try again or try restarting Visual Studio Code, Error = ${exMessage}`;
} else {
if (connected) {
return;
}
msg = `There was an error in starting the debug server. Error = ${exMessage}`;
}
this.debugSession.sendEvent(new OutputEvent(`${msg}${EOL}`, 'stderr'));
reject(msg);
});
const port = typeof this.args.port === 'number' ? this.args.port! : 0;
const host = typeof this.args.host === 'string' && this.args.host.trim().length > 0 ? this.args.host!.trim() : 'localhost';
this.debugSocketServer!.listen({ port, host }, () => {
const server = this.debugSocketServer!.address();
resolve({ port: server.port });
});
});
}
}