Skip to content

Commit 0668588

Browse files
committed
Merge branch 'develop'
2 parents 4827158 + 21f9cf3 commit 0668588

File tree

20 files changed

+840
-157
lines changed

20 files changed

+840
-157
lines changed

examples/jsonrpc/message/ping/ping.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* 如此周而复始,直至与pong端断开,或者接收到了SIGINT停止信号
2929
*/
3030

31+
#include <iostream>
3132
#include <tbox/base/log.h> //! 打印日志
3233
#include <tbox/base/log_output.h> //! 使能日志输出
3334
#include <tbox/base/scope_exit.hpp> //! 使用 SetScopeExitAction()
@@ -38,12 +39,24 @@
3839
#include <tbox/network/tcp_client.h> //! 导入TcpClient模块
3940
#include <tbox/util/json.h> //! 使用JSON操作的辅助函数 GetField()
4041
#include <tbox/jsonrpc/protos/raw_stream_proto.h> //! 导入 jsonrpc::RawStreamProto
41-
#include <tbox/jsonrpc/rpc.h> //! 导入 jsonrpc::Rpc
42+
#include <tbox/jsonrpc/rpc.h> //! 导入 jsonrpc::Rpc
4243

4344
using namespace tbox;
4445

4546
int main(int argc, char **argv)
4647
{
48+
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
49+
if (argc >= 2) {
50+
std::string type_str(argv[1]);
51+
if (type_str == "str") {
52+
id_type = jsonrpc::Rpc::IdType::kString;
53+
} else if (type_str != "int") {
54+
std::cout << "id_type invalid!" << std::endl
55+
<< "Usage: " << argv[0] << " int|str" << std::endl;
56+
return 0;
57+
}
58+
}
59+
4760
LogOutput_Enable();
4861

4962
LogInfo("enter");
@@ -61,7 +74,7 @@ int main(int argc, char **argv)
6174

6275
network::TcpClient tcp_client(loop);
6376
jsonrpc::RawStreamProto proto;
64-
jsonrpc::Rpc rpc(loop);
77+
jsonrpc::Rpc rpc(loop, id_type);
6578

6679
rpc.initialize(&proto, 3);
6780
std::string srv_addr = "/tmp/ping_pong.sock";

examples/jsonrpc/message/pong/pong.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* 当收到ping请求后,将参数中的count提取出来作为结果直接回复。
2626
*/
2727

28+
#include <iostream>
2829
#include <tbox/base/log.h> //! 打印日志
2930
#include <tbox/base/log_output.h> //! LogOutput_Enable()
3031
#include <tbox/base/scope_exit.hpp> //! SetScopeExitAction()
@@ -41,6 +42,18 @@ using namespace tbox;
4142

4243
int main(int argc, char **argv)
4344
{
45+
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
46+
if (argc >= 2) {
47+
std::string type_str(argv[1]);
48+
if (type_str == "str") {
49+
id_type = jsonrpc::Rpc::IdType::kString;
50+
} else if (type_str != "int") {
51+
std::cout << "id_type invalid!" << std::endl
52+
<< "Usage: " << argv[0] << " int|str" << std::endl;
53+
return 0;
54+
}
55+
}
56+
4457
LogOutput_Enable();
4558

4659
LogInfo("enter");
@@ -58,7 +71,7 @@ int main(int argc, char **argv)
5871

5972
network::TcpServer tcp_server(loop);
6073
jsonrpc::RawStreamProto proto;
61-
jsonrpc::Rpc rpc(loop);
74+
jsonrpc::Rpc rpc(loop, id_type);
6275

6376
rpc.initialize(&proto, 3);
6477
std::string srv_addr = "/tmp/ping_pong.sock";
@@ -101,7 +114,7 @@ int main(int argc, char **argv)
101114
rpc.addService("ping", [&] (int id, const Json &js_params, int &, Json &) {
102115
int ping_count = 0;
103116
util::json::GetField(js_params, "count", ping_count);
104-
LogDbg("got ping_count: %d", ping_count);
117+
LogDbg("id: %d, got ping_count: %d", id, ping_count);
105118
rpc.notify("pong", js_params);
106119
return false; //! 表示不回复
107120
});

examples/jsonrpc/req_rsp/ping/ping.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* 如此周而复始,直至与pong端断开,或者接收到了SIGINT停止信号
2929
*/
3030

31+
#include <iostream>
3132
#include <tbox/base/log.h> //! 打印日志
3233
#include <tbox/base/log_output.h> //! 使能日志输出
3334
#include <tbox/base/scope_exit.hpp> //! 使用 SetScopeExitAction()
@@ -44,6 +45,18 @@ using namespace tbox;
4445

4546
int main(int argc, char **argv)
4647
{
48+
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
49+
if (argc >= 2) {
50+
std::string type_str(argv[1]);
51+
if (type_str == "str") {
52+
id_type = jsonrpc::Rpc::IdType::kString;
53+
} else if (type_str != "int") {
54+
std::cout << "id_type invalid!" << std::endl
55+
<< "Usage: " << argv[0] << " int|str" << std::endl;
56+
return 0;
57+
}
58+
}
59+
4760
LogOutput_Enable();
4861

4962
LogInfo("enter");
@@ -61,7 +74,7 @@ int main(int argc, char **argv)
6174

6275
network::TcpClient tcp_client(loop);
6376
jsonrpc::RawStreamProto proto;
64-
jsonrpc::Rpc rpc(loop);
77+
jsonrpc::Rpc rpc(loop, id_type);
6578

6679
rpc.initialize(&proto, 3);
6780
std::string srv_addr = "/tmp/ping_pong.sock";

examples/jsonrpc/req_rsp/pong/pong.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* 当收到ping请求后,将参数中的count提取出来作为结果直接回复。
2626
*/
2727

28+
#include <iostream>
2829
#include <tbox/base/log.h> //! 打印日志
2930
#include <tbox/base/log_output.h> //! LogOutput_Enable()
3031
#include <tbox/base/scope_exit.hpp> //! SetScopeExitAction()
@@ -41,6 +42,18 @@ using namespace tbox;
4142

4243
int main(int argc, char **argv)
4344
{
45+
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
46+
if (argc >= 2) {
47+
std::string type_str(argv[1]);
48+
if (type_str == "str") {
49+
id_type = jsonrpc::Rpc::IdType::kString;
50+
} else if (type_str != "int") {
51+
std::cout << "id_type invalid!" << std::endl
52+
<< "Usage: " << argv[0] << " int|str" << std::endl;
53+
return 0;
54+
}
55+
}
56+
4457
LogOutput_Enable();
4558

4659
LogInfo("enter");
@@ -58,7 +71,7 @@ int main(int argc, char **argv)
5871

5972
network::TcpServer tcp_server(loop);
6073
jsonrpc::RawStreamProto proto;
61-
jsonrpc::Rpc rpc(loop);
74+
jsonrpc::Rpc rpc(loop, id_type);
6275

6376
rpc.initialize(&proto, 3);
6477
std::string srv_addr = "/tmp/ping_pong.sock";
@@ -101,7 +114,7 @@ int main(int argc, char **argv)
101114
rpc.addService("ping", [&] (int id, const Json &js_params, int &errcode, Json &js_result) {
102115
int ping_count = 0;
103116
util::json::GetField(js_params, "count", ping_count);
104-
LogDbg("got ping_count: %d", ping_count);
117+
LogDbg("id:%d, got ping_count: %d", id, ping_count);
105118
js_result = js_params;
106119
return true; //! 表示在函数返回后立即发送回复
107120
});

modules/jsonrpc/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ set(TBOX_JSONRPC_SOURCES
3232
protos/raw_stream_proto.cpp
3333
protos/header_stream_proto.cpp
3434
protos/packet_proto.cpp
35-
rpc.cpp)
35+
rpc.cpp
36+
)
3637

3738
set(TBOX_JSONRPC_TEST_SOURCES
3839
${TBOX_JSONRPC_SOURCES}
3940
protos/raw_stream_proto_test.cpp
4041
protos/header_stream_proto_test.cpp
4142
protos/packet_proto_test.cpp
42-
rpc_test.cpp)
43+
rpc_int_test.cpp
44+
rpc_str_test.cpp
45+
)
4346

4447
add_library(${TBOX_LIBRARY_NAME} ${TBOX_BUILD_LIB_TYPE} ${TBOX_JSONRPC_SOURCES})
4548
add_library(tbox::${TBOX_LIBRARY_NAME} ALIAS ${TBOX_LIBRARY_NAME})

modules/jsonrpc/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ TEST_CPP_SRC_FILES = \
4545
protos/raw_stream_proto_test.cpp \
4646
protos/header_stream_proto_test.cpp \
4747
protos/packet_proto_test.cpp \
48-
rpc_test.cpp \
48+
rpc_int_test.cpp \
49+
rpc_str_test.cpp \
4950

5051
TEST_LDFLAGS := $(LDFLAGS) -ltbox_event -ltbox_util -ltbox_base -ldl
5152
ENABLE_SHARED_LIB = no

0 commit comments

Comments
 (0)