Browse Source

fixed #629. catch HTTPServer exceptions

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

17
HTTPServer.cpp

@ -769,7 +769,7 @@ namespace http { @@ -769,7 +769,7 @@ namespace http {
}
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))
{
}
@ -798,6 +798,7 @@ namespace http { @@ -798,6 +798,7 @@ namespace http {
i2p::config::SetOption("http.pass", 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_Acceptor.listen ();
Accept ();
@ -805,18 +806,30 @@ namespace http { @@ -805,18 +806,30 @@ namespace http {
void HTTPServer::Stop ()
{
m_IsRunning = false;
m_Acceptor.close();
m_Service.stop ();
if (m_Thread) {
if (m_Thread)
{
m_Thread->join ();
m_Thread = nullptr;
}
}
void HTTPServer::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "HTTPServer: runtime exception: ", ex.what ());
}
}
}
void HTTPServer::Accept ()
{

1
HTTPServer.h

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

Loading…
Cancel
Save