-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServer.cpp
67 lines (42 loc) · 1.64 KB
/
HttpServer.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
/*++
Abstract:
HttpServer class
Author:
Rinat Baygildin ([email protected])
--*/
#include "HttpServer.h"
#include <boost/thread.hpp>
namespace http {
namespace server {
CHttpServer::CHttpServer(const std::string& address, const std::string& port, const std::string& folder)
: m_service(), m_service_work(m_service), m_acceptor(m_service, address, port), m_folder(folder) {
Start();
}
void CHttpServer::Start() {
m_connection_ptr.reset(new CConnection(m_service, m_request_handler, m_connections_array, m_folder));
m_acceptor.GetAcceptor().async_accept(m_connection_ptr->GetSocket(),
boost::bind(&CHttpServer::OnAcceptedHandler,
this, boost::asio::placeholders::error));
}
void CHttpServer::Stop() {
m_service.stop();
}
void CHttpServer::Run() {
for(auto i = 0; i < THREADS_COUNT; ++i) {
m_threads_group.create_thread(boost::bind(
&boost::asio::io_service::run, &m_service));
}
m_threads_group.join_all();
}
void CHttpServer::OnAcceptedHandler(const boost::system::error_code& ec) {
if (ec == boost::system::errc::success) {
m_connections_array.Start(m_connection_ptr);
Start();
}
else {
m_acceptor.GetAcceptor().close();
m_connections_array.StopAll();
}
}
} // namespace server
} // namespace http