debugger:complete android part

This commit is contained in:
pengfeizhou
2021-02-21 01:51:59 +08:00
committed by osborn
parent 4c0237aec6
commit af31014e2e
16 changed files with 526 additions and 561 deletions

View File

@@ -6,13 +6,7 @@
"request": "launch",
"name": "Doric Debugger",
"program": "${workspaceFolder}/${relativeFile}",
"preLaunchTask": "Doric Build",
"sourceMaps": true,
"serverReadyAction": {
"pattern": "listening on port ([0-9]+)",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
},
"outFiles": [
"${workspaceFolder}/bundle/**/*.js"
]

View File

@@ -1,10 +1,5 @@
import fs from "fs";
import WebSocket from "ws"
import "colors";
import path from "path";
import { delay, glob } from "./util";
import { Shell } from "./shell";
export type MSG = {
type: "D2C" | "C2D" | "C2S" | "D2S" | "S2C" | "S2D",
@@ -13,59 +8,43 @@ export type MSG = {
}
export async function createServer() {
let contextId: string = "0"
let clientConnection: WebSocket | undefined = undefined
let debuggerConnection: WebSocket | undefined = undefined
let client: WebSocket | undefined = undefined;
let debug: WebSocket | undefined = undefined;
let deviceId = 0
return new WebSocket.Server({ port: 7777 })
const wss = new WebSocket.Server({ port: 7777 })
.on("connection", (ws, request) => {
let thisDeviceId: string
console.log('Connected', request.headers.host)
if (request.headers.host?.startsWith("localhost")) {
thisDeviceId = `Debugger#${deviceId++}`
console.log(`Debugger ${thisDeviceId} attached to dev kit`.green)
debuggerConnection = ws
clientConnection?.send(JSON.stringify({
cmd: 'SWITCH_TO_DEBUG',
contextId: contextId
}))
console.log(`${thisDeviceId} attached to dev kit`.green)
debug = ws;
} else {
thisDeviceId = `Client#${deviceId++}`
console.log(`${thisDeviceId} attached to dev kit`.green)
}
ws.on('message', async function (result: string) {
const resultObject = JSON.parse(result) as MSG
if (resultObject.type === "D2C" || resultObject.type === "C2D") {
ws.send(result);
if (resultObject.type === "D2C") {
if (client === undefined) {
wss?.clients.forEach(e => {
e.send(result);
})
} else {
client.send(result);
}
} else if (resultObject.type === "C2D") {
if (resultObject.cmd === "DEBUG_STOP") {
client = undefined;
}
if (client === undefined) {
client = ws;
} else if (client !== ws) {
console.log("Can only debugging one client at the same time.".red);
}
debug?.send(result);
} else if (resultObject.type === "C2S") {
switch (resultObject.cmd) {
case 'DEBUG':
clientConnection = ws;
(ws as any).debugging = true;
console.log("Enter debugging");
contextId = resultObject.payload.contextId;
const projectHome = '.';
await fs.promises.writeFile(path.resolve(projectHome, "build", "context"), contextId, "utf-8");
let source = resultObject.payload.source as string;
if (source.startsWith(".js")) {
source = source.replace(".js", ".ts");
} else if (!source.startsWith(".ts")) {
source = source + ".ts"
}
let sourceFile = path.resolve(projectHome, "src", source);
if (!fs.existsSync(sourceFile)) {
const tsFiles = await glob(source, {
cwd: path.resolve(projectHome, "src")
})
if (!!!tsFiles || tsFiles.length === 0) {
console.error(`Cannot find ${source} in ${path.resolve(projectHome)}`);
}
sourceFile = tsFiles[0];
}
console.log(thisDeviceId + " request debug, project home: " + projectHome);
await Shell.exec("code", [projectHome, sourceFile]);
await delay(1500);
break;
case 'EXCEPTION':
console.log(resultObject.payload.source.red);
console.log(resultObject.payload.exception.red);
@@ -87,19 +66,38 @@ export async function createServer() {
break
}
}
})
ws.on('connect', function (code: number) {
console.log('connect', code)
})
});
ws.on('close', function (code: number) {
console.log('close: code = ' + code, thisDeviceId)
console.log("quit debugging");
(ws as any).debugging = false
})
console.log('close: code = ' + code, thisDeviceId);
(ws as any).debugging = false;
if (ws === debug) {
console.log("quit debugging");
client?.send(JSON.stringify({
type: "S2C",
cmd: "DEBUG_STOP"
} as MSG));
}
if (ws === client) {
console.log("quit debugging");
client = undefined
}
});
ws.on('error', function (code: number) {
console.log('error', code)
})
})
if (ws === debug) {
console.log("quit debugging");
client?.send(JSON.stringify({
type: "S2C",
cmd: "DEBUG_STOP"
} as MSG));
}
if (ws === client) {
console.log("quit debugging");
client = undefined
}
});
});
return wss;
}