CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/Debugger/WebSocket/WebSocketUtils.h
Views: 1401
// Copyright (c) 2018- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include "ppsspp_config.h"2021#include <string>2223#include "Common/Log.h"24#include "Common/Data/Format/JSONReader.h"25#include "Common/Data/Format/JSONWriter.h"26#include "Common/Net/WebsocketServer.h"2728#if PPSSPP_PLATFORM(UWP)29// Enum name overlapped with UWP macro, quick hack to disable it30#undef OPTIONAL31#endif3233using namespace json;3435struct WebSocketClientInfo {36WebSocketClientInfo() = default;3738std::string name;39std::string version;40std::map <std::string, bool> disallowed;41};4243struct DebuggerErrorEvent {44DebuggerErrorEvent(const std::string m, LogLevel l, const JsonGet data = JsonValue(JSON_NULL))45: message(m), level(l) {46// Need to format right away, before it's out of scope.47if (data) {48const JsonNode *value = data.get("ticket");49if (value)50ticketRaw = json_stringify(value);51}52}5354std::string message;55LogLevel level;56std::string ticketRaw;5758operator std::string() const {59JsonWriter j;60j.begin();61j.writeString("event", "error");62j.writeString("message", message);63j.writeInt("level", (int)level);64if (!ticketRaw.empty()) {65j.writeRaw("ticket", ticketRaw);66}67j.end();68return j.str();69}70};7172enum class DebuggerParamType {73REQUIRED,74OPTIONAL,75REQUIRED_LOOSE,76OPTIONAL_LOOSE,77};7879struct DebuggerRequest {80DebuggerRequest(const char *n, net::WebSocketServer *w, const JsonGet &d, WebSocketClientInfo *client_info)81: name(n), ws(w), data(d), client(client_info) {82}8384const char *name;85net::WebSocketServer *ws;86const JsonGet data;87WebSocketClientInfo *client;8889void Fail(const std::string &message) {90ws->Send(DebuggerErrorEvent(message, LogLevel::LERROR, data));91responseSent_ = true;92}9394bool HasParam(const char *name, bool ignoreNull = false);95bool ParamU32(const char *name, uint32_t *out, bool allowFloatBits = false, DebuggerParamType type = DebuggerParamType::REQUIRED);96bool ParamBool(const char *name, bool *out, DebuggerParamType type = DebuggerParamType::REQUIRED);97bool ParamString(const char *name, std::string *out, DebuggerParamType type = DebuggerParamType::REQUIRED);9899JsonWriter &Respond();100void Flush();101bool Finish();102103private:104JsonWriter writer_;105bool responseBegun_ = false;106bool responseSent_ = false;107bool responsePartial_ = false;108};109110class DebuggerSubscriber {111public:112virtual ~DebuggerSubscriber() {}113114// Subscribers can also broadcast if they have simple cases to.115virtual void Broadcast(net::WebSocketServer *ws) {}116};117118typedef std::function<void(DebuggerRequest &req)> DebuggerEventHandler;119typedef std::unordered_map<std::string, DebuggerEventHandler> DebuggerEventHandlerMap;120121uint32_t RoundMemAddressUp(uint32_t addr);122123124