Bonk http server

This commit is contained in:
wheremyfoodat 2023-07-26 02:11:14 +03:00
parent 3a21661f45
commit 67a08a71f6
3 changed files with 25 additions and 35 deletions

View file

@ -18,7 +18,7 @@ class Emulator;
namespace httplib { namespace httplib {
class Server; class Server;
class Response; class Response;
} // namespace httplib }
// Wrapper for httplib::Response that allows the HTTP server to wait for the response to be ready // Wrapper for httplib::Response that allows the HTTP server to wait for the response to be ready
struct DeferredResponseWrapper { struct DeferredResponseWrapper {
@ -32,6 +32,8 @@ struct DeferredResponseWrapper {
// Actions derive from this class and are used to communicate with the HTTP server // Actions derive from this class and are used to communicate with the HTTP server
class HttpAction { class HttpAction {
HttpActionType type;
public: public:
HttpAction(HttpActionType type) : type(type) {} HttpAction(HttpActionType type) : type(type) {}
virtual ~HttpAction() = default; virtual ~HttpAction() = default;
@ -40,22 +42,17 @@ class HttpAction {
static std::unique_ptr<HttpAction> createScreenshotAction(DeferredResponseWrapper& response); static std::unique_ptr<HttpAction> createScreenshotAction(DeferredResponseWrapper& response);
static std::unique_ptr<HttpAction> createKeyAction(uint32_t key, bool state); static std::unique_ptr<HttpAction> createKeyAction(uint32_t key, bool state);
private:
HttpActionType type;
}; };
struct HttpServer { struct HttpServer {
HttpServer(Emulator* emulator); HttpServer(Emulator* emulator);
~HttpServer(); ~HttpServer();
void processActions(); void processActions();
private: private:
static constexpr const char* httpServerScreenshotPath = "screenshot.png"; static constexpr const char* httpServerScreenshotPath = "screenshot.png";
Emulator* emulator; Emulator* emulator;
std::unique_ptr<httplib::Server> server; std::unique_ptr<httplib::Server> server;
std::thread httpServerThread; std::thread httpServerThread;

View file

@ -90,11 +90,10 @@ class HIDService {
void pressKey(u32 mask) { newButtons |= mask; } void pressKey(u32 mask) { newButtons |= mask; }
void releaseKey(u32 mask) { newButtons &= ~mask; } void releaseKey(u32 mask) { newButtons &= ~mask; }
bool isPressed(u32 mask) { return (oldButtons & mask) != 0; }
u32 getOldButtons() { return oldButtons; } u32 getOldButtons() const { return oldButtons; }
s16 getCirclepadX() { return circlePadX; } s16 getCirclepadX() const { return circlePadX; }
s16 getCirclepadY() { return circlePadY; } s16 getCirclepadY() const { return circlePadY; }
void setCirclepadX(s16 x) { void setCirclepadX(s16 x) {
circlePadX = x; circlePadX = x;

View file

@ -8,35 +8,33 @@
#include <vector> #include <vector>
#include "emulator.hpp" #include "emulator.hpp"
#include "helpers.hpp"
#include "httplib.h" #include "httplib.h"
class HttpActionScreenshot : public HttpAction { class HttpActionScreenshot : public HttpAction {
DeferredResponseWrapper& response;
public: public:
HttpActionScreenshot(DeferredResponseWrapper& response) : HttpAction(HttpActionType::Screenshot), response(response) {} HttpActionScreenshot(DeferredResponseWrapper& response) : HttpAction(HttpActionType::Screenshot), response(response) {}
DeferredResponseWrapper& getResponse() { return response; } DeferredResponseWrapper& getResponse() { return response; }
private:
DeferredResponseWrapper& response;
}; };
class HttpActionKey : public HttpAction { class HttpActionKey : public HttpAction {
public: u32 key;
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; bool state;
public:
HttpActionKey(u32 key, bool state) : HttpAction(HttpActionType::Key), key(key), state(state) {}
u32 getKey() const { return key; }
bool getState() const { return state; }
}; };
std::unique_ptr<HttpAction> HttpAction::createScreenshotAction(DeferredResponseWrapper& response) { std::unique_ptr<HttpAction> HttpAction::createScreenshotAction(DeferredResponseWrapper& response) {
return std::make_unique<HttpActionScreenshot>(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); } std::unique_ptr<HttpAction> HttpAction::createKeyAction(u32 key, bool state) { return std::make_unique<HttpActionKey>(key, state); }
HttpServer::HttpServer(Emulator* emulator) HttpServer::HttpServer(Emulator* emulator)
: emulator(emulator), server(std::make_unique<httplib::Server>()), keyMap({ : emulator(emulator), server(std::make_unique<httplib::Server>()), keyMap({
@ -86,8 +84,7 @@ void HttpServer::startHttpServer() {
u32 key = stringToKey(keyStr); u32 key = stringToKey(keyStr);
if (key != 0) { if (key != 0) {
bool state = value == "1"; bool state = (value == "1");
if (!state && value != "0") { if (!state && value != "0") {
// Invalid state // Invalid state
ok = false; ok = false;
@ -103,11 +100,7 @@ void HttpServer::startHttpServer() {
} }
} }
if (ok) { response.set_content(ok ? "ok" : "error", "text/plain");
response.set_content("ok", "text/plain");
} else {
response.set_content("error", "text/plain");
}
}); });
server->Get("/step", [this](const httplib::Request&, httplib::Response& response) { server->Get("/step", [this](const httplib::Request&, httplib::Response& response) {
@ -124,14 +117,15 @@ void HttpServer::startHttpServer() {
std::string HttpServer::status() { std::string HttpServer::status() {
HIDService& hid = emulator->kernel.getServiceManager().getHID(); HIDService& hid = emulator->kernel.getServiceManager().getHID();
std::stringstream stringStream; std::stringstream stringStream;
stringStream << "Panda3DS\n"; stringStream << "Panda3DS\n";
stringStream << "Status: " << (paused ? "Paused" : "Running") << "\n"; stringStream << "Status: " << (paused ? "Paused" : "Running") << "\n";
// TODO: This currently doesn't work for N3DS buttons
auto keyPressed = [](const HIDService& hid, u32 mask) { return (hid.getOldButtons() & mask) != 0; };
for (auto& [keyStr, value] : keyMap) { for (auto& [keyStr, value] : keyMap) {
stringStream << keyStr << ": " << hid.isPressed(value) << "\n"; stringStream << keyStr << ": " << keyPressed(hid, value) << "\n";
} }
return stringStream.str(); return stringStream.str();
@ -160,6 +154,7 @@ void HttpServer::processActions() {
response.cv.notify_one(); response.cv.notify_one();
break; break;
} }
case HttpActionType::Key: { case HttpActionType::Key: {
HttpActionKey* keyAction = static_cast<HttpActionKey*>(action.get()); HttpActionKey* keyAction = static_cast<HttpActionKey*>(action.get());
if (keyAction->getState()) { if (keyAction->getState()) {
@ -169,9 +164,8 @@ void HttpServer::processActions() {
} }
break; break;
} }
default: {
break; default: break;
}
} }
} }
} }