-
Notifications
You must be signed in to change notification settings - Fork 2
/
coro_frpc_bi.cpp
114 lines (98 loc) · 4.79 KB
/
coro_frpc_bi.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
#include <exception>
#include <functional>
#include <optional>
#include <string>
#include <spdlog/spdlog.h>
#include "fantasy.hpp"
inline std::string addr{"tcp://127.0.0.1:5878"};
void start(std::function<void()> func) {
std::thread(std::move(func)).detach();
}
auto create_bank_info() {
fantasy::BankInfo bank_info;
bank_info.name = "xiaoli";
bank_info.type = fantasy::TestType::EnumOne;
bank_info.test_one = 100;
bank_info.test_two = 101;
bank_info.test_map_one.emplace("frpc", fantasy::TestType::EnumOne);
bank_info.test_map.emplace(false, 555);
bank_info.test_vector.emplace_back("vector");
bank_info.info.name = "rpc";
bank_info.date_time = frpc::getFrpcDateTime();
return bank_info;
}
struct CoroHandler final : public fantasy::FrpcCoroHelloWorldServerHandler {
virtual frpc::Task<void> hello_world(fantasy::BankInfo bank_info,
std::string bank_name,
uint64_t blance,
std::optional<std::string> date,
frpc::DateTime date_time,
std::function<void(std::string, fantasy::Info, uint64_t, std::optional<std::string>)> cb) noexcept override {
spdlog::info("frpc::Task fantasy::HelloWorldServer server recv: {}, bank_name: {}, blance: {}, date: {}, date_time: {}",
fantasy::toString(bank_info), bank_name, blance, date.has_value() ? date.value() : "nullopt", frpc::toString(date_time));
fantasy::Info info;
info.name = "frpc::Task test";
cb("coro hello world", std::move(info), 556, std::nullopt);
co_return;
}
};
frpc::Task<> start_client() {
frpc::ChannelConfig bi_config{};
bi_config.addr = addr;
auto client = fantasy::HelloWorldClient::create(bi_config, [](std::string error) {
spdlog::error("coro fantasy::HelloWorldClient error: {}", error);
});
client->start();
spdlog::info("start coro client.");
auto [reply, info, count, date] = co_await client->hello_world_coro(create_bank_info(), "HF", 996, std::nullopt, frpc::getFrpcDateTime());
spdlog::info("coro fantasy::HelloWorldClient::hello_world recv: {},{},{},{}", reply, fantasy::toString(info), count, date.has_value() ? date.value() : "nullopt");
auto ret = co_await client->hello_world_coro(create_bank_info(), "HF", 666, std::nullopt, frpc::getFrpcDateTime(), std::chrono::milliseconds{500});
if (ret.has_value()) {
auto [reply, info, count, date] = ret.value();
spdlog::info("coro fantasy::HelloWorldClient::hello_world recv: {},{},{},{}", reply, fantasy::toString(info), count, date.has_value() ? date.value() : "nullopt");
co_return;
}
spdlog::error("coro fantasy::HelloWorldClient::hello_world timeout");
co_return;
}
void start_server() {
frpc::ChannelConfig bi_config{};
bi_config.addr = addr;
auto server = fantasy::HelloWorldServer::create(
bi_config,
std::make_shared<CoroHandler>(),
[](std::string error) {
spdlog::error("coro fantasy::HelloWorldServer error: {}", error);
});
server->monitor(
[](std::tuple<zmq_event_t, std::string> data) {
auto& [event, point] = data;
spdlog::info("bi coro server monitor: {} {}", frpc::getEventName(event.event), point);
},
ZMQ_EVENT_ACCEPTED | ZMQ_EVENT_DISCONNECTED);
server->start();
std::this_thread::sleep_for(std::chrono::seconds(10));
}
int main() {
start(start_server);
frpc::ChannelConfig bi_config{};
bi_config.addr = addr;
auto client = fantasy::HelloWorldClient::create(bi_config, [](std::string error) {
spdlog::error("coro fantasy::HelloWorldClient error: {}", error);
});
client->start();
frpc::async_run([&]() -> frpc::Task<> {
spdlog::info("start coro client.");
auto [reply, info, count, date] = co_await client->hello_world_coro(create_bank_info(), "HF", 996, std::nullopt, frpc::getFrpcDateTime());
spdlog::info("coro fantasy::HelloWorldClient::hello_world recv: {},{},{},{}", reply, fantasy::toString(info), count, date.has_value() ? date.value() : "nullopt");
auto ret = co_await client->hello_world_coro(create_bank_info(), "HF", 666, std::nullopt, frpc::getFrpcDateTime(), std::chrono::milliseconds{500});
if (ret.has_value()) {
auto [reply, info, count, date] = ret.value();
spdlog::info("coro fantasy::HelloWorldClient::hello_world recv: {},{},{},{}", reply, fantasy::toString(info), count, date.has_value() ? date.value() : "nullopt");
co_return;
}
spdlog::error("coro fantasy::HelloWorldClient::hello_world timeout");
co_return;
});
std::this_thread::sleep_for(std::chrono::seconds(15));
}