mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00: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):
|
HTTPServer::HTTPServer (int port):
|
||||||
m_Thread (nullptr), m_Work (m_Service),
|
m_Thread (nullptr), m_Work (m_Service),
|
||||||
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4 (), port)),
|
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4 (), port))
|
||||||
m_NewSocket (nullptr)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPServer::~HTTPServer ()
|
HTTPServer::~HTTPServer ()
|
||||||
@ -1079,7 +1077,7 @@ namespace util
|
|||||||
|
|
||||||
void HTTPServer::Start ()
|
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 ();
|
m_Acceptor.listen ();
|
||||||
Accept ();
|
Accept ();
|
||||||
}
|
}
|
||||||
@ -1091,7 +1089,6 @@ namespace util
|
|||||||
if (m_Thread)
|
if (m_Thread)
|
||||||
{
|
{
|
||||||
m_Thread->join ();
|
m_Thread->join ();
|
||||||
delete m_Thread;
|
|
||||||
m_Thread = nullptr;
|
m_Thread = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1103,23 +1100,24 @@ namespace util
|
|||||||
|
|
||||||
void HTTPServer::Accept ()
|
void HTTPServer::Accept ()
|
||||||
{
|
{
|
||||||
m_NewSocket = new boost::asio::ip::tcp::socket (m_Service);
|
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (m_Service);
|
||||||
m_Acceptor.async_accept (*m_NewSocket, boost::bind (&HTTPServer::HandleAccept, this,
|
m_Acceptor.async_accept (*newSocket, boost::bind (&HTTPServer::HandleAccept, this,
|
||||||
boost::asio::placeholders::error));
|
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)
|
if (!ecode)
|
||||||
{
|
{
|
||||||
CreateConnection(m_NewSocket);
|
CreateConnection(newSocket);
|
||||||
Accept ();
|
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 ();
|
conn->Receive ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
HTTPServer.h
13
HTTPServer.h
@ -45,10 +45,9 @@ namespace util
|
|||||||
|
|
||||||
public:
|
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_Socket (socket), m_Timer (socket->get_io_service ()),
|
||||||
m_Stream (nullptr), m_BufferLen (0) {};
|
m_Stream (nullptr), m_BufferLen (0) {};
|
||||||
~HTTPConnection() { delete m_Socket; }
|
|
||||||
void Receive ();
|
void Receive ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -80,7 +79,7 @@ namespace util
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
boost::asio::ip::tcp::socket * m_Socket;
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
||||||
boost::asio::deadline_timer m_Timer;
|
boost::asio::deadline_timer m_Timer;
|
||||||
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||||||
char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1];
|
char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1], m_StreamBuffer[HTTP_CONNECTION_BUFFER_SIZE + 1];
|
||||||
@ -117,18 +116,18 @@ namespace util
|
|||||||
|
|
||||||
void Run ();
|
void Run ();
|
||||||
void Accept ();
|
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:
|
private:
|
||||||
|
|
||||||
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;
|
||||||
boost::asio::ip::tcp::acceptor m_Acceptor;
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||||||
boost::asio::ip::tcp::socket * m_NewSocket;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket);
|
virtual void CreateConnection(std::shared_ptr<boost::asio::ip::tcp::socket> newSocket);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user