-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathproto.cpp
150 lines (124 loc) · 3.61 KB
/
proto.cpp
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
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2023 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "proto.h"
#include <tbox/base/json.hpp>
#include <tbox/base/assert.h>
#include <tbox/util/json.h>
namespace tbox {
namespace jsonrpc {
void Proto::sendRequest(int id, const std::string &method, const Json &js_params)
{
Json js = {
{"jsonrpc", "2.0"},
{"method", method}
};
if (id != 0)
js["id"] = id;
if (!js_params.is_null())
js["params"] = js_params;
sendJson(js);
}
void Proto::sendRequest(int id, const std::string &method)
{
sendRequest(id, method, Json());
}
void Proto::sendResult(int id, const Json &js_result)
{
Json js = {
{"jsonrpc", "2.0"},
{"id", id},
{"result", js_result}
};
sendJson(js);
}
void Proto::sendError(int id, int errcode, const std::string &message)
{
Json js = {
{"jsonrpc", "2.0"},
{"id", id},
{"error", { {"code", errcode } } }
};
if (!message.empty())
js["error"]["message"] = message;
sendJson(js);
}
void Proto::setRecvCallback(RecvRequestCallback &&req_cb, RecvRespondCallback &&rsp_cb)
{
recv_request_cb_ = std::move(req_cb);
recv_respond_cb_ = std::move(rsp_cb);
}
void Proto::setSendCallback(SendDataCallback &&cb)
{
send_data_cb_ = std::move(cb);
}
void Proto::onRecvJson(const Json &js)
{
if (js.is_object()) {
std::string version;
if (!util::json::GetField(js, "jsonrpc", version) || version != "2.0") {
LogNotice("no jsonrpc field, or version not match");
return;
}
if (js.contains("method")) {
if (!recv_request_cb_)
return;
//! 按请求进行处理
std::string method;
int id = 0;
if (!util::json::GetField(js, "method", method)) {
LogNotice("method type not string");
return;
}
util::json::GetField(js, "id", id);
recv_request_cb_(id, method, js.contains("params") ? js["params"] : Json());
} else if (js.contains("result")) {
//! 按结果回复进行处理
if (!recv_respond_cb_)
return;
int id = 0;
if (!util::json::GetField(js, "id", id)) {
LogNotice("no method field in respond");
return;
}
recv_respond_cb_(id, 0, js["result"]);
} else if (js.contains("error")) {
//! 按错误回复进行处理
if (!recv_respond_cb_)
return;
int id = 0;
util::json::GetField(js, "id", id);
auto &js_error = js["error"];
int errcode = 0;
if (!util::json::GetField(js_error, "code", errcode)) {
LogNotice("no code field in error");
return;
}
recv_respond_cb_(id, errcode, Json());
} else {
LogNotice("not jsonrpc format");
}
} else if (js.is_array()) {
for (auto &js_item : js) {
onRecvJson(js_item);
}
}
}
}
}