Skip to content

Commit 723b9e8

Browse files
committed
!41 feat(jsonrpc):1.12.24, 修改jsonrpc,将Rpc拆分成StrIdRpc与IntIdRpc
* feat(jsonrpc):1.12.24, 修改jsonrpc * feat(jsonrpc): 将Rpc拆分成IntIdRpc,StrIdRpc,并通过测试
1 parent 099d353 commit 723b9e8

38 files changed

+1696
-129
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# .============.
3+
# // M A K E / \
4+
# // C++ DEV / \
5+
# // E A S Y / \/ \
6+
# ++ ----------. \/\ .
7+
# \\ \ \ /\ /
8+
# \\ \ \ /
9+
# \\ \ \ /
10+
# -============'
11+
#
12+
# Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
#
14+
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
# Use of this source code is governed by MIT license that can be found
16+
# in the LICENSE file in the root of the source tree. All contributing
17+
# project authors may be found in the CONTRIBUTORS.md file in the root
18+
# of the source tree.
19+
#
20+
21+
all test clean distclean:
22+
@for i in $(shell ls) ; do \
23+
if [ -d $$i ]; then \
24+
$(MAKE) -C $$i $@ || exit $$? ; \
25+
fi \
26+
done

examples/jsonrpc/message/ping/Makefile renamed to examples/jsonrpc/message/int_id/ping/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# of the source tree.
1919
#
2020

21-
PROJECT := examples/jsonrpc/message/ping
21+
PROJECT := examples/jsonrpc/message/int_id/ping
2222
EXE_NAME := ${PROJECT}
2323

2424
CPP_SRC_FILES := ping.cpp

examples/jsonrpc/message/ping/ping.cpp renamed to examples/jsonrpc/message/int_id/ping/ping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <tbox/network/tcp_client.h> //! 导入TcpClient模块
3939
#include <tbox/util/json.h> //! 使用JSON操作的辅助函数 GetField()
4040
#include <tbox/jsonrpc/protos/raw_stream_proto.h> //! 导入 jsonrpc::RawStreamProto
41-
#include <tbox/jsonrpc/rpc.h> //! 导入 jsonrpc::Rpc
41+
#include <tbox/jsonrpc/int_id_rpc.h> //! 导入 jsonrpc::IntIdRpc
4242

4343
using namespace tbox;
4444

@@ -61,7 +61,7 @@ int main(int argc, char **argv)
6161

6262
network::TcpClient tcp_client(loop);
6363
jsonrpc::RawStreamProto proto;
64-
jsonrpc::Rpc rpc(loop);
64+
jsonrpc::IntIdRpc rpc(loop);
6565

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

examples/jsonrpc/message/pong/Makefile renamed to examples/jsonrpc/message/int_id/pong/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# of the source tree.
1919
#
2020

21-
PROJECT := examples/jsonrpc/message/pong
21+
PROJECT := examples/jsonrpc/message/int_id/pong
2222
EXE_NAME := ${PROJECT}
2323

2424
CPP_SRC_FILES := pong.cpp

examples/jsonrpc/message/pong/pong.cpp renamed to examples/jsonrpc/message/int_id/pong/pong.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <tbox/event/signal_event.h> //! ctrl+c信号事件
3636
#include <tbox/network/tcp_server.h> //! TcpServer
3737
#include <tbox/jsonrpc/protos/raw_stream_proto.h> //! jsonrpc::RawStreamProto
38-
#include <tbox/jsonrpc/rpc.h> //! jsonrpc::Rpc
38+
#include <tbox/jsonrpc/int_id_rpc.h> //! jsonrpc::IntIdRpc
3939

4040
using namespace tbox;
4141

@@ -58,7 +58,7 @@ int main(int argc, char **argv)
5858

5959
network::TcpServer tcp_server(loop);
6060
jsonrpc::RawStreamProto proto;
61-
jsonrpc::Rpc rpc(loop);
61+
jsonrpc::IntIdRpc rpc(loop);
6262

