-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_listener.cpp
87 lines (62 loc) · 1.6 KB
/
users_listener.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
#include "users_listener.hpp"
users_listener::users_listener(boost::asio::io_service& ios, boost::asio::ssl::context& ctx, tcp::endpoint endpoint)
: ios_(ios), strand_(ios), acceptor_(ios), socket_(ios), ctx_(ctx)
{
boost::system::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if (ec)
{
logger::fail(ec, "open");
return;
}
// Bind to the server address
acceptor_.bind(endpoint, ec);
if (ec)
{
logger::fail(ec, "bind");
return;
}
// Start listening for connections
acceptor_.listen(boost::asio::socket_base::max_connections, ec);
if (ec)
{
logger::fail(ec, "listen");
return;
}
}
// Start accepting incoming connections
void users_listener::run()
{
if (!acceptor_.is_open())
{
logger::fail("Acceptor not open");
return;
}
do_accept();
}
void users_listener::do_accept()
{
acceptor_.async_accept(
socket_,
std::bind(
&users_listener::on_accept,
shared_from_this(),
std::placeholders::_1));
}
void users_listener::on_accept(boost::system::error_code ec)
{
// Accept another connection
if (ec)
{
logger::fail(ec, "accept");
}
else
{
logger::debug("Attempt for user connection");
// Create the session and run it
std::make_shared<user_client>(ios_, ctx_, std::move(socket_))->run();
}
socket_ = tcp::socket(ios_);
do_accept();
}