Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit c8f3b9d

Browse files
committed
Only disconnect socket io server on exit from port and not on restart
1 parent eb417fe commit c8f3b9d

File tree

6 files changed

+85
-33
lines changed

6 files changed

+85
-33
lines changed

src/debugger/debugAdapter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import { DebugAdapterTracker, DebugConsole, DebugSession } from "vscode";
2+
import { DebuggerCommunicationService } from "../service/debuggerCommunicationService";
23
import { MessagingService } from "../service/messagingService";
34
import { DEBUG_COMMANDS } from "../view/constants";
45

56
export class DebugAdapter implements DebugAdapterTracker {
67
private readonly console: DebugConsole | undefined;
78
private readonly messagingService: MessagingService;
9+
private debugCommunicationService: DebuggerCommunicationService;
810
constructor(
911
debugSession: DebugSession,
10-
messagingService: MessagingService
12+
messagingService: MessagingService,
13+
debugCommunicationService: DebuggerCommunicationService
1114
) {
1215
this.console = debugSession.configuration.console;
1316
this.messagingService = messagingService;
17+
this.debugCommunicationService = debugCommunicationService;
1418
}
1519
onWillStartSession() {
1620
// To Implement
1721
}
1822
onWillReceiveMessage(message: any): void {
23+
console.log(JSON.stringify(message));
1924
if (message.command) {
2025
// Only send pertinent debug messages
2126
switch (message.command) {
@@ -24,6 +29,12 @@ export class DebugAdapter implements DebugAdapterTracker {
2429
break;
2530
case DEBUG_COMMANDS.STACK_TRACE:
2631
this.messagingService.sendPauseMessage();
32+
break;
33+
case DEBUG_COMMANDS.DISCONNECT:
34+
// Triggered on stop event for debugger
35+
if (!message.arguments.restart) {
36+
this.debugCommunicationService.handleStopEvent();
37+
}
2738
}
2839
}
2940
}

src/debugger/debugAdapterFactory.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ import {
44
DebugSession,
55
ProviderResult,
66
} from "vscode";
7+
import { DebuggerCommunicationService } from "../service/debuggerCommunicationService";
78
import { MessagingService } from "../service/messagingService";
89
import { DebugAdapter } from "./debugAdapter";
910

1011
export class DebugAdapterFactory implements DebugAdapterTrackerFactory {
1112
private debugSession: DebugSession;
1213
private messagingService: MessagingService;
14+
private debugCommunicationService: DebuggerCommunicationService;
1315
constructor(
1416
debugSession: DebugSession,
15-
messagingService: MessagingService
17+
messagingService: MessagingService,
18+
debugCommunicationService: DebuggerCommunicationService
1619
) {
1720
this.debugSession = debugSession;
1821
this.messagingService = messagingService;
22+
this.debugCommunicationService = debugCommunicationService;
1923
}
2024
public createDebugAdapterTracker(
2125
session: DebugSession
2226
): ProviderResult<DebugAdapterTracker> {
23-
return new DebugAdapter(session, this.messagingService);
27+
return new DebugAdapter(
28+
session,
29+
this.messagingService,
30+
this.debugCommunicationService
31+
);
2432
}
2533
}

src/debuggerCommunicationServer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class DebuggerCommunicationServer {
4747

4848
// send the message to start closing the connection
4949
public closeConnection(): void {
50-
this.disconnectSocketIo();
50+
this.sendDisconnectEvent();
5151
}
5252

5353
public setWebview(webviewPanel: WebviewPanel | undefined) {
@@ -70,12 +70,12 @@ export class DebuggerCommunicationServer {
7070
this.isPendingResponse = true;
7171
}
7272
}
73-
public disconnectSocketIo() {
73+
public disconnectFromPort() {
74+
this.serverIo.close();
75+
this.serverHttp.close();
76+
}
77+
private sendDisconnectEvent() {
7478
this.serverIo.emit(DEBUGGER_MESSAGES.EMITTER.DISCONNECT, {});
75-
setTimeout(() => {
76-
this.serverIo.close();
77-
this.serverHttp.close();
78-
}, 100);
7979
}
8080

8181
private initHttpServer(): void {

src/extension.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { DebugAdapterFactory } from "./debugger/debugAdapterFactory";
2020
import { DebuggerCommunicationServer } from "./debuggerCommunicationServer";
2121
import * as utils from "./extension_utils/utils";
2222
import { SerialMonitor } from "./serialMonitor";
23+
import { DebuggerCommunicationService } from "./service/debuggerCommunicationService";
2324
import { MessagingService } from "./service/messagingService";
2425
import { SimulatorDebugConfigurationProvider } from "./simulatorDebugConfigurationProvider";
2526
import TelemetryAI from "./telemetry/telemetryAI";
@@ -32,12 +33,12 @@ let telemetryAI: TelemetryAI;
3233
let pythonExecutableName: string = "python";
3334
let configFileCreated: boolean = false;
3435
let inDebugMode: boolean = false;
35-
let debuggerCommunicationHandler: DebuggerCommunicationServer;
3636
// Notification booleans
3737
let firstTimeClosed: boolean = true;
3838
let shouldShowInvalidFileNamePopup: boolean = true;
3939
let shouldShowRunCodePopup: boolean = true;
4040
const messagingService = new MessagingService();
41+
const debuggerCommunicationService = new DebuggerCommunicationService();
4142

4243
let currentActiveDevice: string = DEFAULT_DEVICE;
4344

@@ -181,11 +182,11 @@ export async function activate(context: vscode.ExtensionContext) {
181182
console.log(`About to write ${messageJson} \n`);
182183
if (
183184
inDebugMode &&
184-
debuggerCommunicationHandler
185+
debuggerCommunicationService.getCurrentDebuggerServer()
185186
) {
186-
debuggerCommunicationHandler.emitInputChanged(
187-
messageJson
188-
);
187+
debuggerCommunicationService
188+
.getCurrentDebuggerServer()
189+
.emitInputChanged(messageJson);
189190
} else if (childProcess) {
190191
childProcess.stdin.write(
191192
messageJson + "\n"
@@ -228,11 +229,11 @@ export async function activate(context: vscode.ExtensionContext) {
228229
console.log(`Sensor changed ${messageJson} \n`);
229230
if (
230231
inDebugMode &&
231-
debuggerCommunicationHandler
232+
debuggerCommunicationService.getCurrentDebuggerServer()
232233
) {
233-
debuggerCommunicationHandler.emitInputChanged(
234-
messageJson
235-
);
234+
debuggerCommunicationService
235+
.getCurrentDebuggerServer()
236+
.emitInputChanged(messageJson);
236237
} else if (childProcess) {
237238
childProcess.stdin.write(
238239
messageJson + "\n"
@@ -271,8 +272,12 @@ export async function activate(context: vscode.ExtensionContext) {
271272
currentPanel.onDidDispose(
272273
() => {
273274
currentPanel = undefined;
274-
if (debuggerCommunicationHandler) {
275-
debuggerCommunicationHandler.setWebview(undefined);
275+
if (
276+
debuggerCommunicationService.getCurrentDebuggerServer()
277+
) {
278+
debuggerCommunicationService
279+
.getCurrentDebuggerServer()
280+
.setWebview(undefined);
276281
}
277282
killProcessIfRunning();
278283
if (firstTimeClosed) {
@@ -921,7 +926,8 @@ export async function activate(context: vscode.ExtensionContext) {
921926

922927
const debugAdapterFactory = new DebugAdapterFactory(
923928
vscode.debug.activeDebugSession,
924-
messagingService
929+
messagingService,
930+
debuggerCommunicationService
925931
);
926932
vscode.debug.registerDebugAdapterTrackerFactory(
927933
"python",
@@ -932,27 +938,29 @@ export async function activate(context: vscode.ExtensionContext) {
932938
if (simulatorDebugConfiguration.deviceSimulatorExpressDebug) {
933939
// Reinitialize process
934940
killProcessIfRunning();
935-
console.log("Debug Started");
936941
inDebugMode = true;
937942

938943
try {
939944
// Shut down existing server on debug restart
940-
if (debuggerCommunicationHandler) {
941-
debuggerCommunicationHandler.closeConnection();
942-
debuggerCommunicationHandler = undefined;
945+
if (debuggerCommunicationService.getCurrentDebuggerServer()) {
946+
debuggerCommunicationService.resetCurrentDebuggerServer();
943947
}
944948

945-
debuggerCommunicationHandler = new DebuggerCommunicationServer(
946-
currentPanel,
947-
utils.getServerPortConfig(),
948-
currentActiveDevice
949+
debuggerCommunicationService.setCurrentDebuggerServer(
950+
new DebuggerCommunicationServer(
951+
currentPanel,
952+
utils.getServerPortConfig(),
953+
currentActiveDevice
954+
)
949955
);
950956

951957
handleDebuggerTelemetry();
952958

953959
openWebview();
954960
if (currentPanel) {
955-
debuggerCommunicationHandler.setWebview(currentPanel);
961+
debuggerCommunicationService
962+
.getCurrentDebuggerServer()
963+
.setWebview(currentPanel);
956964
currentPanel.webview.postMessage({
957965
currentActiveDevice,
958966
command: "activate-play",
@@ -982,9 +990,8 @@ export async function activate(context: vscode.ExtensionContext) {
982990
console.log("Debug Stopped");
983991
inDebugMode = false;
984992
simulatorDebugConfiguration.deviceSimulatorExpressDebug = false;
985-
if (debuggerCommunicationHandler) {
986-
debuggerCommunicationHandler.closeConnection();
987-
debuggerCommunicationHandler = undefined;
993+
if (debuggerCommunicationService.getCurrentDebuggerServer()) {
994+
debuggerCommunicationService.resetCurrentDebuggerServer();
988995
}
989996
if (currentPanel) {
990997
currentPanel.webview.postMessage({
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { DebuggerCommunicationServer } from "../debuggerCommunicationServer";
2+
3+
export class DebuggerCommunicationService {
4+
private currentDebuggerServer: DebuggerCommunicationServer | undefined;
5+
6+
public setCurrentDebuggerServer(debugServer: DebuggerCommunicationServer) {
7+
this.currentDebuggerServer = debugServer;
8+
}
9+
// Used for restart and stop event
10+
public resetCurrentDebuggerServer() {
11+
if (this.currentDebuggerServer) {
12+
this.currentDebuggerServer.closeConnection();
13+
}
14+
this.currentDebuggerServer = undefined;
15+
}
16+
public getCurrentDebuggerServer() {
17+
return this.currentDebuggerServer;
18+
}
19+
// Only used for stop event
20+
public handleStopEvent() {
21+
if (this.currentDebuggerServer) {
22+
this.currentDebuggerServer.disconnectFromPort();
23+
}
24+
}
25+
}

src/view/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export enum VSCODE_MESSAGES_TO_WEBVIEW {
7878
export enum DEBUG_COMMANDS {
7979
STACK_TRACE = "stackTrace",
8080
CONTINUE = "continue",
81+
DISCONNECT = "disconnect",
8182
}
8283

8384
export default CONSTANTS;

0 commit comments

Comments
 (0)