Browse Source

fixed #629. catch HTTPServer exceptions

pull/633/head
orignal 8 years ago
parent
commit
db71673722
  1. 19
      HTTPServer.cpp
  2. 1
      HTTPServer.h

19
HTTPServer.cpp

@ -769,7 +769,7 @@ namespace http {
} }
HTTPServer::HTTPServer (const std::string& address, int port): HTTPServer::HTTPServer (const std::string& address, int port):
m_Thread (nullptr), m_Work (m_Service), m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)) m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port))
{ {
} }
@ -798,6 +798,7 @@ namespace http {
i2p::config::SetOption("http.pass", pass); i2p::config::SetOption("http.pass", pass);
LogPrint(eLogInfo, "HTTPServer: password set to ", pass); LogPrint(eLogInfo, "HTTPServer: password set to ", pass);
} }
m_IsRunning = true;
m_Thread = std::unique_ptr<std::thread>(new std::thread (std::bind (&HTTPServer::Run, this))); m_Thread = std::unique_ptr<std::thread>(new std::thread (std::bind (&HTTPServer::Run, this)));
m_Acceptor.listen (); m_Acceptor.listen ();
Accept (); Accept ();
@ -805,9 +806,11 @@ namespace http {
void HTTPServer::Stop () void HTTPServer::Stop ()
{ {
m_IsRunning = false;
m_Acceptor.close(); m_Acceptor.close();
m_Service.stop (); m_Service.stop ();
if (m_Thread) { if (m_Thread)
{
m_Thread->join (); m_Thread->join ();
m_Thread = nullptr; m_Thread = nullptr;
} }
@ -815,7 +818,17 @@ namespace http {
void HTTPServer::Run () void HTTPServer::Run ()
{ {
m_Service.run (); while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "HTTPServer: runtime exception: ", ex.what ());
}
}
} }
void HTTPServer::Accept () void HTTPServer::Accept ()

1
HTTPServer.h

@ -57,6 +57,7 @@ namespace http {
private: private:
bool m_IsRunning;
std::unique_ptr<std::thread> m_Thread; std::unique_ptr<std::thread> m_Thread;
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work; boost::asio::io_service::work m_Work;

Loading…
Cancel
Save