forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_server.cpp
More file actions
215 lines (173 loc) · 4.89 KB
/
rpc_server.cpp
File metadata and controls
215 lines (173 loc) · 4.89 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <asio2/rpc/rpc_server.hpp>
#include <nlohmann/json.hpp>
class userinfo
{
public:
std::string name;
int age;
std::map<int, std::string> purview;
// User defined object types require serialized the members like this:
template <class Archive>
void serialize(Archive & ar)
{
ar(name);
ar(age);
ar(purview);
}
};
void operator<<(asio2::rpc::oarchive& sr, const nlohmann::json& j)
{
sr << j.dump();
}
void operator>>(asio2::rpc::iarchive& dr, nlohmann::json& j)
{
std::string v;
dr >> v;
j = nlohmann::json::parse(v);
}
int add(int a, int b)
{
return a + b;
}
rpc::future<int> async_add(int a, int b)
{
rpc::promise<int> promise;
rpc::future<int> f = promise.get_future();
std::thread([a, b, promise = std::move(promise)]() mutable
{
std::this_thread::sleep_for(std::chrono::seconds(1));
asio2::ignore_unused(a, b);
promise.set_value(a + b);
}).detach();
return f;
}
rpc::future<void> async_test(std::shared_ptr<asio2::rpc_session>& session_ptr, std::string a, std::string b)
{
asio2::ignore_unused(session_ptr, a, b);
rpc::promise<void> promise;
rpc::future<void> f = promise.get_future();
ASIO2_ASSERT(a == "abc" && b == "def");
std::thread([a, b, promise]() mutable
{
asio2::ignore_unused(a, b);
promise.set_value();
}).detach();
return f;
}
nlohmann::json test_json(nlohmann::json j)
{
std::string s = j.dump();
return nlohmann::json::parse(s);
}
class A
{
public:
double mul(double a, double b)
{
return a * b;
}
// If you want to know which client called this function, set the first parameter
// to std::shared_ptr<asio2::rpc_session>& session_ptr
userinfo get_user(std::shared_ptr<asio2::rpc_session>& session_ptr)
{
asio2::detail::ignore_unused(session_ptr);
userinfo u;
u.name = "lilei";
u.age = ((int)time(nullptr)) % 100;
u.purview = { {1,"read"},{2,"write"} };
return u;
}
// If you want to know which client called this function, set the first parameter
// to std::shared_ptr<asio2::rpc_session>& session_ptr
void del_user(std::shared_ptr<asio2::rpc_session>& session_ptr, const userinfo& u)
{
printf("del_user is called by %s %u : %s %d \n",
session_ptr->remote_address().c_str(), session_ptr->remote_port(),
u.name.c_str(), u.age);
}
};
int main()
{
#if defined(WIN32) || defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS_)
// Detected memory leaks on windows system
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
std::string_view host = "0.0.0.0";
std::string_view port = "8010";
std::srand((unsigned int)time(nullptr));
for(;;)
{
// Specify the "max recv buffer size" to avoid malicious packets, if some client
// sent data packets size is too long to the "max recv buffer size", then the
// client will be disconnect automatic .
asio2::rpc_server server(
512, // the initialize recv buffer size :
1024, // the max recv buffer size :
4 // the thread count :
);
server.bind_connect([&](auto & session_ptr)
{
printf("client enter : %s %u %s %u\n",
session_ptr->remote_address().c_str(), session_ptr->remote_port(),
session_ptr->local_address().c_str(), session_ptr->local_port());
session_ptr->async_call([](asio::error_code ec, int v)
{
if (!ec)
{
ASIO2_ASSERT(v == 15 - 6);
}
printf("sub : %d err : %d %s\n", v, ec.value(), ec.message().c_str());
}, std::chrono::seconds(10), "sub", 15, 6);
}).bind_start([&](asio::error_code ec)
{
printf("start rpc server : %s %u %d %s\n",
server.listen_address().c_str(), server.listen_port(),
ec.value(), ec.message().c_str());
}).bind_stop([&](asio::error_code ec)
{
printf("stop : %d %s\n", ec.value(), ec.message().c_str());
});
A a;
server
.bind("add", add)
.bind("mul", &A::mul, a)
.bind("get_user", &A::get_user, a)
.bind("del_user", &A::del_user, &a);
server.bind("async_add", async_add);
server.bind("async_test", async_test);
server.bind("test_json", test_json);
server.bind("cat", [&](std::shared_ptr<asio2::rpc_session>& session_ptr,
const std::string& a, const std::string& b)
{
// Nested call rpc function in business function is ok.
session_ptr->async_call([session_ptr](asio::error_code ec, int v) mutable
{
// Nested call rpc function in business function is ok.
session_ptr->async_call([](asio::error_code ec, int v)
{
if (!ec)
{
ASIO2_ASSERT(v == 15 + 18);
}
printf("async_add : %d err : %d %s\n", v, ec.value(), ec.message().c_str());
}, "async_add", 15, 18);
if (!ec)
{
ASIO2_ASSERT(v == 15 - 8);
}
printf("sub : %d err : %d %s\n", v, ec.value(), ec.message().c_str());
}, "sub", 15, 8);
return a + b;
});
server.start(host, port);
auto sec = 1 + std::rand() % 2;
printf("enter %d\n", sec);
std::this_thread::sleep_for(std::chrono::seconds(sec));
printf("leave %d\n", sec);
server.start(host, port);
server.stop();
printf("<-------------------------------------------->\n");
}
while (std::getchar() != '\n');
return 0;
}