-
Notifications
You must be signed in to change notification settings - Fork 4
/
rpc_config.cpp
85 lines (73 loc) · 2.25 KB
/
rpc_config.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
#include <atomic>
#include <cstdio>
#include <cstdlib>
#include <thread>
#include "asio_net/rpc_client.hpp"
#include "asio_net/rpc_server.hpp"
#include "assert_def.h"
#include "log.h"
using namespace asio_net;
const uint16_t PORT = 6666;
int main() {
// test flags
static std::atomic_bool pass_flag_rpc_pass{false};
static std::atomic_bool pass_flag_session_close{false};
static std::atomic_bool pass_flag_client_close{false};
// server
std::thread([] {
auto rpc = rpc_core::rpc::create();
rpc->subscribe("cmd", [](const std::string& data) -> std::string {
LOG("session on cmd: %s", data.c_str());
ASSERT(data == "hello");
return "world";
});
asio::io_context context;
rpc_server server(context, PORT, rpc_config{.rpc = rpc});
server.on_session = [&](const std::weak_ptr<rpc_session>& rs) {
LOG("on_session:");
auto session = rs.lock();
ASSERT(session->rpc == rpc);
session->on_close = [] {
LOG("session on_close:");
pass_flag_session_close = true;
};
};
server.start(true);
}).detach();
// client
std::thread([] {
auto rpc = rpc_core::rpc::create();
rpc->cmd("cmd")->msg(std::string("hello"))->call(); // no effect
asio::io_context context;
rpc_client client(context, rpc_config{.rpc = rpc});
client.on_open = [&](const std::shared_ptr<rpc_core::rpc>& rpc_) {
LOG("client on_open:");
ASSERT(rpc_ == rpc);
rpc->cmd("cmd")
->msg(std::string("hello"))
->rsp([&](const std::string& data) {
LOG("cmd rsp: %s", data.c_str());
if (data == "world") {
pass_flag_rpc_pass = true;
}
client.close();
})
->call();
};
client.on_close = [&] {
pass_flag_client_close = true;
LOG("client on_close:");
client.stop();
};
client.open("localhost", PORT);
client.run();
LOG("client exited");
rpc->cmd("cmd")->msg(std::string("hello"))->call(); // no effect
}).join();
ASSERT(pass_flag_rpc_pass);
ASSERT(pass_flag_client_close);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ASSERT(pass_flag_session_close);
LOG("all rpc_session should destroyed before here!!!");
return EXIT_SUCCESS;
}