Browse Source

insert multiple packets to the queue using splice

pull/2108/head
orignal 1 month ago
parent
commit
ec1f41b13c
  1. 44
      libi2pd/SSU2.cpp
  2. 2
      libi2pd/SSU2.h

44
libi2pd/SSU2.cpp

@ -367,23 +367,22 @@ namespace transport
return; return;
} }
packet->len = bytes_transferred; packet->len = bytes_transferred;
InsertToReceivedPacketsQueue (packet);
size_t numPackets = 1;
boost::system::error_code ec; boost::system::error_code ec;
size_t moreBytes = socket.available (ec); size_t moreBytes = socket.available (ec);
if (!ec && moreBytes) if (!ec && moreBytes)
{ {
do std::list<Packet *> packets;
packets.push_back (packet);
while (moreBytes && packets.size () < SSU2_MAX_NUM_PACKETS_PER_BATCH)
{ {
packet = m_PacketsPool.AcquireMt (); packet = m_PacketsPool.AcquireMt ();
packet->len = socket.receive_from (boost::asio::buffer (packet->buf, SSU2_MAX_PACKET_SIZE), packet->from, 0, ec); packet->len = socket.receive_from (boost::asio::buffer (packet->buf, SSU2_MAX_PACKET_SIZE), packet->from, 0, ec);
if (!ec) if (!ec)
{ {
i2p::transport::transports.UpdateReceivedBytes (packet->len); i2p::transport::transports.UpdateReceivedBytes (packet->len);
numPackets++;
if (packet->len >= SSU2_MIN_RECEIVED_PACKET_SIZE) if (packet->len >= SSU2_MIN_RECEIVED_PACKET_SIZE)
InsertToReceivedPacketsQueue (packet); packets.push_back (packet);
else // drop too short packets else // drop too short packets
m_PacketsPool.ReleaseMt (packet); m_PacketsPool.ReleaseMt (packet);
moreBytes = socket.available(ec); moreBytes = socket.available(ec);
@ -396,8 +395,10 @@ namespace transport
break; break;
} }
} }
while (moreBytes && numPackets < SSU2_MAX_NUM_PACKETS_PER_BATCH); InsertToReceivedPacketsQueue (packets);
} }
else
InsertToReceivedPacketsQueue (packet);
Receive (socket); Receive (socket);
} }
else else
@ -448,17 +449,30 @@ namespace transport
m_ReceivedPacketsQueue.push_back (packet); m_ReceivedPacketsQueue.push_back (packet);
} }
if (empty) if (empty)
GetService ().post([this]() { HandleReceivedPacketsQueue (); });
}
void SSU2Server::InsertToReceivedPacketsQueue (std::list<Packet *>& packets)
{
if (packets.empty ()) return;
bool empty = false;
{ {
GetService ().post([this]() std::lock_guard<std::mutex> l(m_ReceivedPacketsQueueMutex);
{ empty = m_ReceivedPacketsQueue.empty ();
std::list<Packet *> receivedPackets; m_ReceivedPacketsQueue.splice (m_ReceivedPacketsQueue.end (), packets);
{ }
std::lock_guard<std::mutex> l(m_ReceivedPacketsQueueMutex); if (empty)
m_ReceivedPacketsQueue.swap (receivedPackets); GetService ().post([this]() { HandleReceivedPacketsQueue (); });
} }
HandleReceivedPackets (std::move (receivedPackets));
}); void SSU2Server::HandleReceivedPacketsQueue ()
{
std::list<Packet *> receivedPackets;
{
std::lock_guard<std::mutex> l(m_ReceivedPacketsQueueMutex);
m_ReceivedPacketsQueue.swap (receivedPackets);
} }
HandleReceivedPackets (std::move (receivedPackets));
} }
void SSU2Server::AddSession (std::shared_ptr<SSU2Session> session) void SSU2Server::AddSession (std::shared_ptr<SSU2Session> session)

2
libi2pd/SSU2.h

@ -135,6 +135,8 @@ namespace transport
void HandleReceivedPackets (std::list<Packet *>&& packets); void HandleReceivedPackets (std::list<Packet *>&& packets);
void ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); void ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
void InsertToReceivedPacketsQueue (Packet * packet); void InsertToReceivedPacketsQueue (Packet * packet);
void InsertToReceivedPacketsQueue (std::list<Packet *>& packets);
void HandleReceivedPacketsQueue ();
void ScheduleTermination (); void ScheduleTermination ();
void HandleTerminationTimer (const boost::system::error_code& ecode); void HandleTerminationTimer (const boost::system::error_code& ecode);

Loading…
Cancel
Save