-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCServer.cpp
More file actions
47 lines (37 loc) · 1.19 KB
/
CServer.cpp
File metadata and controls
47 lines (37 loc) · 1.19 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
#include "CServer.h"
void CServer::Start()
{
LOG(INFO) << "Server started at: " << acceptor_.local_endpoint() << std::endl;
// init first client
VLOG(1) << "DEBUG: init first client" << std::endl;
CClientSession::ptr client = CClientSession::new_(io_context);
// accept first client
VLOG(1) << "DEBUG: accept first client";
acceptor_.async_accept(client->sock(), boost::bind(&CServer::do_accept, this, client, _1));
// start listen
VLOG(1) << "DEBUG: start listening" << std::endl;
start_listen();
threads.join_all();
}
void CServer::do_accept(CClientSession::ptr client, const boost::system::error_code & err)
{
// if err != 0, CHECK will write to log and exit with error.
CHECK(!err) << "\nAccepting client faild with error: " << err << " . Closing server...";
client->start();
CClientSession::ptr new_client = CClientSession::new_(io_context);
VLOG(1) << "DEBUG: accept NEXT client";
acceptor_.async_accept(new_client->sock(), boost::bind(&CServer::do_accept, this, new_client, _1));
}
void CServer::start_listen()
{
// run io_context in thread_num_ of threads
for( int i = 0; i < thread_num_; ++i )
{
threads.create_thread(
[this]()
{
this->io_context.run();
}
);
}
}