Skip to content

Commit b83a8cf

Browse files
author
chunjun.li
committed
feat(jsonrpc):1.13.1
1. 实现在返回错误的时候,带上message字段 2. 调整文件结构
1 parent eedd14b commit b83a8cf

File tree

17 files changed

+182
-130
lines changed

17 files changed

+182
-130
lines changed

examples/jsonrpc/message/ping/ping.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ using namespace tbox;
4545

4646
int main(int argc, char **argv)
4747
{
48-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
48+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4949
if (argc >= 2) {
5050
std::string type_str(argv[1]);
5151
if (type_str == "str") {
52-
id_type = jsonrpc::Rpc::IdType::kString;
52+
id_type = jsonrpc::IdType::kString;
5353
} else if (type_str != "int") {
5454
std::cout << "id_type invalid!" << std::endl
5555
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -115,7 +115,7 @@ int main(int argc, char **argv)
115115
};
116116

117117
//! 定义收到pong的动作
118-
rpc.addService("pong", [&] (int id, const Json &js_params, int &, Json &) {
118+
rpc.addService("pong", [&] (int id, const Json &js_params, tbox::jsonrpc::Response &) {
119119
int pong_count = 0;
120120
util::json::GetField(js_params, "count", pong_count);
121121
send_ping();

examples/jsonrpc/message/pong/pong.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ using namespace tbox;
4242

4343
int main(int argc, char **argv)
4444
{
45-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
45+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4646
if (argc >= 2) {
4747
std::string type_str(argv[1]);
4848
if (type_str == "str") {
49-
id_type = jsonrpc::Rpc::IdType::kString;
49+
id_type = jsonrpc::IdType::kString;
5050
} else if (type_str != "int") {
5151
std::cout << "id_type invalid!" << std::endl
5252
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -111,7 +111,7 @@ int main(int argc, char **argv)
111111
tcp_server.start(); //! 启动tcp服务
112112

113113
//! 注册ping的服务处理函数
114-
rpc.addService("ping", [&] (int id, const Json &js_params, int &, Json &) {
114+
rpc.addService("ping", [&] (int id, const Json &js_params, tbox::jsonrpc::Response &) {
115115
int ping_count = 0;
116116
util::json::GetField(js_params, "count", ping_count);
117117
LogDbg("id: %d, got ping_count: %d", id, ping_count);

examples/jsonrpc/req_rsp/ping/ping.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ using namespace tbox;
4545

4646
int main(int argc, char **argv)
4747
{
48-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
48+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4949
if (argc >= 2) {
5050
std::string type_str(argv[1]);
5151
if (type_str == "str") {
52-
id_type = jsonrpc::Rpc::IdType::kString;
52+
id_type = jsonrpc::IdType::kString;
5353
} else if (type_str != "int") {
5454
std::cout << "id_type invalid!" << std::endl
5555
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -111,14 +111,15 @@ int main(int argc, char **argv)
111111

112112
//! 发送ping请求,并在收到回复后,进行下一个ping动作
113113
rpc.request("ping", js_params,
114-
[&] (int errcode, const Json &js_result) {
115-
int pong_count = 0;
116-
util::json::GetField(js_result, "count", pong_count);
117-
if (errcode == 0) {
114+
[&] (const tbox::jsonrpc::Response &r) {
115+
if (r.error.code == 0) {
116+
int pong_count = 0;
117+
util::json::GetField(r.js_result, "count", pong_count);
118118
LogDbg("got pong: %d", pong_count);
119119
send_ping();
120-
} else
121-
LogNotice("got erro: %d", errcode);
120+
} else {
121+
LogNotice("got erro: %d", r.error.code);
122+
}
122123
});
123124
LogDbg("send ping: %d", ping_count);
124125
};

examples/jsonrpc/req_rsp/pong/pong.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ using namespace tbox;
4242

4343
int main(int argc, char **argv)
4444
{
45-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
45+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4646
if (argc >= 2) {
4747
std::string type_str(argv[1]);
4848
if (type_str == "str") {
49-
id_type = jsonrpc::Rpc::IdType::kString;
49+
id_type = jsonrpc::IdType::kString;
5050
} else if (type_str != "int") {
5151
std::cout << "id_type invalid!" << std::endl
5252
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -111,11 +111,11 @@ int main(int argc, char **argv)
111111
tcp_server.start(); //! 启动tcp服务
112112

113113
//! 注册ping的服务处理函数
114-
rpc.addService("ping", [&] (int id, const Json &js_params, int &errcode, Json &js_result) {
114+
rpc.addService("ping", [&] (int id, const Json &js_params, tbox::jsonrpc::Response &r) {
115115
int ping_count = 0;
116116
util::json::GetField(js_params, "count", ping_count);
117117
LogDbg("id:%d, got ping_count: %d", id, ping_count);
118-
js_result = js_params;
118+
r.js_result = js_params;
119119
return true; //! 表示在函数返回后立即发送回复
120120
});
121121

modules/jsonrpc/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ install(
7272

7373
# install header file
7474
install(
75-
FILES proto.h rpc.h
75+
FILES types.h proto.h rpc.h
7676
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tbox/jsonrpc
7777
)
7878

modules/jsonrpc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ LIB_VERSION_Y = 0
2525
LIB_VERSION_Z = 1
2626

2727
HEAD_FILES = \
28+
types.h \
2829
proto.h \
2930
protos/raw_stream_proto.h \
3031
protos/header_stream_proto.h \

modules/jsonrpc/proto.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,24 @@ void Proto::handleAsResult(const Json &js) const
229229
return;
230230
}
231231

232-
auto &js_result = js["result"];
232+
Response response;
233+
response.js_result = std::move(js["result"]);
233234

234235
if (id_type_ == IdType::kInt) {
235236
int id = 0;
236237
if (!util::json::GetField(js, "id", id)) {
237238
LogNotice("id type not int in respond");
238239
return;
239240
}
240-
recv_respond_int_cb_(id, 0, js_result);
241+
recv_respond_int_cb_(id, response);
241242

242243
} else if (id_type_ == IdType::kString) {
243244
std::string id;
244245
if (!util::json::GetField(js, "id", id)) {
245246
LogNotice("id type not string in respond");
246247
return;
247248
}
248-
recv_respond_str_cb_(id, 0, js_result);
249+
recv_respond_str_cb_(id, response);
249250

250251
} else {
251252
LogWarn("please invoke setRecvCallback() first.");
@@ -254,28 +255,30 @@ void Proto::handleAsResult(const Json &js) const
254255

255256
void Proto::handleAsError(const Json &js) const
256257
{
258+
Response response;
259+
257260
auto &js_error = js["error"];
258-
int errcode = 0;
259-
if (!util::json::GetField(js_error, "code", errcode)) {
261+
if (!util::json::GetField(js_error, "code", response.error.code)) {
260262
LogNotice("no code field in error");
261263
return;
262264
}
265+
util::json::GetField(js_error, "message", response.error.message);
263266

264267
if (id_type_ == IdType::kInt) {
265268
int id = 0;
266269
if (js.contains("id") && !util::json::GetField(js, "id", id)) {
267270
LogNotice("id type not int in error");
268271
return;
269272
}
270-
recv_respond_int_cb_(id, errcode, Json());
273+
recv_respond_int_cb_(id, response);
271274

272275
} else if (id_type_ == IdType::kString) {
273276
std::string id;
274277
if (js.contains("id") && !util::json::GetField(js, "id", id)) {
275278
LogNotice("id type not string in error");
276279
return;
277280
}
278-
recv_respond_str_cb_(id, errcode, Json());
281+
recv_respond_str_cb_(id, response);
279282

280283
} else {
281284
LogWarn("please invoke setRecvCallback() first.");

modules/jsonrpc/proto.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
#include <functional>
2424
#include <tbox/base/json_fwd.h>
2525

26+
#include "types.h"
27+
2628
namespace tbox {
2729
namespace jsonrpc {
2830

2931
class Proto {
3032
public:
3133
using RecvRequestIntCallback = std::function<void(int id, const std::string &method, const Json &params)>;
32-
using RecvRespondIntCallback = std::function<void(int id, int errcode, const Json &result)>;
34+
using RecvRespondIntCallback = std::function<void(int id, const Response &response)>;
3335
using RecvRequestStrCallback = std::function<void(const std::string &id, const std::string &method, const Json &params)>;
34-
using RecvRespondStrCallback = std::function<void(const std::string &id, int errcode, const Json &result)>;
36+
using RecvRespondStrCallback = std::function<void(const std::string &id, const Response &response)>;
3537
using SendDataCallback = std::function<void(const void* data_ptr, size_t data_size)>;
3638

3739
void setRecvCallback(RecvRequestIntCallback &&req_cb, RecvRespondIntCallback &&rsp_cb);
@@ -46,12 +48,12 @@ class Proto {
4648
void sendRequest(int id, const std::string &method);
4749
void sendRequest(int id, const std::string &method, const Json &js_params);
4850
void sendResult(int id, const Json &js_result);
49-
void sendError(int id, int errcode, const std::string &message = "");
51+
void sendError(int id, int errcode, const std::string &message);
5052

5153
void sendRequest(const std::string &id, const std::string &method);
5254
void sendRequest(const std::string &id, const std::string &method, const Json &js_params);
5355
void sendResult(const std::string &id, const Json &js_result);
54-
void sendError(const std::string &id, int errcode, const std::string &message = "");
56+
void sendError(const std::string &id, int errcode, const std::string &message);
5557

5658
public:
5759
/**
@@ -60,8 +62,6 @@ class Proto {
6062
virtual ssize_t onRecvData(const void *data_ptr, size_t data_size) = 0;
6163

6264
protected:
63-
enum class IdType { kNone, kInt, kString };
64-
6565
virtual void sendJson(const Json &js) = 0;
6666

6767
void onRecvJson(const Json &js) const;

modules/jsonrpc/protos/header_stream_proto_test.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TEST(HeaderStreamProto, sendRequest) {
4141
EXPECT_EQ(js_params, Json());
4242
++count;
4343
},
44-
[&] (int id, int errcode, const Json &js_result) { ++count; UNUSED_VAR(id), UNUSED_VAR(errcode), UNUSED_VAR(js_result); }
44+
[&] (int, const Response &) { ++count; }
4545
);
4646
proto.setSendCallback(
4747
[&] (const void *data_ptr, size_t data_size) {
@@ -73,7 +73,7 @@ TEST(HeaderStreamProto, sendRequestWithParams) {
7373
EXPECT_EQ(js_params, js_send_params);
7474
++count;
7575
},
76-
[&] (int id, int errcode, const Json &js_result) { ++count; UNUSED_VAR(id), UNUSED_VAR(errcode), UNUSED_VAR(js_result); }
76+
[&] (int, const Response &) { ++count; }
7777
);
7878
proto.setSendCallback(
7979
[&] (const void *data_ptr, size_t data_size) {
@@ -100,11 +100,10 @@ TEST(HeaderStreamProto, sendResult) {
100100
int count = 0;
101101
proto.setRecvCallback(
102102
[&] (int id, const std::string &method, const Json &js_params) { ++count; UNUSED_VAR(id), UNUSED_VAR(method), UNUSED_VAR(js_params);},
103-
[&] (int id, int errcode, const Json &js_result) {
103+
[&] (int id, const Response &r) {
104104
EXPECT_EQ(id, 1);
105-
EXPECT_EQ(js_result, js_send_result);
105+
EXPECT_EQ(r.js_result, js_send_result);
106106
++count;
107-
UNUSED_VAR(errcode);
108107
}
109108
);
110109
proto.setSendCallback(
@@ -128,9 +127,10 @@ TEST(HeaderStreamProto, sendError) {
128127
int count = 0;
129128
proto.setRecvCallback(
130129
[&] (int id, const std::string &method, const Json &js_params) { ++count; UNUSED_VAR(id); UNUSED_VAR(method); UNUSED_VAR(js_params); },
131-
[&] (int id, int errcode, const Json &) {
130+
[&] (int id, const Response &r) {
132131
EXPECT_EQ(id, 1);
133-
EXPECT_EQ(errcode, -1000);
132+
EXPECT_EQ(r.error.code, -1000);
133+
EXPECT_EQ(r.error.message, "-1000");
134134
++count;
135135
}
136136
);
@@ -140,7 +140,7 @@ TEST(HeaderStreamProto, sendError) {
140140
}
141141
);
142142

143-
proto.sendError(1, -1000);
143+
proto.sendError(1, -1000, "-1000");
144144
EXPECT_EQ(count, 1);
145145

146146
LogOutput_Disable();
@@ -160,7 +160,7 @@ TEST(HeaderStreamProto, RecvUncompleteData) {
160160
EXPECT_EQ(js_params, Json());
161161
++count;
162162
},
163-
[] (int, int, const Json &) { }
163+
[] (int, const Response &) { }
164164
);
165165

166166
const char *str_1 = "\xEA\x53\x00\x00\x00\x28{\"id\":1,\"meth";

modules/jsonrpc/protos/packet_proto_test.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TEST(PacketProto, sendRequest) {
4141
EXPECT_EQ(js_params, Json());
4242
++count;
4343
},
44-
[&] (int id, int errcode, const Json &js_result) { ++count; UNUSED_VAR(id); UNUSED_VAR(errcode); UNUSED_VAR(js_result); }
44+
[&] (int, const Response &) { ++count; }
4545
);
4646
proto.setSendCallback(
4747
[&] (const void *data_ptr, size_t data_size) {
@@ -73,7 +73,7 @@ TEST(PacketProto, sendRequestWithParams) {
7373
EXPECT_EQ(js_params, js_send_params);
7474
++count;
7575
},
76-
[&] (int id, int errcode, const Json &js_result) { ++count; UNUSED_VAR(id); UNUSED_VAR(errcode); UNUSED_VAR(js_result); }
76+
[&] (int, const Response &) { ++count; }
7777
);
7878
proto.setSendCallback(
7979
[&] (const void *data_ptr, size_t data_size) {
@@ -100,11 +100,10 @@ TEST(PacketProto, sendResult) {
100100
int count = 0;
101101
proto.setRecvCallback(
102102
[&] (int id, const std::string &method, const Json &js_params) { ++count; UNUSED_VAR(id); UNUSED_VAR(method); UNUSED_VAR(js_params); },
103-
[&] (int id, int errcode, const Json &js_result) {
103+
[&] (int id, const Response &r) {
104104
EXPECT_EQ(id, 1);
105-
EXPECT_EQ(js_result, js_send_result);
105+
EXPECT_EQ(r.js_result, js_send_result);
106106
++count;
107-
UNUSED_VAR(errcode);
108107
}
109108
);
110109
proto.setSendCallback(
@@ -128,9 +127,10 @@ TEST(PacketProto, sendError) {
128127
int count = 0;
129128
proto.setRecvCallback(
130129
[&] (int id, const std::string &method, const Json &js_params) { ++count; UNUSED_VAR(id); UNUSED_VAR(method); UNUSED_VAR(js_params); },
131-
[&] (int id, int errcode, const Json &) {
130+
[&] (int id, const Response &r) {
132131
EXPECT_EQ(id, 1);
133-
EXPECT_EQ(errcode, -1000);
132+
EXPECT_EQ(r.error.code, -1000);
133+
EXPECT_EQ(r.error.message, "-1000");
134134
++count;
135135
}
136136
);
@@ -140,7 +140,7 @@ TEST(PacketProto, sendError) {
140140
}
141141
);
142142

143-
proto.sendError(1, -1000);
143+
proto.sendError(1, -1000, "-1000");
144144
EXPECT_EQ(count, 1);
145145

146146
LogOutput_Disable();
@@ -160,7 +160,7 @@ TEST(PacketProto, RecvUncompleteData) {
160160
EXPECT_EQ(js_params, Json());
161161
++count;
162162
},
163-
[] (int, int, const Json &) { }
163+
[] (int, const Response &) { }
164164
);
165165

166166
const char *str_1 = R"({"id":1,"meth)";

0 commit comments

Comments
 (0)