diff --git a/HTTPServer.cpp b/HTTPServer.cpp index ce336fe7..b99a748c 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -526,15 +526,14 @@ namespace util { m_Stream.reset (); m_Stream = nullptr; - // delete this }); } void HTTPConnection::Receive () { m_Socket->async_read_some (boost::asio::buffer (m_Buffer, HTTP_CONNECTION_BUFFER_SIZE), - boost::bind(&HTTPConnection::HandleReceive, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + std::bind(&HTTPConnection::HandleReceive, shared_from_this (), + std::placeholders::_1, std::placeholders::_2)); } void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred) @@ -968,8 +967,8 @@ namespace util m_BufferLen = len; i2p::client::context.GetSharedLocalDestination ()->RequestDestination (destination); m_Timer.expires_from_now (boost::posix_time::seconds(HTTP_DESTINATION_REQUEST_TIMEOUT)); - m_Timer.async_wait (boost::bind (&HTTPConnection::HandleDestinationRequestTimeout, - this, boost::asio::placeholders::error, destination, port, m_Buffer, m_BufferLen)); + m_Timer.async_wait (std::bind (&HTTPConnection::HandleDestinationRequestTimeout, + shared_from_this (), std::placeholders::_1, destination, port, m_Buffer, m_BufferLen)); } } @@ -1002,8 +1001,8 @@ namespace util { if (m_Stream) m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), - boost::bind (&HTTPConnection::HandleStreamReceive, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred), + std::bind (&HTTPConnection::HandleStreamReceive, shared_from_this (), + std::placeholders::_1, std::placeholders::_2), 45); // 45 seconds timeout } @@ -1012,7 +1011,7 @@ namespace util if (!ecode) { boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred), - boost::bind (&HTTPConnection::HandleWrite, this, boost::asio::placeholders::error)); + std::bind (&HTTPConnection::HandleWrite, shared_from_this (), std::placeholders::_1)); } else { @@ -1033,8 +1032,7 @@ namespace util m_Reply.headers[1].value = "text/html"; boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status), - boost::bind (&HTTPConnection::HandleWriteReply, this, - boost::asio::placeholders::error)); + std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1)); } HTTPServer::HTTPServer (int port): @@ -1085,14 +1083,15 @@ namespace util { if (!ecode) { - CreateConnection(m_NewSocket); // new HTTPConnection(m_NewSocket); + CreateConnection(m_NewSocket); Accept (); } } void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket) { - new HTTPConnection (m_NewSocket); + auto conn = std::make_shared (m_NewSocket); + conn->Receive (); } } } diff --git a/HTTPServer.h b/HTTPServer.h index 4db3486b..b289cbc5 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -15,7 +15,7 @@ namespace util { const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192; const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds - class HTTPConnection + class HTTPConnection: public std::enable_shared_from_this { protected: @@ -48,13 +48,13 @@ namespace util HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket), m_Timer (socket->get_io_service ()), - m_Stream (nullptr), m_BufferLen (0) { Receive (); }; - virtual ~HTTPConnection() { delete m_Socket; } - + m_Stream (nullptr), m_BufferLen (0) {}; + ~HTTPConnection() { delete m_Socket; } + void Receive (); + private: void Terminate (); - void Receive (); void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void AsyncStreamReceive (); void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);