Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "DapRpcMessage.h"
class DapRpcMessagePrivate : public QSharedData
{
public:
DapRpcMessagePrivate();
~DapRpcMessagePrivate();
DapRpcMessagePrivate(const DapRpcMessagePrivate &aDapRpcMessagePrivate);
void initializeWithObject(const QJsonObject &aMessage);
static DapRpcMessage createBasicRequest(const QString &asMethod, const QJsonArray &aParams);
static DapRpcMessage createBasicRequest(const QString &asMethod,
const QJsonObject &aNamedParameters);
DapRpcMessage::Type m_type;
QScopedPointer<QJsonObject> m_pObject;
static int uniqueRequestCounter;
};
int DapRpcMessagePrivate::uniqueRequestCounter = 0;
DapRpcMessagePrivate::DapRpcMessagePrivate()
: m_type(DapRpcMessage::Invalid),
m_pObject(nullptr)
{
}
DapRpcMessagePrivate::DapRpcMessagePrivate(const DapRpcMessagePrivate &aDapRpcMessagePrivate)
: QSharedData(aDapRpcMessagePrivate),
m_type(aDapRpcMessagePrivate.m_type),
m_pObject(aDapRpcMessagePrivate.m_pObject ? new QJsonObject(*aDapRpcMessagePrivate.m_pObject) : nullptr)
{
}
void DapRpcMessagePrivate::initializeWithObject(const QJsonObject &aMessage)
{
m_pObject.reset(new QJsonObject(aMessage));
if (aMessage.contains(QLatin1String("id"))) {
if (aMessage.contains(QLatin1String("result")) ||
aMessage.contains(QLatin1String("error"))) {
if (aMessage.contains(QLatin1String("error")) &&
!aMessage.value(QLatin1String("error")).isNull())
m_type = DapRpcMessage::Error;
else
m_type = DapRpcMessage::Response;
} else if (aMessage.contains(QLatin1String("method"))) {
m_type = DapRpcMessage::Request;
}
} else {
if (aMessage.contains(QLatin1String("method")))
m_type = DapRpcMessage::Notification;
}
}
DapRpcMessagePrivate::~DapRpcMessagePrivate()
{
}
DapRpcMessage::DapRpcMessage()
: d(new DapRpcMessagePrivate)
{
d->m_pObject.reset(new QJsonObject);
}
DapRpcMessage::DapRpcMessage(const DapRpcMessage &aDapRPCMessage)
: d(aDapRPCMessage.d)
{
}
DapRpcMessage::~DapRpcMessage()
{
}
DapRpcMessage &DapRpcMessage::operator=(const DapRpcMessage &aDapRPCMessage)
{
d = aDapRPCMessage.d;
return *this;
}
bool DapRpcMessage::operator==(const DapRpcMessage &aDapRpcMessage) const
{
if (aDapRpcMessage.d == d)
return true;
if (aDapRpcMessage.type() == type()) {
if (aDapRpcMessage.type() == DapRpcMessage::Error) {
return (aDapRpcMessage.errorCode() == errorCode() &&
aDapRpcMessage.errorMessage() == errorMessage() &&
aDapRpcMessage.errorData() == errorData());
} else {
if (aDapRpcMessage.type() == DapRpcMessage::Notification) {
return (aDapRpcMessage.method() == method() &&
aDapRpcMessage.params() == params());
} else {
return (aDapRpcMessage.id() == id() &&
aDapRpcMessage.method() == method() &&
aDapRpcMessage.params() == params());
}
}
}
return false;
}
DapRpcMessage DapRpcMessage::fromJson(const QByteArray &aData)
{
DapRpcMessage result;
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(aData, &error);
if (error.error != QJsonParseError::NoError) {
qJsonRpcDebug() << error.errorString();
return result;
}
if (!document.isObject()) {
qJsonRpcDebug() << "invalid message: " << aData;
return result;
}
result.d->initializeWithObject(document.object());
return result;
}
DapRpcMessage DapRpcMessage::fromObject(const QJsonObject &aObject)
{
DapRpcMessage result;
result.d->initializeWithObject(aObject);
return result;
}
QJsonObject DapRpcMessage::toObject() const
{
if (d->m_pObject)
return QJsonObject(*d->m_pObject);
return QJsonObject();
}
QByteArray DapRpcMessage::toJson() const
{
if (d->m_pObject) {
QJsonDocument doc(*d->m_pObject);
return doc.toJson();
}
return QByteArray();
}
bool DapRpcMessage::isValid() const
{
return d->m_type != DapRpcMessage::Invalid;
}
DapRpcMessage::Type DapRpcMessage::type() const
{
return d->m_type;
}
DapRpcMessage DapRpcMessagePrivate::createBasicRequest(const QString &asMethod, const QJsonArray &aParams)
{
DapRpcMessage request;
request.d->m_pObject->insert(QLatin1String("jsonrpc"), QLatin1String("2.0"));
request.d->m_pObject->insert(QLatin1String("method"), asMethod);
if (!aParams.isEmpty())
request.d->m_pObject->insert(QLatin1String("params"), aParams);
return request;
}
DapRpcMessage DapRpcMessagePrivate::createBasicRequest(const QString &asMethod,
const QJsonObject &aNamedParameters)
{
DapRpcMessage request;
request.d->m_pObject->insert(QLatin1String("jsonrpc"), QLatin1String("2.0"));
request.d->m_pObject->insert(QLatin1String("method"), asMethod);
if (!aNamedParameters.isEmpty())
request.d->m_pObject->insert(QLatin1String("params"), aNamedParameters);
return request;
}
DapRpcMessage DapRpcMessage::createRequest(const QString &asMethod, const QJsonArray &aParams)
{
DapRpcMessage request = DapRpcMessagePrivate::createBasicRequest(asMethod, aParams);
request.d->m_type = DapRpcMessage::Request;
DapRpcMessagePrivate::uniqueRequestCounter++;
request.d->m_pObject->insert(QLatin1String("id"), DapRpcMessagePrivate::uniqueRequestCounter);
return request;
}
DapRpcMessage DapRpcMessage::createRequest(const QString &asMethod, const QJsonValue &aParam)
{
QJsonArray params;
params.append(aParam);
return createRequest(asMethod, params);
}
DapRpcMessage DapRpcMessage::createRequest(const QString &asMethod,
const QJsonObject &aNamedParameters)
{
DapRpcMessage request =
DapRpcMessagePrivate::createBasicRequest(asMethod, aNamedParameters);
request.d->m_type = DapRpcMessage::Request;
DapRpcMessagePrivate::uniqueRequestCounter++;
request.d->m_pObject->insert(QLatin1String("id"), DapRpcMessagePrivate::uniqueRequestCounter);
return request;
}
DapRpcMessage DapRpcMessage::createRequest(const QString& asMethod, const QByteArray& aStream)
{
DapRpcMessage request = createRequest(asMethod, QJsonValue::fromVariant(aStream));
return request;
}
DapRpcMessage DapRpcMessage::createNotification(const QString &asMethod, const QJsonArray &aParams)
{
DapRpcMessage notification = DapRpcMessagePrivate::createBasicRequest(asMethod, aParams);
notification.d->m_type = DapRpcMessage::Notification;
return notification;
}
DapRpcMessage DapRpcMessage::createNotification(const QString &asMethod, const QJsonValue &aParam)
{
QJsonArray params;
params.append(aParam);
return createNotification(asMethod, params);
}
DapRpcMessage DapRpcMessage::createNotification(const QString &asMethod,
const QJsonObject &aNamedParameters)
{
DapRpcMessage notification =
DapRpcMessagePrivate::createBasicRequest(asMethod, aNamedParameters);
notification.d->m_type = DapRpcMessage::Notification;
return notification;
}
DapRpcMessage DapRpcMessage::createNotification(const QString& asMethod, const QByteArray& aStream)
{
DapRpcMessage notification = createNotification(asMethod, QJsonValue::fromVariant(aStream));
return notification;
}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
DapRpcMessage DapRpcMessage::createResponse(const QJsonValue &aResult) const
{
DapRpcMessage response;
if (d->m_pObject->contains(QLatin1String("id"))) {
QJsonObject *object = response.d->m_pObject.data();
object->insert(QLatin1String("jsonrpc"), QLatin1String("2.0"));
object->insert(QLatin1String("id"), d->m_pObject->value(QLatin1String("id")));
object->insert(QLatin1String("result"), aResult);
response.d->m_type = DapRpcMessage::Response;
}
return response;
}
DapRpcMessage DapRpcMessage::createErrorResponse(DapErrorCode aCode,
const QString &asMessage,
const QJsonValue &aData) const
{
DapRpcMessage response;
QJsonObject error;
error.insert(QLatin1String("code"), aCode);
if (!asMessage.isEmpty())
error.insert(QLatin1String("message"), asMessage);
if (!aData.isUndefined())
error.insert(QLatin1String("data"), aData);
response.d->m_type = DapRpcMessage::Error;
QJsonObject *object = response.d->m_pObject.data();
object->insert(QLatin1String("jsonrpc"), QLatin1String("2.0"));
if (d->m_pObject->contains(QLatin1String("id")))
object->insert(QLatin1String("id"), d->m_pObject->value(QLatin1String("id")));
else
object->insert(QLatin1String("id"), 0);
object->insert(QLatin1String("error"), error);
return response;
}
int DapRpcMessage::id() const
{
if (d->m_type == DapRpcMessage::Notification || !d->m_pObject)
return -1;
const QJsonValue &value = d->m_pObject->value(QLatin1String("id"));
if (value.isString())
return value.toString().toInt();
return value.toInt();
}
QString DapRpcMessage::method() const
{
if (d->m_type == DapRpcMessage::Response || !d->m_pObject)
return QString();
return d->m_pObject->value(QLatin1String("method")).toString();
}
QJsonValue DapRpcMessage::params() const
{
if (d->m_type == DapRpcMessage::Response || d->m_type == DapRpcMessage::Error)
return QJsonValue(QJsonValue::Undefined);
if (!d->m_pObject)
return QJsonValue(QJsonValue::Undefined);
return d->m_pObject->value(QLatin1String("params"));
}
QJsonValue DapRpcMessage::toJsonValue() const
{
if (d->m_type != DapRpcMessage::Response || !d->m_pObject)
return QJsonValue(QJsonValue::Undefined);
return d->m_pObject->value(QLatin1String("result"));
}
QByteArray DapRpcMessage::toByteArray() const
{
QJsonValue value = toJsonValue();
return QByteArray::fromHex(value.toVariant().toByteArray());
}
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
int DapRpcMessage::errorCode() const
{
if (d->m_type != DapRpcMessage::Error || !d->m_pObject)
return 0;
QJsonObject error =
d->m_pObject->value(QLatin1String("error")).toObject();
const QJsonValue &value = error.value(QLatin1String("code"));
if (value.isString())
return value.toString().toInt();
return value.toInt();
}
QString DapRpcMessage::errorMessage() const
{
if (d->m_type != DapRpcMessage::Error || !d->m_pObject)
return QString();
QJsonObject error =
d->m_pObject->value(QLatin1String("error")).toObject();
return error.value(QLatin1String("message")).toString();
}
QJsonValue DapRpcMessage::errorData() const
{
if (d->m_type != DapRpcMessage::Error || !d->m_pObject)
return QJsonValue(QJsonValue::Undefined);
QJsonObject error =
d->m_pObject->value(QLatin1String("error")).toObject();
return error.value(QLatin1String("data"));
}
static QDebug operator<<(QDebug dbg, DapRpcMessage::Type type)
{
switch (type) {
case DapRpcMessage::Request:
return dbg << "DapRpcMessage::Request";
case DapRpcMessage::Response:
return dbg << "DapRpcMessage::Response";
case DapRpcMessage::Notification:
return dbg << "DapRpcMessage::Notification";
case DapRpcMessage::Error:
return dbg << "DapRpcMessage::Error";
default:
return dbg << "DapRpcMessage::Invalid";
}
}
QDebug operator<<(QDebug dbg, const DapRpcMessage &msg)
{
dbg.nospace() << "DapRpcMessage(type=" << msg.type();
if (msg.type() != DapRpcMessage::Notification) {
dbg.nospace() << ", id=" << msg.id();
}
if (msg.type() == DapRpcMessage::Request ||
msg.type() == DapRpcMessage::Notification) {
dbg.nospace() << ", method=" << msg.method()
<< ", params=" << msg.params();
} else if (msg.type() == DapRpcMessage::Response) {
dbg.nospace() << ", result=" << msg.toJsonValue();
} else if (msg.type() == DapRpcMessage::Error) {
dbg.nospace() << ", code=" << msg.errorCode()
<< ", message=" << msg.errorMessage()
<< ", data=" << msg.errorData();
}
dbg.nospace() << ")";
return dbg.space();
}