Ticket #5815: http_server.cpp

File http_server.cpp, 1.5 KB (added by mileandrei@…, 11 years ago)
Line 
1
2#include "http_server.h"
3#include "../../engine/logger.h"
4#include <boost/bind.hpp>
5#include <signal.h>
6
7namespace http_server
8{
9
10using namespace boost::asio::ip;
11using namespace engine;
12
13cHttpServer::cHttpServer(unsigned int port)
14 :m_IOService(),
15 m_Acceptor(m_IOService, tcp::endpoint(tcp::v4(), port)),
16 m_Signals(m_IOService)
17{
18 m_Signals.add(SIGINT);
19 m_Signals.add(SIGTERM);
20#if defined(SIGQUIT)
21 m_Signals.add(SIGQUIT);
22#endif // defined(SIGQUIT)
23 m_Signals.async_wait(boost::bind(&cHttpServer::Stop, this));
24
25 StartAccept();
26};
27
28cHttpServer::~cHttpServer()
29{
30 Stop();
31};
32
33void cHttpServer::StartAccept()
34{
35 connection_ptr new_connection = cHttpConnection::Create(m_Acceptor.get_io_service(), m_ConnectionManager);
36
37 m_Acceptor.async_accept(new_connection->GetSocket(),
38 boost::bind(&cHttpServer::HandleConnection, this, new_connection,
39 boost::asio::placeholders::error));
40};
41
42void cHttpServer::HandleConnection(connection_ptr new_connection,
43 const boost::system::error_code& error)
44{
45 if (!error)
46 {
47 m_ConnectionManager.StartConnection(new_connection);
48 StartAccept();
49 }
50 else
51 {
52 cLogger log(LOG_SEV_ERROR);
53 log<< CONTEXT_STR + error.message();
54 }
55};
56
57void cHttpServer::Start()
58{
59 m_IOService.run();
60};
61
62void cHttpServer::Stop()
63{
64 cLogger log(LOG_SEV_INFO);
65 log<< CONTEXT_STR + "Server is stopping...ignore further errors";
66 m_Acceptor.close();
67 m_ConnectionManager.StopAllConnections();
68};
69
70
71
72}
73
74