diff --git a/bsnes/target-bsnes/program/program.hpp b/bsnes/target-bsnes/program/program.hpp index 1b0483ce..62197dd7 100644 --- a/bsnes/target-bsnes/program/program.hpp +++ b/bsnes/target-bsnes/program/program.hpp @@ -213,11 +213,13 @@ public: // rpc.cpp struct RpcCommandType { enum : uint { - SaveStateToFile = 1 << 1 + SaveStateToFile = 1 << 1, + Pause = 1 << 2, }; }; struct RpcCommand { - RpcCommandType type; + public: + uint type; string arg; }; diff --git a/bsnes/target-bsnes/program/rpc.cpp b/bsnes/target-bsnes/program/rpc.cpp index 8a050c9d..50aae8e4 100644 --- a/bsnes/target-bsnes/program/rpc.cpp +++ b/bsnes/target-bsnes/program/rpc.cpp @@ -2,16 +2,50 @@ auto Program::startRpcListener() -> void { rpcServer.main([&](nall::HTTP::Request& rq) -> nall::HTTP::Response { - OutputDebugStringA("GOT REQUEST\n"); nall::HTTP::Response resp; - resp.setResponseType(200); - resp.setText("hooray!"); - resp.header.append("Content-Type", "text/plain"); + if (rq.requestType() != nall::HTTP::Request::RequestType::Post) { + resp.setResponseType(200); + resp.header.append("Content-Type", "text/plain"); + resp.setText("no"); + + return resp; + } + + rq.body([&](const uint8_t *data, nall::uint size) -> bool { + if (size > 16*1024) { + resp.setResponseType(400); + return true; + } + + string content((char*)data); + + auto idx = content.find("|"); + if (!idx) { + resp.setResponseType(400); + resp.setText("Invalid Command!!\n"); + resp.header.append("Content-Type", "text/plain"); + return true; + } + + auto cmd = content.slice(0, idx.get()); + auto arg = content.slice(idx.get() + 1); + RpcCommand rpcCmd; + if (cmd == "pau") { + rpcCmd.type = RpcCommandType::Pause; + pendingRpcCommands.write(rpcCmd); + } + + resp.setResponseType(200); + resp.setText("ok\n"); + resp.header.append("Content-Type", "text/plain"); + + return true; + }); + return resp; }); - OutputDebugStringA("SETTING UP SERVER\n"); rpcServer.open(); } @@ -21,4 +55,16 @@ auto Program::stopRpcListener() -> void { auto Program::processRpcCommands() -> void { rpcServer.scan(); + + RpcCommand cmd; + if (pendingRpcCommands.read(cmd)) { + if (cmd.type == RpcCommandType::Pause) { + if (presentation.runEmulation.checked()) { + presentation.pauseEmulation.setChecked().doActivate(); + } else { + // unpausing can also cancel frame advance mode + presentation.runEmulation.setChecked().doActivate(); + } + } + } } \ No newline at end of file