CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/WebSocket/WebSocketUtils.h
Views: 1401
1
// Copyright (c) 2018- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include "ppsspp_config.h"
21
22
#include <string>
23
24
#include "Common/Log.h"
25
#include "Common/Data/Format/JSONReader.h"
26
#include "Common/Data/Format/JSONWriter.h"
27
#include "Common/Net/WebsocketServer.h"
28
29
#if PPSSPP_PLATFORM(UWP)
30
// Enum name overlapped with UWP macro, quick hack to disable it
31
#undef OPTIONAL
32
#endif
33
34
using namespace json;
35
36
struct WebSocketClientInfo {
37
WebSocketClientInfo() = default;
38
39
std::string name;
40
std::string version;
41
std::map <std::string, bool> disallowed;
42
};
43
44
struct DebuggerErrorEvent {
45
DebuggerErrorEvent(const std::string m, LogLevel l, const JsonGet data = JsonValue(JSON_NULL))
46
: message(m), level(l) {
47
// Need to format right away, before it's out of scope.
48
if (data) {
49
const JsonNode *value = data.get("ticket");
50
if (value)
51
ticketRaw = json_stringify(value);
52
}
53
}
54
55
std::string message;
56
LogLevel level;
57
std::string ticketRaw;
58
59
operator std::string() const {
60
JsonWriter j;
61
j.begin();
62
j.writeString("event", "error");
63
j.writeString("message", message);
64
j.writeInt("level", (int)level);
65
if (!ticketRaw.empty()) {
66
j.writeRaw("ticket", ticketRaw);
67
}
68
j.end();
69
return j.str();
70
}
71
};
72
73
enum class DebuggerParamType {
74
REQUIRED,
75
OPTIONAL,
76
REQUIRED_LOOSE,
77
OPTIONAL_LOOSE,
78
};
79
80
struct DebuggerRequest {
81
DebuggerRequest(const char *n, net::WebSocketServer *w, const JsonGet &d, WebSocketClientInfo *client_info)
82
: name(n), ws(w), data(d), client(client_info) {
83
}
84
85
const char *name;
86
net::WebSocketServer *ws;
87
const JsonGet data;
88
WebSocketClientInfo *client;
89
90
void Fail(const std::string &message) {
91
ws->Send(DebuggerErrorEvent(message, LogLevel::LERROR, data));
92
responseSent_ = true;
93
}
94
95
bool HasParam(const char *name, bool ignoreNull = false);
96
bool ParamU32(const char *name, uint32_t *out, bool allowFloatBits = false, DebuggerParamType type = DebuggerParamType::REQUIRED);
97
bool ParamBool(const char *name, bool *out, DebuggerParamType type = DebuggerParamType::REQUIRED);
98
bool ParamString(const char *name, std::string *out, DebuggerParamType type = DebuggerParamType::REQUIRED);
99
100
JsonWriter &Respond();
101
void Flush();
102
bool Finish();
103
104
private:
105
JsonWriter writer_;
106
bool responseBegun_ = false;
107
bool responseSent_ = false;
108
bool responsePartial_ = false;
109
};
110
111
class DebuggerSubscriber {
112
public:
113
virtual ~DebuggerSubscriber() {}
114
115
// Subscribers can also broadcast if they have simple cases to.
116
virtual void Broadcast(net::WebSocketServer *ws) {}
117
};
118
119
typedef std::function<void(DebuggerRequest &req)> DebuggerEventHandler;
120
typedef std::unordered_map<std::string, DebuggerEventHandler> DebuggerEventHandlerMap;
121
122
uint32_t RoundMemAddressUp(uint32_t addr);
123
124