forked from freewayz/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalDebugServer.ts
More file actions
77 lines (71 loc) · 2.97 KB
/
Copy pathLocalDebugServer.ts
File metadata and controls
77 lines (71 loc) · 2.97 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
"use strict";
import { DebugSession, OutputEvent } from "vscode-debugadapter";
import { IPythonProcess, IDebugServer } from "../Common/Contracts";
import * as net from "net";
import { BaseDebugServer } from "./BaseDebugServer";
export class LocalDebugServer extends BaseDebugServer {
private debugSocketServer: net.Server = null;
constructor(debugSession: DebugSession, pythonProcess: IPythonProcess) {
super(debugSession, pythonProcess);
}
public Stop() {
if (this.debugSocketServer === null) { return; }
try {
this.debugSocketServer.close();
}
catch (ex) { }
this.debugSocketServer = null;
}
public Start(): Promise<IDebugServer> {
return new Promise<IDebugServer>((resolve, reject) => {
let that = this;
let connectedResolve = this.debugClientConnected.resolve.bind(this.debugClientConnected);
let connected = 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 = that.pythonProcess.Connect(buffer, c, false);
}
else {
that.pythonProcess.HandleIncomingData(buffer);
that.isRunning = true;
}
});
c.on("close", d => {
that.emit("detach", d);
});
c.on("timeout", d => {
let msg = "Debugger client timedout, " + d;
that.debugSession.sendEvent(new OutputEvent(msg + "\n", "stderr"));
});
});
this.debugSocketServer.on("error", ex => {
let exMessage = JSON.stringify(ex);
let msg = "";
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) {
// Under what circumstance does this happen?
// Needs to be documented
return;
}
msg = `There was an error in starting the debug server. Error = ${exMessage}`;
}
that.debugSession.sendEvent(new OutputEvent(msg + "\n", "stderr"));
reject(msg);
});
this.debugSocketServer.listen(0, () => {
let server = that.debugSocketServer.address();
resolve({ port: server.port });
});
});
}
}