From 0a6219834838a382d1aee4219d1964e6e9fdc70d Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Fri, 5 Nov 2021 02:24:30 +0100 Subject: [PATCH] Persist the running state on disconnection instead of enabling. --- commands.lua | 24 ++++++++++++++++++------ server.lua | 36 +++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/commands.lua b/commands.lua index 84cdefc..7f756c8 100644 --- a/commands.lua +++ b/commands.lua @@ -272,7 +272,9 @@ return function(server) end local function processDisplayGet(command) + print("Taking screenshot") if command.apiVersion < 0x02 then + print("API Version error") server.errorResponse(server.errorType.INVALID_API_VERSION, command.requestId) return end @@ -280,6 +282,7 @@ return function(server) local infoLength = 13; local shot = emu.takeScreenshot() + print("Screenshot taken") local r = {} @@ -781,7 +784,15 @@ return function(server) command.type = server.readUint8(remainingHeader, 5) command.body = body - print(string.format("Command start: %02x", command.type)) + local prettyType = "" + for k, v in pairs(server.commandType) do + if v == command.type then + prettyType = k + break + end + end + + print(string.format("Command start: %02x (%s)", command.type, prettyType)) local ct = command.type @@ -851,11 +862,12 @@ return function(server) return end - server.running = false - - server.deregisterFrameCallback() - emu.breakExecution() - server.registerFrameCallback() + if server.running then + server.running = false + server.deregisterFrameCallback() + print("Break called by trap") + emu.breakExecution() + end end return me diff --git a/server.lua b/server.lua index 24e9cde..ffb2a63 100644 --- a/server.lua +++ b/server.lua @@ -206,8 +206,8 @@ local function initConnection() end local function prepareCommand() + initConnection() if me.conn == nil then - me.running = true return end @@ -222,7 +222,6 @@ local function prepareCommand() return elseif status == "closed" then me.conn = nil - me.running = true return end @@ -257,17 +256,11 @@ end me.deregisterFrameCallback = nil me.registerFrameCallback = nil local function frameHandle() - initConnection() - - if me.conn == nil then - return - end - prepareCommand() if not me.running then me.deregisterFrameCallback() + print("Break called by frame") emu.breakExecution() - me.registerFrameCallback() end end @@ -280,42 +273,55 @@ function me.deregisterFrameCallback() emu.removeEventCallback(frameCallback, emu.eventType.inputPolled) end +local lastTime = -1 local function breakHandle() - print("Break") + local pc = emu.getState().cpu.pc + print(string.format("PC: %04x", pc)) if me.stepping then me.stepping = false commands.monitorOpened() end + local now = socket.gettime() + if lastTime ~= -1 then + print("Took "..(now - lastTime)) + end + lastTime = now + print("Start loop") + print(tostring(me.running)) repeat prepareCommand() until me.running + print("End loop") + me.registerFrameCallback() emu.resume() end function me.start(host, port, waitForConnection) me.stepping = false - me.running = true print("Binding to host '" ..host.. "' and port " ..port.. "...") me.server = assert(socket.tcp()) assert(me.server:bind(host, port)) assert(me.server:listen(32)) + local waitType = "" if waitForConnection then + me.running = false me.server:settimeout(-1) + waitType = "Waiting" else + me.running = true me.server:settimeout(0) + waitType = "Listening" end - local i, p = me.server:getsockname() + local i, p = me.server:getsockname() assert(i, p) - print("Waiting for connection on " .. i .. ":" .. p .. "...") - - me.registerFrameCallback() + print(string.format("%s for connection on %s:%d...", waitType, i, p)) emu.addEventCallback(breakHandle, emu.eventType.codeBreak)