Refactor http server

This commit is contained in:
offtkp 2023-07-24 10:37:02 +03:00
parent 4a24a331da
commit 3a21661f45
5 changed files with 212 additions and 152 deletions

View file

@ -13,7 +13,11 @@ __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;
Emulator::Emulator()
: config(std::filesystem::current_path() / "config.toml"), kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory, config),
memory(cpu.getTicksRef()), cheats(memory, kernel.getServiceManager().getHID()) {
memory(cpu.getTicksRef()), cheats(memory, kernel.getServiceManager().getHID())
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
, httpServer(this)
#endif
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
Helpers::panic("Failed to initialize SDL2");
}
@ -100,10 +104,6 @@ void Emulator::step() {}
void Emulator::render() {}
void Emulator::run() {
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
httpServer.startHttpServer();
#endif
while (running) {
runFrame();
HIDService& hid = kernel.getServiceManager().getHID();
@ -337,7 +337,7 @@ void Emulator::run() {
void Emulator::runFrame() {
if (romType != ROMType::None) {
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
pollHttpServer();
httpServer.processActions();
#endif
cpu.runFrame(); // Run 1 frame of instructions
gpu.display(); // Display graphics
@ -448,37 +448,3 @@ bool Emulator::loadELF(std::ifstream& file) {
// Reset our graphics context and initialize the GPU's graphics context
void Emulator::initGraphicsContext() { gpu.initGraphicsContext(); }
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
void Emulator::pollHttpServer() {
std::scoped_lock lock(httpServer.actionMutex);
HIDService& hid = kernel.getServiceManager().getHID();
if (httpServer.pendingAction) {
switch (httpServer.action) {
case HttpAction::Screenshot: gpu.screenshot(HttpServer::httpServerScreenshotPath); break;
case HttpAction::PressKey:
if (httpServer.pendingKey != 0) {
hid.pressKey(httpServer.pendingKey);
httpServer.pendingKey = 0;
}
break;
case HttpAction::ReleaseKey:
if (httpServer.pendingKey != 0) {
hid.releaseKey(httpServer.pendingKey);
httpServer.pendingKey = 0;
}
break;
case HttpAction::None: break;
}
httpServer.action = HttpAction::None;
httpServer.pendingAction = false;
httpServer.pendingAction.notify_all();
}
}
#endif

View file

@ -2,131 +2,186 @@
#include "httpserver.hpp"
#include <fstream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <sstream>
#include "emulator.hpp"
#include "httplib.h"
#include "services/hid.hpp"
HttpServer::HttpServer() : keyMap(
{
{"A", { HID::Keys::A, false } },
{"B", { HID::Keys::B, false } },
{"Select", { HID::Keys::Select, false } },
{"Start", { HID::Keys::Start, false } },
{"Right", { HID::Keys::Right, false } },
{"Left", { HID::Keys::Left, false } },
{"Up", { HID::Keys::Up, false } },
{"Down", { HID::Keys::Down, false } },
{"R", { HID::Keys::R, false } },
{"L", { HID::Keys::L, false } },
{"X", { HID::Keys::X, false } },
{"Y", { HID::Keys::Y, false } },
class HttpActionScreenshot : public HttpAction {
public:
HttpActionScreenshot(DeferredResponseWrapper& response) : HttpAction(HttpActionType::Screenshot), response(response) {}
DeferredResponseWrapper& getResponse() { return response; }
private:
DeferredResponseWrapper& response;
};
class HttpActionKey : public HttpAction {
public:
HttpActionKey(uint32_t key, bool state) : HttpAction(HttpActionType::Key), key(key), state(state) {}
uint32_t getKey() const { return key; }
bool getState() const { return state; }
private:
uint32_t key;
bool state;
};
std::unique_ptr<HttpAction> HttpAction::createScreenshotAction(DeferredResponseWrapper& response) {
return std::make_unique<HttpActionScreenshot>(response);
}
std::unique_ptr<HttpAction> HttpAction::createKeyAction(uint32_t key, bool state) { return std::make_unique<HttpActionKey>(key, state); }
HttpServer::HttpServer(Emulator* emulator)
: emulator(emulator), server(std::make_unique<httplib::Server>()), keyMap({
{"A", {HID::Keys::A}},
{"B", {HID::Keys::B}},
{"Select", {HID::Keys::Select}},
{"Start", {HID::Keys::Start}},
{"Right", {HID::Keys::Right}},
{"Left", {HID::Keys::Left}},
{"Up", {HID::Keys::Up}},
{"Down", {HID::Keys::Down}},
{"R", {HID::Keys::R}},
{"L", {HID::Keys::L}},
{"X", {HID::Keys::X}},
{"Y", {HID::Keys::Y}},
}) {
httpServerThread = std::thread(&HttpServer::startHttpServer, this);
}
HttpServer::~HttpServer() {
printf("Stopping http server...\n");
server->stop();
if (httpServerThread.joinable()) {
httpServerThread.join();
}
) {}
}
void HttpServer::pushAction(std::unique_ptr<HttpAction> action) {
std::scoped_lock lock(actionQueueMutex);
actionQueue.push(std::move(action));
}
void HttpServer::startHttpServer() {
std::thread http_thread([this]() {
httplib::Server server;
server->Get("/ping", [](const httplib::Request&, httplib::Response& response) { response.set_content("pong", "text/plain"); });
server.Get("/ping", [](const httplib::Request&, httplib::Response& response) { response.set_content("pong", "text/plain"); });
server.Get("/screen", [this](const httplib::Request&, httplib::Response& response) {
{
std::scoped_lock lock(actionMutex);
pendingAction = true;
action = HttpAction::Screenshot;
}
// wait until the screenshot is ready
pendingAction.wait(true);
std::ifstream image(httpServerScreenshotPath, std::ios::binary);
std::vector<char> buffer(std::istreambuf_iterator<char>(image), {});
response.set_content(buffer.data(), buffer.size(), "image/png");
});
server.Get("/input", [this](const httplib::Request& request, httplib::Response& response) {
bool ok = false;
for (auto& [keyStr, value] : request.params) {
auto key = stringToKey(keyStr);
printf("Param: %s\n", keyStr.c_str());
if (key != 0) {
std::scoped_lock lock(actionMutex);
pendingAction = true;
pendingKey = key;
ok = true;
if (value == "1") {
action = HttpAction::PressKey;
setKeyState(keyStr, true);
} else if (value == "0") {
action = HttpAction::ReleaseKey;
setKeyState(keyStr, false);
} else {
// Should not happen but just in case
pendingAction = false;
ok = false;
}
// Not supporting multiple keys at once for now (ever?)
break;
}
}
if (ok) {
response.set_content("ok", "text/plain");
}
});
server.Get("/step", [this](const httplib::Request&, httplib::Response& response) {
// TODO: implement /step
response.set_content("ok", "text/plain");
});
server.Get("/status", [this](const httplib::Request&, httplib::Response& response) {
response.set_content(status(), "text/plain");
});
// TODO: ability to specify host and port
printf("Starting HTTP server on port 1234\n");
server.listen("localhost", 1234);
server->Get("/screen", [this](const httplib::Request&, httplib::Response& response) {
DeferredResponseWrapper wrapper(response);
// Lock the mutex before pushing the action to ensure that the condition variable is not notified before we wait on it
std::unique_lock lock(wrapper.mutex);
pushAction(HttpAction::createScreenshotAction(wrapper));
wrapper.cv.wait(lock, [&wrapper] { return wrapper.ready; });
});
http_thread.detach();
server->Get("/input", [this](const httplib::Request& request, httplib::Response& response) {
bool ok = false;
for (auto& [keyStr, value] : request.params) {
u32 key = stringToKey(keyStr);
if (key != 0) {
bool state = value == "1";
if (!state && value != "0") {
// Invalid state
ok = false;
break;
}
pushAction(HttpAction::createKeyAction(key, state));
ok = true;
} else {
// Invalid key
ok = false;
break;
}
}
if (ok) {
response.set_content("ok", "text/plain");
} else {
response.set_content("error", "text/plain");
}
});
server->Get("/step", [this](const httplib::Request&, httplib::Response& response) {
// TODO: implement /step
response.set_content("ok", "text/plain");
});
server->Get("/status", [this](const httplib::Request&, httplib::Response& response) { response.set_content(status(), "text/plain"); });
// TODO: ability to specify host and port
printf("Starting HTTP server on port 1234\n");
server->listen("localhost", 1234);
}
std::string HttpServer::status() {
HIDService& hid = emulator->kernel.getServiceManager().getHID();
std::stringstream stringStream;
stringStream << "Panda3DS\n";
stringStream << "Status: " << (paused ? "Paused" : "Running") << "\n";
for (auto& [keyStr, value] : keyMap) {
stringStream << keyStr << ": " << value.second << "\n";
stringStream << keyStr << ": " << hid.isPressed(value) << "\n";
}
return stringStream.str();
}
void HttpServer::processActions() {
std::scoped_lock lock(actionQueueMutex);
HIDService& hid = emulator->kernel.getServiceManager().getHID();
while (!actionQueue.empty()) {
std::unique_ptr<HttpAction> action = std::move(actionQueue.front());
actionQueue.pop();
switch (action->getType()) {
case HttpActionType::Screenshot: {
HttpActionScreenshot* screenshotAction = static_cast<HttpActionScreenshot*>(action.get());
emulator->gpu.screenshot(httpServerScreenshotPath);
std::ifstream file(httpServerScreenshotPath, std::ios::binary);
std::vector<char> buffer(std::istreambuf_iterator<char>(file), {});
DeferredResponseWrapper& response = screenshotAction->getResponse();
response.inner_response.set_content(buffer.data(), buffer.size(), "image/png");
std::unique_lock<std::mutex> lock(response.mutex);
response.ready = true;
response.cv.notify_one();
break;
}
case HttpActionType::Key: {
HttpActionKey* keyAction = static_cast<HttpActionKey*>(action.get());
if (keyAction->getState()) {
hid.pressKey(keyAction->getKey());
} else {
hid.releaseKey(keyAction->getKey());
}
break;
}
default: {
break;
}
}
}
}
u32 HttpServer::stringToKey(const std::string& key_name) {
if (keyMap.find(key_name) != keyMap.end()) {
return keyMap[key_name].first;
return keyMap[key_name];
}
return 0;
}
bool HttpServer::getKeyState(const std::string& key_name) {
if (keyMap.find(key_name) != keyMap.end()) {
return keyMap[key_name].second;
}
return false;
}
void HttpServer::setKeyState(const std::string& key_name, bool state) {
if (keyMap.find(key_name) != keyMap.end()) {
keyMap[key_name].second = state;
}
}
#endif // PANDA3DS_ENABLE_HTTP_SERVER