-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathpong.cpp
128 lines (110 loc) · 4.12 KB
/
pong.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
/*
* .============.
* // 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.
*/
/**
* 这是JsonRpc模块ping/pong示例中pong的一方
*
* 它绑定Unix Domain Sock: /tmp/ping_pong.sock,等待被连接。
* 当收到ping请求后,将参数中的count提取出来作为结果直接回复。
*/
#include <tbox/base/log.h> //! 打印日志
#include <tbox/base/log_output.h> //! LogOutput_Enable()
#include <tbox/base/scope_exit.hpp> //! SetScopeExitAction()
#include <tbox/base/json.hpp> //! 操作JSON对象用
#include <tbox/util/buffer.h> //! 对Buffer进行操作
#include <tbox/util/json.h> //! util::json::GetField()
#include <tbox/event/loop.h> //! 事件循环
#include <tbox/event/signal_event.h> //! ctrl+c信号事件
#include <tbox/network/tcp_server.h> //! TcpServer
#include <tbox/jsonrpc/protos/raw_stream_proto.h> //! jsonrpc::RawStreamProto
#include <tbox/jsonrpc/rpc.h> //! jsonrpc::Rpc
using namespace tbox;
int main(int argc, char **argv)
{
LogOutput_Enable();
LogInfo("enter");
auto loop = event::Loop::New();
auto sig_event = loop->newSignalEvent();
//! 设置退出时,要释放loop与sig_event
SetScopeExitAction(
[=] {
delete sig_event;
delete loop;
}
);
network::TcpServer tcp_server(loop);
jsonrpc::RawStreamProto proto;
jsonrpc::Rpc rpc(loop);
rpc.initialize(&proto, 3);
std::string srv_addr = "/tmp/ping_pong.sock";
network::TcpServer::ConnToken curr_client_token; //! 当前的客户端
tcp_server.initialize(network::SockAddr::FromString(srv_addr), 2);
//! 设置接收到连接后的动作:保存curr_client_token
tcp_server.setConnectedCallback([&] (network::TcpServer::ConnToken client_token) {
tcp_server.disconnect(curr_client_token);
curr_client_token = client_token;
});
//! 设置连接断开后的动作:清除curr_client_token
tcp_server.setDisconnectedCallback([&] (network::TcpServer::ConnToken client_token) {
curr_client_token.reset();
});
//! 设置接收到数据后的处理
tcp_server.setReceiveCallback([&] (network::TcpServer::ConnToken client_token, network::Buffer &buff) {
while (buff.readableSize() > 0) {
//! 将buff中的数据交给proto进行解析
auto ret = proto.onRecvData(buff.readableBegin(), buff.readableSize());
if (ret > 0) {
buff.hasRead(ret);
} else if (ret < 0) { //! 有错误
tcp_server.disconnect(curr_client_token);
curr_client_token.reset();
} else
break;
}
}, 0);
//! 设置proto发送数据的方法
proto.setSendCallback([&] (const void* data_ptr, size_t data_size) {
tcp_server.send(curr_client_token, data_ptr, data_size);
});
tcp_server.start(); //! 启动tcp服务
//! 注册ping的服务处理函数
rpc.addService("ping", [&] (int id, const Json &js_params, int &, Json &) {
int ping_count = 0;
util::json::GetField(js_params, "count", ping_count);
LogDbg("got ping_count: %d", ping_count);
rpc.notify("pong", js_params);
return false; //! 表示不回复
});
//! 设置程序安全退出条件
sig_event->initialize(SIGINT, event::Event::Mode::kPersist);
sig_event->enable();
//! 设置程序退出动作
sig_event->setCallback(
[&] (int) {
tcp_server.stop();
loop->exitLoop();
}
);
LogInfo("start");
loop->runLoop();
LogInfo("stop");
rpc.cleanup();
tcp_server.cleanup();
return 0;
}