mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 13:27:17 +01:00
use shared_ptr for sockets
This commit is contained in:
parent
50a7cd19b4
commit
7943b13891
|
@ -1066,10 +1066,8 @@ namespace util
|
|||
|
||||
HTTPServer::HTTPServer (int port):
|
||||
m_Thread (nullptr), m_Work (m_Service),
|
||||
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4 (), port)),
|
||||
m_NewSocket (nullptr)
|
||||
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4 (), port))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
HTTPServer::~HTTPServer ()
|
||||
|
@ -1079,7 +1077,7 @@ namespace util
|
|||
|
||||
void HTTPServer::Start ()
|
||||
{
|
||||
m_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 ();
|
||||
Accept ();
|
||||
}
|
||||
|
@ -1091,7 +1089,6 @@ namespace util
|
|||
if (m_Thread)
|
||||
{
|
||||
m_Thread->join ();
|
||||
delete m_Thread;
|
||||
m_Thread = nullptr;
|
||||
}
|
||||
}
|
||||
|
@ -1103,23 +1100,24 @@ namespace util
|
|||
|
||||
void HTTPServer::Accept ()
|
||||
{
|
||||
m_NewSocket = new boost::asio::ip::tcp::socket (m_Service);
|
||||
m_Acceptor.async_accept (*m_NewSocket, boost::bind (&HTTPServer::HandleAccept, this,
|
||||
boost::asio::placeholders::error));
|
||||
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (m_Service);
|
||||
m_Acceptor.async_accept (*newSocket, boost::bind (&HTTPServer::HandleAccept, this,
|
||||
boost::asio::placeholders::error, newSocket));
|
||||
}
|
||||
|
||||
void HTTPServer::HandleAccept(const boost::system::error_code& ecode)
|
||||
void HTTPServer::HandleAccept(const boost::system::error_code& ecode,
|
||||
std::shared_ptr<boost::asio::ip::tcp::socket> newSocket)
|
||||
{
|
||||
if (!ecode)
|
||||
{
|
||||
CreateConnection(m_NewSocket);
|
||||
CreateConnection(newSocket);
|
||||
Accept ();
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket)
|
||||
void HTTPServer::CreateConnection(std::shared_ptr<boost::asio::ip::tcp::socket> newSocket)
|
||||
{
|
||||
auto conn = std::make_shared<HTTPConnection> (m_NewSocket);
|
||||
auto conn = std::make_shared<HTTPConnection> (newSocket);
|
||||
conn->Receive ();
|
||||
}
|
||||
}
|
||||
|
|
13
HTTPServer.h
13
HTTPServer.h
|
@ -45,10 +45,9 @@ namespace util
|
|||
|
||||
public:
|
||||
|
||||
HTTPConnection (boost::asio::ip::tcp::socket * socket):
|
||||
HTTPConnection (std::shared_ptr<boost::asio::ip::tcp::socket> socket):
|
||||
m_Socket (socket), m_Timer (socket->get_io_service ()),
|
||||
m_Stream (nullptr), m_BufferLen (0) {};
|
||||
~HTTPConnection() { delete m_Socket; }
|
||||
void Receive ();
|
||||
|
||||
private:
|
||||
|
@ -80,7 +79,7 @@ namespace util
|
|||
|
||||
protected:
|
||||
|
||||
boost::asio::ip::tcp::socket * m_Socket;
|
||||
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
||||
boost::asio::deadline_timer m_Timer;
|
||||
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||||
char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1];
|
||||
|
@ -117,18 +116,18 @@ namespace util
|
|||
|
||||
void Run ();
|
||||
void Accept ();
|
||||
void HandleAccept(const boost::system::error_code& ecode);
|
||||
void HandleAccept(const boost::system::error_code& ecode,
|
||||
std::shared_ptr<boost::asio::ip::tcp::socket> newSocket);
|
||||
|
||||
private:
|
||||
|
||||
std::thread * m_Thread;
|
||||
std::unique_ptr<std::thread> m_Thread;
|
||||
boost::asio::io_service m_Service;
|
||||
boost::asio::io_service::work m_Work;
|
||||
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||||
boost::asio::ip::tcp::socket * m_NewSocket;
|
||||
|
||||
protected:
|
||||
virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket);
|
||||
virtual void CreateConnection(std::shared_ptr<boost::asio::ip::tcp::socket> newSocket);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue