5 #ifndef PACKIO_SERVER_H
6 #define PACKIO_SERVER_H
14 #include "internal/config.h"
15 #include "internal/log.h"
16 #include "internal/utils.h"
26 template <
typename Rpc,
typename Acceptor,
typename Dispatcher = dispatcher<Rpc>>
28 :
public std::enable_shared_from_this<server<Rpc, Acceptor, Dispatcher>> {
35 typename acceptor_type::executor_type;
37 std::declval<acceptor_type>().accept())>;
40 using std::enable_shared_from_this<server<Rpc, Acceptor, Dispatcher>>::shared_from_this;
63 std::shared_ptr<dispatcher_type>
dispatcher() {
return dispatcher_ptr_; }
65 std::shared_ptr<const dispatcher_type>
dispatcher()
const
67 return dispatcher_ptr_;
78 template <PACKIO_COMPLETION_TOKEN_FOR(
void(error_code, std::shared_ptr<session_type>))
79 ServeHandler PACKIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
81 ServeHandler&& handler PACKIO_DEFAULT_COMPLETION_TOKEN(
executor_type))
83 return net::async_initiate<
85 void(error_code, std::shared_ptr<session_type>)>(
86 initiate_async_serve(
this), handler);
92 async_serve([
self = shared_from_this()](
auto ec,
auto session) {
98 self->async_serve_forever();
103 class initiate_async_serve {
107 explicit initiate_async_serve(
server*
self) : self_(self) {}
111 return self_->get_executor();
114 template <
typename ServeHandler>
115 void operator()(ServeHandler&& handler)
117 PACKIO_STATIC_ASSERT_TTRAIT(ServeHandler, session_type);
118 PACKIO_TRACE(
"async_serve");
120 self_->acceptor_.async_accept(
121 [
self = self_->shared_from_this(),
122 handler = std::forward<ServeHandler>(handler)](
124 std::shared_ptr<session_type> session;
126 PACKIO_WARN(
"accept error: {}", ec.message());
129 internal::set_no_delay(sock);
130 session = std::make_shared<session_type>(
131 std::move(sock), self->dispatcher_ptr_);
133 handler(ec, std::move(session));
141 acceptor_type acceptor_;
142 std::shared_ptr<dispatcher_type> dispatcher_ptr_;
149 template <
typename Rpc,
typename Acceptor,
typename Dispatcher = dispatcher<Rpc>>
152 return std::make_shared<server<Rpc, Acceptor, Dispatcher>>(
153 std::forward<Acceptor>(acceptor));
The dispatcher class, used to store and dispatch procedures.
Definition: dispatcher.h:33
The server_session class, created by the server.
Definition: server_session.h:26
The server class.
Definition: server.h:28
server(acceptor_type acceptor)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: server.h:52
const acceptor_type & acceptor() const
Get the underlying acceptor, const.
Definition: server.h:60
Acceptor acceptor_type
The acceptor type.
Definition: server.h:31
typename acceptor_type::executor_type executor_type
The executor type.
Definition: server.h:35
std::shared_ptr< const dispatcher_type > dispatcher() const
Get the dispatcher, const.
Definition: server.h:65
auto async_serve(ServeHandler &&handler=typename net::default_completion_token< executor_type >::type())
Accept one connection and initialize a session for it.
Definition: server.h:80
server(acceptor_type acceptor, std::shared_ptr< dispatcher_type > dispatcher)
The constructor.
Definition: server.h:46
void async_serve_forever()
Accept connections and automatically start the associated sessions forever.
Definition: server.h:90
std::shared_ptr< dispatcher_type > dispatcher()
Get the dispatcher.
Definition: server.h:63
executor_type get_executor()
Get the executor associated with the object.
Definition: server.h:71
Rpc rpc_type
The RPC protocol type.
Definition: server.h:30
acceptor_type & acceptor()
Get the underlying acceptor.
Definition: server.h:58
std::decay_t< decltype(std::declval< acceptor_type >().accept())> socket_type
The connection socket type.
Definition: server.h:37
typename Acceptor::protocol_type protocol_type
The protocol type.
Definition: server.h:32
Dispatcher dispatcher_type
The dispatcher type.
Definition: server.h:33
The packio namespace.
Definition: arg.h:14
auto make_server(Acceptor &&acceptor)
Create a server from an acceptor.
Definition: server.h:150