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
4243int 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 });
0 commit comments