-
Notifications
You must be signed in to change notification settings - Fork 30
/
vm-connection.ts
206 lines (173 loc) · 5.02 KB
/
vm-connection.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* jshint -W097 */
"use strict";
import * as WebSocket from "ws";
import { Controller } from "./controller";
import { Message, Respond, BreakpointData } from "./messages";
import { Activity } from "./execution-data";
const LOCAL_WS_URL = "ws://localhost";
/**
* Encapsulates the connection to the VM via a web socket and encodes
* the communication protocol, currently using JSON.
*/
export class VmConnection {
private socket: WebSocket;
private traceDataSocket: WebSocket;
private readonly useTraceData: boolean;
private controller: Controller;
constructor(useTraceData: boolean) {
this.socket = null;
this.traceDataSocket = null;
this.controller = null;
this.useTraceData = useTraceData;
}
public setController(controller: Controller) {
this.controller = controller;
}
public isConnected() {
return this.socket !== null && this.socket.readyState === WebSocket.OPEN;
}
private connectTraceDataSocket(tracePort: number) {
if (!this.useTraceData) { return; }
console.assert(this.traceDataSocket === null || this.traceDataSocket.readyState === WebSocket.CLOSED);
this.traceDataSocket = new WebSocket(LOCAL_WS_URL + ":" + tracePort);
(<any> this.traceDataSocket).binaryType = "arraybuffer"; // workaround, typescript doesn't recognize this property
const controller = this.controller;
this.traceDataSocket.onmessage = function(e) {
const data: DataView = new DataView(<ArrayBuffer> e.data);
controller.onTracingData(data);
};
}
protected onOpen() {
if (!this.controller) { return; }
this.controller.onConnect();
const ctrl = this.controller;
let s: any = this.socket;
s._socket.on("error", () => {
if (!ctrl) { return; }
ctrl.onError();
});
}
public connect() {
$.getJSON("ports.json", data => {
console.log(data);
this.connectWebSockets(data.dbgPort, data.tracePort);
});
}
protected connectWebSockets(dbgPort: number, tracePort: number) {
console.assert(this.socket === null || this.socket.readyState === WebSocket.CLOSED);
this.socket = new WebSocket(LOCAL_WS_URL + ":" + dbgPort);
const ctrl = this.controller;
this.socket.onopen = () => {
this.onOpen();
};
this.socket.onclose = () => {
if (!ctrl) { return; }
ctrl.onClose();
};
this.socket.onerror = () => {
if (!ctrl) { return; }
ctrl.onError();
};
this.socket.on("error", () => {
if (!ctrl) { return; }
ctrl.onError();
});
this.socket.onmessage = (e) => {
if (!ctrl) { return; }
const data: Message = JSON.parse(<string> e.data);
switch (data.type) {
case "source":
ctrl.onReceivedSource(data);
break;
case "StoppedMessage":
ctrl.onStoppedMessage(data);
break;
case "SymbolMessage":
ctrl.onSymbolMessage(data);
break;
case "StackTraceResponse":
ctrl.onStackTrace(data);
break;
case "ScopesResponse":
ctrl.onScopes(data);
break;
case "VariablesResponse":
ctrl.onVariables(data);
break;
case "ProgramInfoResponse":
ctrl.onProgramInfo(data);
break;
case "InitializationResponse":
ctrl.onInitializationResponse(data);
break;
default:
ctrl.onUnknownMessage(data);
break;
}
};
this.connectTraceDataSocket(tracePort);
}
public disconnect() {
if (this.socket) {
try {
this.socket.terminate();
} catch (_) { }
}
if (this.traceDataSocket) {
try {
this.traceDataSocket.terminate();
} catch (_) { }
}
}
public requestProgramInfo() {
this.send({ action: "ProgramInfoRequest" });
}
public requestTraceData() {
this.send({ action: "TraceDataRequest" });
}
public sendInitializeConnection(breakpoints: BreakpointData[]) {
this.send({
action: "InitializeConnection",
breakpoints: breakpoints
});
}
public updateBreakpoint(breakpoint: BreakpointData) {
this.send({
action: "updateBreakpoint",
breakpoint: breakpoint
});
};
public sendDebuggerAction(step: string, activity: Activity) {
this.send({
action: "StepMessage",
step: step,
activityId: activity.id
});
}
public requestStackTrace(activityId: number, requestId: number = 0) {
this.send({
action: "StackTraceRequest",
activityId: activityId,
startFrame: 0, // from the top
levels: 0, // request all
requestId: requestId
});
}
public requestScope(scopeId: number) {
this.send({
action: "ScopesRequest",
frameId: scopeId,
requestId: 0 // only used in VS code
});
}
public requestVariables(variablesReference: number) {
this.send({
action: "VariablesRequest",
variablesReference: variablesReference,
requestId: 0 // only used in VS code
});
}
private send(respond: Respond) {
this.socket.send(JSON.stringify(respond));
}
}