Browse Source

fixed memory leak

pull/177/head
orignal 10 years ago
parent
commit
62593f60c5
  1. 23
      HTTPServer.cpp
  2. 10
      HTTPServer.h

23
HTTPServer.cpp

@ -526,15 +526,14 @@ namespace util
{ {
m_Stream.reset (); m_Stream.reset ();
m_Stream = nullptr; m_Stream = nullptr;
// delete this
}); });
} }
void HTTPConnection::Receive () void HTTPConnection::Receive ()
{ {
m_Socket->async_read_some (boost::asio::buffer (m_Buffer, HTTP_CONNECTION_BUFFER_SIZE), m_Socket->async_read_some (boost::asio::buffer (m_Buffer, HTTP_CONNECTION_BUFFER_SIZE),
boost::bind(&HTTPConnection::HandleReceive, this, std::bind(&HTTPConnection::HandleReceive, shared_from_this (),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); std::placeholders::_1, std::placeholders::_2));
} }
void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred) void HTTPConnection::HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
@ -968,8 +967,8 @@ namespace util
m_BufferLen = len; m_BufferLen = len;
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (destination); i2p::client::context.GetSharedLocalDestination ()->RequestDestination (destination);
m_Timer.expires_from_now (boost::posix_time::seconds(HTTP_DESTINATION_REQUEST_TIMEOUT)); m_Timer.expires_from_now (boost::posix_time::seconds(HTTP_DESTINATION_REQUEST_TIMEOUT));
m_Timer.async_wait (boost::bind (&HTTPConnection::HandleDestinationRequestTimeout, m_Timer.async_wait (std::bind (&HTTPConnection::HandleDestinationRequestTimeout,
this, boost::asio::placeholders::error, destination, port, m_Buffer, m_BufferLen)); shared_from_this (), std::placeholders::_1, destination, port, m_Buffer, m_BufferLen));
} }
} }
@ -1002,8 +1001,8 @@ namespace util
{ {
if (m_Stream) if (m_Stream)
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
boost::bind (&HTTPConnection::HandleStreamReceive, this, std::bind (&HTTPConnection::HandleStreamReceive, shared_from_this (),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred), std::placeholders::_1, std::placeholders::_2),
45); // 45 seconds timeout 45); // 45 seconds timeout
} }
@ -1012,7 +1011,7 @@ namespace util
if (!ecode) if (!ecode)
{ {
boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred), 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 else
{ {
@ -1033,8 +1032,7 @@ namespace util
m_Reply.headers[1].value = "text/html"; m_Reply.headers[1].value = "text/html";
boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status), boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status),
boost::bind (&HTTPConnection::HandleWriteReply, this, std::bind (&HTTPConnection::HandleWriteReply, shared_from_this (), std::placeholders::_1));
boost::asio::placeholders::error));
} }
HTTPServer::HTTPServer (int port): HTTPServer::HTTPServer (int port):
@ -1085,14 +1083,15 @@ namespace util
{ {
if (!ecode) if (!ecode)
{ {
CreateConnection(m_NewSocket); // new HTTPConnection(m_NewSocket); CreateConnection(m_NewSocket);
Accept (); Accept ();
} }
} }
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket) void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket)
{ {
new HTTPConnection (m_NewSocket); auto conn = std::make_shared<HTTPConnection> (m_NewSocket);
conn->Receive ();
} }
} }
} }

10
HTTPServer.h

@ -15,7 +15,7 @@ namespace util
{ {
const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192; const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192;
const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds const int HTTP_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds
class HTTPConnection class HTTPConnection: public std::enable_shared_from_this<HTTPConnection>
{ {
protected: protected:
@ -48,13 +48,13 @@ namespace util
HTTPConnection (boost::asio::ip::tcp::socket * socket): HTTPConnection (boost::asio::ip::tcp::socket * socket):
m_Socket (socket), m_Timer (socket->get_io_service ()), m_Socket (socket), m_Timer (socket->get_io_service ()),
m_Stream (nullptr), m_BufferLen (0) { Receive (); }; m_Stream (nullptr), m_BufferLen (0) {};
virtual ~HTTPConnection() { delete m_Socket; } ~HTTPConnection() { delete m_Socket; }
void Receive ();
private: private:
void Terminate (); void Terminate ();
void Receive ();
void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void AsyncStreamReceive (); void AsyncStreamReceive ();
void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);

Loading…
Cancel
Save