#pragma once #include #include #include #include namespace whetstone { class DAPProxyServer { public: using Handler = std::function; DAPProxyServer() { auto empty = [](const nlohmann::json&) -> nlohmann::json { return nlohmann::json::object(); }; for (const auto& cmd : {"initialize","launch","attach","setBreakpoints", "stackTrace","scopes","variables","continue", "next","stepIn","stepOut","disconnect"}) handlers_[cmd] = empty; } nlohmann::json handle(const nlohmann::json& msg) { if (!msg.contains("type")) return {}; std::string type = msg["type"].get(); // Events: no response if (type == "event") return {}; // Requests if (type == "request") { std::string command = msg.value("command", ""); int reqSeq = msg.value("seq", 0); nlohmann::json args = msg.contains("arguments") ? msg["arguments"] : nlohmann::json::object(); nlohmann::json body = nlohmann::json::object(); auto it = handlers_.find(command); if (it != handlers_.end()) body = it->second(args); return { {"seq", ++seq_}, {"type", "response"}, {"request_seq", reqSeq}, {"command", command}, {"success", true}, {"body", body} }; } return {}; } void registerHandler(const std::string& command, Handler h) { handlers_[command] = std::move(h); } private: std::map handlers_; int seq_ = 0; }; } // namespace whetstone