generic SocketsPipe for different socket types

This commit is contained in:
orignal 2024-02-14 15:35:47 -05:00
parent a9ad6fc31e
commit 710b27688b
2 changed files with 76 additions and 179 deletions

View file

@ -148,162 +148,6 @@ namespace client
}
}
SocketsPipe::SocketsPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream) : I2PServiceHandler(owner), m_up(upstream), m_down(downstream)
{
boost::asio::socket_base::receive_buffer_size option(SOCKETS_PIPE_BUFFER_SIZE);
upstream->set_option(option);
downstream->set_option(option);
}
SocketsPipe::~SocketsPipe()
{
Terminate();
}
void SocketsPipe::Start()
{
AsyncReceiveUpstream();
AsyncReceiveDownstream();
}
void SocketsPipe::Terminate()
{
if(Kill()) return;
if (m_up)
{
if (m_up->is_open())
m_up->close();
m_up = nullptr;
}
if (m_down)
{
if (m_down->is_open())
m_down->close();
m_down = nullptr;
}
Done(shared_from_this());
}
void SocketsPipe::AsyncReceiveUpstream()
{
if (m_up)
{
m_up->async_read_some(boost::asio::buffer(m_upstream_to_down_buf, SOCKETS_PIPE_BUFFER_SIZE),
std::bind(&SocketsPipe::HandleUpstreamReceived, shared_from_this(),
std::placeholders::_1, std::placeholders::_2));
}
else
LogPrint(eLogError, "SocketsPipe: Upstream receive: No socket");
}
void SocketsPipe::AsyncReceiveDownstream()
{
if (m_down) {
m_down->async_read_some(boost::asio::buffer(m_downstream_to_up_buf, SOCKETS_PIPE_BUFFER_SIZE),
std::bind(&SocketsPipe::HandleDownstreamReceived, shared_from_this(),
std::placeholders::_1, std::placeholders::_2));
}
else
LogPrint(eLogError, "SocketsPipe: Downstream receive: No socket");
}
void SocketsPipe::UpstreamWrite(size_t len)
{
if (m_up)
{
LogPrint(eLogDebug, "SocketsPipe: Upstream: ", (int) len, " bytes written");
boost::asio::async_write(*m_up, boost::asio::buffer(m_upstream_buf, len),
boost::asio::transfer_all(),
std::bind(&SocketsPipe::HandleUpstreamWrite,
shared_from_this(),
std::placeholders::_1));
}
else
LogPrint(eLogError, "SocketsPipe: Upstream write: no socket");
}
void SocketsPipe::DownstreamWrite(size_t len)
{
if (m_down)
{
LogPrint(eLogDebug, "TCPIPPipe: Downstream: ", (int) len, " bytes written");
boost::asio::async_write(*m_down, boost::asio::buffer(m_downstream_buf, len),
boost::asio::transfer_all(),
std::bind(&SocketsPipe::HandleDownstreamWrite,
shared_from_this(),
std::placeholders::_1));
}
else
LogPrint(eLogError, "TCPIPPipe: Downstream write: No socket");
}
void SocketsPipe::HandleDownstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
{
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint(eLogDebug, "TCPIPPipe: Downstream: ", (int) bytes_transfered, " bytes received");
if (ecode)
{
LogPrint(eLogWarning, "TCPIPPipe: Downstream read error:" , ecode.message());
Terminate();
}
else
{
if (bytes_transfered > 0 )
memcpy(m_upstream_buf, m_downstream_to_up_buf, bytes_transfered);
UpstreamWrite(bytes_transfered);
}
}
}
void SocketsPipe::HandleDownstreamWrite(const boost::system::error_code & ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
if (ecode)
{
LogPrint(eLogWarning, "TCPIPPipe: Downstream write error:" , ecode.message());
Terminate();
}
else
AsyncReceiveUpstream();
}
}
void SocketsPipe::HandleUpstreamWrite(const boost::system::error_code & ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
if (ecode)
{
LogPrint(eLogWarning, "SocketsPipe: Upstream write error:" , ecode.message());
Terminate();
}
else
AsyncReceiveDownstream();
}
}
void SocketsPipe::HandleUpstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
{
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint(eLogDebug, "SocketsPipe: Upstream ", (int)bytes_transfered, " bytes received");
if (ecode)
{
LogPrint(eLogWarning, "SocketsPipe: Upstream read error:" , ecode.message());
Terminate();
}
else
{
if (bytes_transfered > 0 )
memcpy(m_downstream_buf, m_upstream_to_down_buf, bytes_transfered);
DownstreamWrite(bytes_transfered);
}
}
}
void TCPIPAcceptor::Start ()
{
m_Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), m_LocalEndpoint));