| 1 |
|
|---|
| 2 | #include "http_server.h"
|
|---|
| 3 | #include "../../engine/logger.h"
|
|---|
| 4 | #include <boost/bind.hpp>
|
|---|
| 5 | #include <signal.h>
|
|---|
| 6 |
|
|---|
| 7 | namespace http_server
|
|---|
| 8 | {
|
|---|
| 9 |
|
|---|
| 10 | using namespace boost::asio::ip;
|
|---|
| 11 | using namespace engine;
|
|---|
| 12 |
|
|---|
| 13 | cHttpServer::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 |
|
|---|
| 28 | cHttpServer::~cHttpServer()
|
|---|
| 29 | {
|
|---|
| 30 | Stop();
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | void 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 |
|
|---|
| 42 | void 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 |
|
|---|
| 57 | void cHttpServer::Start()
|
|---|
| 58 | {
|
|---|
| 59 | m_IOService.run();
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | void 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 |
|
|---|