mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-13 15:27:52 +00:00
send multiple messages though single write call
This commit is contained in:
parent
ea353ac3ba
commit
c61cd350ee
@ -547,6 +547,12 @@ namespace transport
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NTCPSession::Send (i2p::I2NPMessage * msg)
|
void NTCPSession::Send (i2p::I2NPMessage * msg)
|
||||||
|
{
|
||||||
|
boost::asio::async_write (m_Socket, CreateMsgBuffer (msg), boost::asio::transfer_all (),
|
||||||
|
std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::asio::const_buffers_1 NTCPSession::CreateMsgBuffer (I2NPMessage * msg)
|
||||||
{
|
{
|
||||||
uint8_t * sendBuffer;
|
uint8_t * sendBuffer;
|
||||||
int len;
|
int len;
|
||||||
@ -579,10 +585,8 @@ namespace transport
|
|||||||
|
|
||||||
int l = len + padding + 6;
|
int l = len + padding + 6;
|
||||||
m_Encryption.Encrypt(sendBuffer, l, sendBuffer);
|
m_Encryption.Encrypt(sendBuffer, l, sendBuffer);
|
||||||
|
return boost::asio::buffer ((const uint8_t *)sendBuffer, l);
|
||||||
boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (),
|
}
|
||||||
std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
|
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
|
||||||
{
|
{
|
||||||
@ -602,6 +606,34 @@ namespace transport
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NTCPSession::Send (const std::vector<I2NPMessage *>& msgs)
|
||||||
|
{
|
||||||
|
std::vector<boost::asio::const_buffer> bufs;
|
||||||
|
for (auto it: msgs)
|
||||||
|
bufs.push_back (CreateMsgBuffer (it));
|
||||||
|
boost::asio::async_write (m_Socket, bufs, boost::asio::transfer_all (),
|
||||||
|
std::bind(&NTCPSession::HandleBatchSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msgs));
|
||||||
|
}
|
||||||
|
|
||||||
|
void NTCPSession::HandleBatchSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<I2NPMessage *> msgs)
|
||||||
|
{
|
||||||
|
for (auto it: msgs)
|
||||||
|
if (it) i2p::DeleteI2NPMessage (it);
|
||||||
|
if (ecode)
|
||||||
|
{
|
||||||
|
LogPrint (eLogWarning, "Couldn't send msgs: ", ecode.message ());
|
||||||
|
// we shouldn't call Terminate () here, because HandleReceive takes care
|
||||||
|
// TODO: 'delete this' statement in Terminate () must be eliminated later
|
||||||
|
// Terminate ();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_NumSentBytes += bytes_transferred;
|
||||||
|
ScheduleTermination (); // reset termination timer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void NTCPSession::SendTimeSyncMessage ()
|
void NTCPSession::SendTimeSyncMessage ()
|
||||||
{
|
{
|
||||||
Send (nullptr);
|
Send (nullptr);
|
||||||
@ -625,8 +657,7 @@ namespace transport
|
|||||||
|
|
||||||
void NTCPSession::PostI2NPMessages (std::vector<I2NPMessage *> msgs)
|
void NTCPSession::PostI2NPMessages (std::vector<I2NPMessage *> msgs)
|
||||||
{
|
{
|
||||||
for (auto it: msgs)
|
Send (msgs);
|
||||||
if (it) Send (it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NTCPSession::ScheduleTermination ()
|
void NTCPSession::ScheduleTermination ()
|
||||||
|
@ -101,9 +101,12 @@ namespace transport
|
|||||||
bool DecryptNextBlock (const uint8_t * encrypted);
|
bool DecryptNextBlock (const uint8_t * encrypted);
|
||||||
|
|
||||||
void Send (i2p::I2NPMessage * msg);
|
void Send (i2p::I2NPMessage * msg);
|
||||||
|
boost::asio::const_buffers_1 CreateMsgBuffer (I2NPMessage * msg);
|
||||||
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg);
|
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg);
|
||||||
|
void Send (const std::vector<I2NPMessage *>& msgs);
|
||||||
|
void HandleBatchSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<I2NPMessage *> msgs);
|
||||||
|
|
||||||
|
|
||||||
// timer
|
// timer
|
||||||
void ScheduleTermination ();
|
void ScheduleTermination ();
|
||||||
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
||||||
|
Loading…
Reference in New Issue
Block a user