-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmprpc_echo_client.cpp
131 lines (101 loc) · 4.13 KB
/
mprpc_echo_client.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
129
130
131
#include "solid/frame/manager.hpp"
#include "solid/frame/scheduler.hpp"
#include "solid/frame/service.hpp"
#include "solid/frame/aio/aioresolver.hpp"
#include "solid/frame/mprpc/mprpcconfiguration.hpp"
#include "solid/frame/mprpc/mprpcservice.hpp"
#include "solid/utility/threadpool.hpp"
#include "mprpc_echo_messages.hpp"
#include <iostream>
using namespace solid;
using namespace std;
using AioSchedulerT = frame::Scheduler<frame::aio::Reactor<frame::mprpc::EventT>>;
using CallPoolT = ThreadPool<Function<void()>, Function<void()>>;
//-----------------------------------------------------------------------------
// Parameters
//-----------------------------------------------------------------------------
struct Parameters {
Parameters()
: port("3333")
{
}
string port;
};
//-----------------------------------------------------------------------------
namespace rpc_echo_client {
template <class M>
void complete_message(
frame::mprpc::ConnectionContext& _rctx,
frame::mprpc::MessagePointerT<M>& _rsent_msg_ptr,
frame::mprpc::MessagePointerT<M>& _rrecv_msg_ptr,
ErrorConditionT const& _rerror)
{
if (_rerror) {
cout << "Error sending message to " << _rctx.recipientName() << ". Error: " << _rerror.message() << endl;
return;
}
solid_check(_rrecv_msg_ptr && _rsent_msg_ptr);
cout << "Received from " << _rctx.recipientName() << ": " << _rrecv_msg_ptr->str << endl;
}
} // namespace rpc_echo_client
//-----------------------------------------------------------------------------
bool parseArguments(Parameters& _par, int argc, char* argv[]);
//-----------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
Parameters p;
if (!parseArguments(p, argc, argv))
return 0;
{
AioSchedulerT scheduler;
frame::Manager manager;
frame::mprpc::ServiceT rpcservice(manager);
CallPoolT cwp{{1, 100, 0}, [](const size_t) {}, [](const size_t) {}};
frame::aio::Resolver resolver([&cwp](std::function<void()>&& _fnc) { cwp.pushOne(std::move(_fnc)); });
ErrorConditionT err;
scheduler.start(1);
{
auto proto = frame::mprpc::serialization_v3::create_protocol<reflection::v1::metadata::Variant, uint8_t>(
reflection::v1::metadata::factory,
[&](auto& _rmap) {
auto lambda = [&]<typename T>(const uint8_t _id, const std::string_view _name, type_identity<T> const& _rtype) {
_rmap.template registerMessage<T>(_id, _name, rpc_echo_client::complete_message<T>);
};
rpc_echo::configure_protocol(lambda);
});
frame::mprpc::Configuration cfg(scheduler, proto);
cfg.client.name_resolve_fnc = frame::mprpc::InternetResolverF(resolver, p.port.c_str());
cfg.client.connection_start_state = frame::mprpc::ConnectionState::Active;
rpcservice.start(std::move(cfg));
}
while (true) {
string line;
getline(cin, line);
if (line == "q" || line == "Q" || line == "quit") {
break;
}
{
string recipient;
size_t offset = line.find(' ');
if (offset != string::npos) {
recipient = line.substr(0, offset);
rpcservice.sendMessage({recipient}, frame::mprpc::make_message<rpc_echo::Message>(line.substr(offset + 1)), {frame::mprpc::MessageFlagsE::AwaitResponse});
} else {
cout << "No recipient specified. E.g:" << endl
<< "localhost:4444 Some text to send" << endl;
}
}
}
}
return 0;
}
//-----------------------------------------------------------------------------
bool parseArguments(Parameters& _par, int argc, char* argv[])
{
if (argc == 2) {
_par.port = argv[1];
}
return true;
}