6363
rpc.initialize(&proto, 3);
6464
std::string srv_addr = "/tmp/ping_pong.sock";
@@ -101,7 +101,7 @@ int main(int argc, char **argv)
101101
rpc.addService("ping", [&] (int id, const Json &js_params, int &, Json &) {
102102
int ping_count = 0;
103103
util::json::GetField(js_params, "count", ping_count);
104-
LogDbg("got ping_count: %d", ping_count);
104+
LogDbg("id: %d, got ping_count: %d", id, ping_count);
105105
rpc.notify("pong", js_params);
106106
return false; //! 表示不回复
107107
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# .============.
3+
# // M A K E / \
4+
# // C++ DEV / \
5+
# // E A S Y / \/ \
6+
# ++ ----------. \/\ .
7+
# \\ \ \ /\ /
8+
# \\ \ \ /
9+
# \\ \ \ /
10+
# -============'
11+
#
12+
# Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
#
14+
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
# Use of this source code is governed by MIT license that can be found
16+
# in the LICENSE file in the root of the source tree. All contributing
17+
# project authors may be found in the CONTRIBUTORS.md file in the root
18+
# of the source tree.
19+
#
20+
21+
all test clean distclean:
22+
@for i in $(shell ls) ; do \
23+
if [ -d $$i ]; then \
24+
$(MAKE) -C $$i $@ || exit $$? ; \
25+
fi \
26+
done

examples/jsonrpc/req_rsp/ping/Makefile renamed to examples/jsonrpc/message/str_id/ping/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# of the source tree.
1919
#
2020

21-
PROJECT := examples/jsonrpc/req_rsp/ping
21+
PROJECT := examples/jsonrpc/message/str_id/ping
2222
EXE_NAME := ${PROJECT}
2323

2424
CPP_SRC_FILES := ping.cpp
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2023 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
21+
/**
22+
* 这是JsonRpc模块ping/pong示例中ping的一方
23+
*
24+
* 它主动去连接/tmp/ping_pong.sock,连接成功后就会发送ping请求,并在请
25+
* 求中附带一个count参数。该count参数在每次请求之前都会自增1。
26+
* pong端收到ping请求后,将count作为结果回复该请求。
27+
* ping端收到回复后,就立即发送下一个ping请求。
28+
* 如此周而复始,直至与pong端断开,或者接收到了SIGINT停止信号
29+
*/
30+
31+
#include <tbox/base/log.h> //! 打印日志
32+
#include <tbox/base/log_output.h> //! 使能日志输出
33+
#include <tbox/base/scope_exit.hpp> //! 使用 SetScopeExitAction()
34+
#include <tbox/base/json.hpp> //! 操作JSON对象用
35+
#include <tbox/util/buffer.h> //! 对Buffer进行操作
36+
#include <tbox/event/loop.h> //! 事件循环
37+
#include <tbox/event/signal_event.h> //! ctrl+c信号事件
38+
#include <tbox/network/tcp_client.h> //! 导入TcpClient模块
39+
#include <tbox/util/json.h> //! 使用JSON操作的辅助函数 GetField()
40+
#include <tbox/jsonrpc/protos/raw_stream_proto.h> //! 导入 jsonrpc::RawStreamProto
41+
#include <tbox/jsonrpc/str_id_rpc.h> //! 导入 jsonrpc::StrIdRpc
42+
43+
using namespace tbox;
44+
45+
int main(int argc, char **argv)
46+
{
47+
LogOutput_Enable();
48+
49+
LogInfo("enter");
50+
51+
auto loop = event::Loop::New();
52+
auto sig_event = loop->newSignalEvent();
53+
54+
//! 设置退出时,要释放loop与sig_event
55+
SetScopeExitAction(
56+
[=] {
57+
delete sig_event;
58+
delete loop;
59+
}
60+
);
61+
62+
network::TcpClient tcp_client(loop);
63+
jsonrpc::RawStreamProto proto;
64+
jsonrpc::StrIdRpc rpc(loop);
65+
66+
rpc.initialize(&proto, 3);
67+
std::string srv_addr = "/tmp/ping_pong.sock";
68+
69+
tcp_client.initialize(network::SockAddr::FromString(srv_addr));
70+
//! 设置接收到数据后的处理
71+
tcp_client.setReceiveCallback([&] (network::Buffer &buff) {
72+
while (buff.readableSize() > 0) {
73+
//! 将buff中的数据交给proto进行解析
74+
auto ret = proto.onRecvData(buff.readableBegin(), buff.readableSize());
75+
if (ret > 0) { //! 正常解析
76+
buff.hasRead(ret);
77+
} else if (ret < 0) { //! 有错误
78+
tcp_client.stop();
79+
tcp_client.start();
80+
} else
81+
break;
82+
}
83+
}, 0);
84+
85+
//! 设置proto发送数据的方法
86+
proto.setSendCallback([&] (const void* data_ptr, size_t data_size) {
87+
tcp_client.send(data_ptr, data_size); //! 将数据交给tcp_client去发送
88+
});
89+
90+
tcp_client.start();
91+
92+
int ping_count = 0;
93+
std::function<void()> send_ping;
94+
//! 定义ping的动作
95+
send_ping = [&] {
96+
++ping_count;
97+
Json js_params = {{"count", ping_count}}; //! 组装请求参数
98+
99+
//! 发送ping消息
100+
rpc.notify("ping", js_params);
101+
LogDbg("send ping: %d", ping_count);
102+
};
103+
104+
//! 定义收到pong的动作
105+
rpc.addService("pong", [&] (const std::string &, const Json &js_params, int &, Json &) {
106+
int pong_count = 0;
107+
util::json::GetField(js_params, "count", pong_count);
108+
send_ping();
109+
return false;
110+
});
111+
112+
//! 设置一旦tcp_client连接上就进行ping动作
113+
tcp_client.setConnectedCallback(send_ping);
114+
115+
//! 设置程序安全退出条件
116+
sig_event->initialize(SIGINT, event::Event::Mode::kPersist);
117+
sig_event->enable();
118+
119+
//! 设置程序退出动作
120+
sig_event->setCallback(
121+
[&] (int) {
122+
tcp_client.stop();
123+
loop->exitLoop();
124+
}
125+
);
126+
127+
LogInfo("start");
128+
loop->runLoop();
129+
LogInfo("stop");
130+
131+
rpc.cleanup();
132+
tcp_client.cleanup();
133+
return 0;
134+
}

examples/jsonrpc/req_rsp/pong/Makefile renamed to examples/jsonrpc/message/str_id/pong/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# of the source tree.
1919
#
2020

21-
PROJECT := examples/jsonrpc/req_rsp/pong
21+
PROJECT := examples/jsonrpc/message/str_id/pong
2222
EXE_NAME := ${PROJECT}
2323

2424
CPP_SRC_FILES := pong.cpp
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2023 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
21+
/**
22+
* 这是JsonRpc模块ping/pong示例中pong的一方
23+
*
24+
* 它绑定Unix Domain Sock: /tmp/ping_pong.sock,等待被连接。
25+
* 当收到ping请求后,将参数中的count提取出来作为结果直接回复。
26+
*/
27+
28+
#include <tbox/base/log.h> //! 打印日志
29+
#include <tbox/base/log_output.h> //! LogOutput_Enable()
30+
#include <tbox/base/scope_exit.hpp> //! SetScopeExitAction()
31+
#include <tbox/base/json.hpp> //! 操作JSON对象用
32+
#include <tbox/util/buffer.h> //! 对Buffer进行操作
33+
#include <tbox/util/json.h> //! util::json::GetField()
34+
#include <tbox/event/loop.h> //! 事件循环
35+
#include <tbox/event/signal_event.h> //! ctrl+c信号事件
36+
#include <tbox/network/tcp_server.h> //! TcpServer
37+
#include <tbox/jsonrpc/protos/raw_stream_proto.h> //! jsonrpc::RawStreamProto
38+
#include <tbox/jsonrpc/str_id_rpc.h> //! jsonrpc::StrIdRpc
39+
40+
using namespace tbox;
41+
42+
int main(int argc, char **argv)
43+
{
44+
LogOutput_Enable();
45+
46+
LogInfo("enter");
47+
48+
auto loop = event::Loop::New();
49+
auto sig_event = loop->newSignalEvent();
50+
51+
//! 设置退出时,要释放loop与sig_event
52+
SetScopeExitAction(
53+
[=] {
54+
delete sig_event;
55+
delete loop;
56+
}
57+
);
58+
59+
network::TcpServer tcp_server(loop);
60+
jsonrpc::RawStreamProto proto;
61+
jsonrpc::StrIdRpc rpc(loop);
62+
63+
rpc.initialize(&proto, 3);
64+
std::string srv_addr = "/tmp/ping_pong.sock";
65+
66+
network::TcpServer::ConnToken curr_client_token; //! 当前的客户端
67+
68+
tcp_server.initialize(network::SockAddr::FromString(srv_addr), 2);
69+
//! 设置接收到连接后的动作:保存curr_client_token
70+
tcp_server.setConnectedCallback([&] (network::TcpServer::ConnToken client_token) {
71+
tcp_server.disconnect(curr_client_token);
72+
curr_client_token = client_token;
73+
});
74+
//! 设置连接断开后的动作:清除curr_client_token
75+
tcp_server.setDisconnectedCallback([&] (network::TcpServer::ConnToken client_token) {
76+
curr_client_token.reset();
77+
});
78+
//! 设置接收到数据后的处理
79+
tcp_server.setReceiveCallback([&] (network::TcpServer::ConnToken client_token, network::Buffer &buff) {
80+
while (buff.readableSize() > 0) {
81+
//! 将buff中的数据交给proto进行解析
82+
auto ret = proto.onRecvData(buff.readableBegin(), buff.readableSize());
83+
if (ret > 0) {
84+
buff.hasRead(ret);
85+
} else if (ret < 0) { //! 有错误
86+
tcp_server.disconnect(curr_client_token);
87+
curr_client_token.reset();
88+
} else
89+
break;
90+
}
91+
}, 0);
92+
93+
//! 设置proto发送数据的方法
94+
proto.setSendCallback([&] (const void* data_ptr, size_t data_size) {
95+
tcp_server.send(curr_client_token, data_ptr, data_size);
96+
});
97+
98+
tcp_server.start(); //! 启动tcp服务
99+
100+
//! 注册ping的服务处理函数
101+
rpc.addService("ping", [&] (const std::string &id, const Json &js_params, int &, Json &) {
102+
int ping_count = 0;
103+
util::json::GetField(js_params, "count", ping_count);
104+
LogDbg("id: %s, got ping_count: %d", id.c_str(), ping_count);
105+
rpc.notify("pong", js_params);
106+
return false; //! 表示不回复
107+
});
108+
109+
//! 设置程序安全退出条件
110+
sig_event->initialize(SIGINT, event::Event::Mode::kPersist);
111+
sig_event->enable();
112+
113+
//! 设置程序退出动作
114+
sig_event->setCallback(
115+
[&] (int) {
116+
tcp_server.stop();
117+
loop->exitLoop();
118+
}
119+
);
120+
121+
LogInfo("start");
122+
loop->runLoop();
123+
LogInfo("stop");
124+
125+
rpc.cleanup();
126+
tcp_server.cleanup();
127+
return 0;
128+
}

0 commit comments

Comments
 (0)