forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.hpp
More file actions
338 lines (290 loc) · 9 KB
/
server.hpp
File metadata and controls
338 lines (290 loc) · 9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* COPYRIGHT (C) 2017-2021, zhllxt
*
* author : zhllxt
* email : 37792738@qq.com
*
* Distributed under the GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* (See accompanying file LICENSE or see <http://www.gnu.org/licenses/>)
*/
#ifndef __ASIO2_SERVER_HPP__
#define __ASIO2_SERVER_HPP__
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <asio2/base/detail/push_options.hpp>
#include <cstdint>
#include <memory>
#include <chrono>
#include <functional>
#include <atomic>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <type_traits>
#include <asio2/3rd/asio.hpp>
#include <asio2/3rd/magic_enum.hpp>
#include <asio2/base/iopool.hpp>
#include <asio2/base/error.hpp>
#include <asio2/base/log.hpp>
#include <asio2/base/listener.hpp>
#include <asio2/base/session_mgr.hpp>
#include <asio2/base/define.hpp>
#include <asio2/base/detail/object.hpp>
#include <asio2/base/detail/allocator.hpp>
#include <asio2/base/detail/util.hpp>
#include <asio2/base/detail/buffer_wrap.hpp>
#include <asio2/base/detail/condition_wrap.hpp>
#include <asio2/base/component/user_data_cp.hpp>
#include <asio2/base/component/user_timer_cp.hpp>
#include <asio2/base/component/post_cp.hpp>
#include <asio2/base/component/async_event_cp.hpp>
namespace asio2::detail
{
ASIO2_CLASS_FORWARD_DECLARE_BASE;
template<class derived_t, class session_t>
class server_impl_t
: public object_t <derived_t>
, public iopool_cp
, public user_data_cp <derived_t>
, public user_timer_cp <derived_t>
, public post_cp <derived_t>
, public async_event_cp <derived_t>
{
ASIO2_CLASS_FRIEND_DECLARE_BASE;
public:
using super = object_t <derived_t>;
using self = server_impl_t<derived_t, session_t>;
using key_type = std::size_t;
public:
/**
* @constructor
*/
template<class ThreadCountOrScheduler>
explicit server_impl_t(ThreadCountOrScheduler&& tcos)
: object_t <derived_t>()
, iopool_cp (std::forward<ThreadCountOrScheduler>(tcos))
, user_data_cp <derived_t>()
, user_timer_cp<derived_t>()
, post_cp <derived_t>()
, rallocator_()
, wallocator_()
, listener_ ()
, io_ (iopool_cp::_get_io(0))
, sessions_ (io_, this->state_)
{
}
/**
* @destructor
*/
~server_impl_t()
{
}
/**
* @function : start the server
*/
inline bool start()
{
return true;
}
/**
* @function : stop the server
*/
inline void stop()
{
ASIO2_ASSERT(this->io_.strand().running_in_this_thread());
if (!this->io_.strand().running_in_this_thread())
{
this->derived().post([this]() mutable
{
this->stop();
});
return;
}
// close user custom timers
this->stop_all_timers();
// close all posted timed tasks
this->stop_all_timed_tasks();
// close all async_events
this->notify_all_events();
// destroy user data, maybe the user data is self shared_ptr,
// if don't destroy it, will cause loop refrence.
this->user_data_.reset();
}
/**
* @function : check whether the server is started
*/
inline bool is_started() const
{
return (this->state_ == state_t::started);
}
/**
* @function : check whether the server is stopped
*/
inline bool is_stopped() const
{
return (this->state_ == state_t::stopped);
}
/**
* @function : get this object hash key
*/
inline key_type hash_key() const
{
return reinterpret_cast<key_type>(this);
}
/**
* @function : Asynchronous send data for each session
* supporting multi data formats,see asio::buffer(...) in /asio/buffer.hpp
* You can call this function on the communication thread and anywhere,it's multi thread safed.
* PodType * : async_send("abc");
* PodType (&data)[N] : double m[10]; async_send(m);
* std::array<PodType, N> : std::array<int,10> m; async_send(m);
* std::vector<PodType, Allocator> : std::vector<float> m; async_send(m);
* std::basic_string<Elem, Traits, Allocator> : std::string m; async_send(m);
*/
template<class T>
inline derived_t & async_send(const T& data)
{
this->sessions_.for_each([&data](std::shared_ptr<session_t>& session_ptr) mutable
{
session_ptr->async_send(data);
});
return this->derived();
}
/**
* @function : Asynchronous send data for each session
* You can call this function on the communication thread and anywhere,it's multi thread safed.
* PodType * : async_send("abc");
*/
template<class CharT, class Traits = std::char_traits<CharT>>
inline typename std::enable_if_t<
std::is_same_v<detail::remove_cvref_t<CharT>, char> ||
std::is_same_v<detail::remove_cvref_t<CharT>, wchar_t> ||
std::is_same_v<detail::remove_cvref_t<CharT>, char16_t> ||
std::is_same_v<detail::remove_cvref_t<CharT>, char32_t>, derived_t&> async_send(CharT * s)
{
return this->async_send(s, s ? Traits::length(s) : 0);
}
/**
* @function : Asynchronous send data for each session
* You can call this function on the communication thread and anywhere,it's multi thread safed.
* PodType (&data)[N] : double m[10]; async_send(m,5);
*/
template<class CharT, class SizeT>
inline typename std::enable_if_t<std::is_integral_v<detail::remove_cvref_t<SizeT>>, derived_t&>
async_send(CharT* s, SizeT count)
{
if (s)
{
this->sessions_.for_each([s, count](std::shared_ptr<session_t>& session_ptr) mutable
{
session_ptr->async_send(s, count);
});
}
return this->derived();
}
public:
/**
* @function : get the acceptor refrence,derived classes must override this function
*/
inline auto & acceptor() { return this->derived().acceptor(); }
/**
* @function : get the listen address
*/
inline std::string listen_address()
{
try
{
return this->acceptor().local_endpoint().address().to_string();
}
catch (system_error & e) { set_last_error(e); }
return std::string();
}
/**
* @function : get the listen port
*/
inline unsigned short listen_port()
{
try
{
return this->acceptor().local_endpoint().port();
}
catch (system_error & e) { set_last_error(e); }
return static_cast<unsigned short>(0);
}
/**
* @function : get connected session count
*/
inline std::size_t session_count() { return this->sessions_.size(); }
/**
* @function :
* @param : fn - The handler to be called for each session.
* Function signature :
* void(std::shared_ptr<asio2::xxx_session>& session_ptr)
*/
template<class Fun>
inline derived_t & foreach_session(Fun&& fn)
{
this->sessions_.for_each(std::forward<Fun>(fn));
return this->derived();
}
/**
* @function : find the session by session's hash key
*/
template<class KeyType>
inline std::shared_ptr<session_t> find_session(const KeyType& key)
{
return this->sessions_.find(key);
}
/**
* @function : find the session by user custom role
* @param : fn - The handler to be called when search the session.
* Function signature :
* bool(std::shared_ptr<asio2::xxx_session>& session_ptr)
* @return : std::shared_ptr<asio2::xxx_session>
*/
template<class Fun>
inline std::shared_ptr<session_t> find_session_if(Fun&& fn)
{
return std::shared_ptr<session_t>(this->sessions_.find_if(std::forward<Fun>(fn)));
}
/**
* @function : get the io object refrence
*/
inline io_t & io() { return this->io_; }
protected:
/**
* @function : get the recv/read allocator object refrence
*/
inline auto & rallocator() { return this->rallocator_; }
/**
* @function : get the send/write/post allocator object refrence
*/
inline auto & wallocator() { return this->wallocator_; }
inline session_mgr_t<session_t> & sessions() { return this->sessions_; }
inline listener_t & listener() { return this->listener_; }
inline std::atomic<state_t> & state () { return this->state_; }
inline std::shared_ptr<derived_t> selfptr () { return std::shared_ptr<derived_t>{}; }
inline constexpr static bool is_session() { return false; }
inline constexpr static bool is_client () { return false; }
inline constexpr static bool is_server () { return true ; }
protected:
// The memory to use for handler-based custom memory allocation. used for acceptor.
handler_memory<> rallocator_;
/// The memory to use for handler-based custom memory allocation. used fo send/write/post.
handler_memory<size_op<>, std::true_type> wallocator_;
/// listener
listener_t listener_;
/// The io (include io_context and strand) used to handle the accept event.
io_t & io_;
/// state
std::atomic<state_t> state_ = state_t::stopped;
/// session_mgr
session_mgr_t<session_t> sessions_;
/// use this to ensure that server stop only after all sessions are closed
std::shared_ptr<void> counter_ptr_;
};
}
#include <asio2/base/detail/pop_options.hpp>
#endif // !__ASIO2_SERVER_HPP__