Browse Source

allocated bigger buffer for remaining data

pull/766/head
orignal 8 years ago
parent
commit
2e1c508bc4
  1. 91
      NTCPSession.cpp

91
NTCPSession.cpp

@ -493,7 +493,8 @@ namespace transport
void NTCPSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred) void NTCPSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{ {
if (ecode) { if (ecode)
{
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
LogPrint (eLogDebug, "NTCP: Read error: ", ecode.message ()); LogPrint (eLogDebug, "NTCP: Read error: ", ecode.message ());
//if (ecode != boost::asio::error::operation_aborted) //if (ecode != boost::asio::error::operation_aborted)
@ -507,51 +508,57 @@ namespace transport
if (m_ReceiveBufferOffset >= 16) if (m_ReceiveBufferOffset >= 16)
{ {
int numReloads = 0; // process received data
do uint8_t * nextBlock = m_ReceiveBuffer;
{ while (m_ReceiveBufferOffset >= 16)
uint8_t * nextBlock = m_ReceiveBuffer; {
while (m_ReceiveBufferOffset >= 16) if (!DecryptNextBlock (nextBlock)) // 16 bytes
{ {
if (!DecryptNextBlock (nextBlock)) // 16 bytes Terminate ();
{ return;
Terminate ();
return;
}
nextBlock += 16;
m_ReceiveBufferOffset -= 16;
} }
if (m_ReceiveBufferOffset > 0) nextBlock += 16;
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset); m_ReceiveBufferOffset -= 16;
}
// try to read more if (m_ReceiveBufferOffset > 0)
if (numReloads < 16) // ~16K memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
{ }
boost::system::error_code ec;
size_t moreBytes = m_Socket.available(ec); // read and process more is available
if (moreBytes && !ec) boost::system::error_code ec;
{ size_t moreBytes = m_Socket.available(ec);
if (moreBytes > NTCP_BUFFER_SIZE - m_ReceiveBufferOffset) if (moreBytes && !ec)
moreBytes = NTCP_BUFFER_SIZE - m_ReceiveBufferOffset; {
moreBytes = m_Socket.read_some (boost::asio::buffer (m_ReceiveBuffer + m_ReceiveBufferOffset, moreBytes), ec); uint8_t * buf = nullptr, * moreBuf = m_ReceiveBuffer;
if (ec) if (moreBytes + m_ReceiveBufferOffset > NTCP_BUFFER_SIZE)
{ {
LogPrint (eLogInfo, "NTCP: Read more bytes error: ", ec.message ()); buf = new uint8_t[moreBytes + m_ReceiveBufferOffset];
Terminate (); if (m_ReceiveBufferOffset)
return; memcpy (buf, m_ReceiveBuffer, m_ReceiveBufferOffset);
} moreBuf = buf;
m_NumReceivedBytes += moreBytes; }
i2p::transport::transports.UpdateReceivedBytes (moreBytes); moreBytes = m_Socket.read_some (boost::asio::buffer (moreBuf + m_ReceiveBufferOffset, moreBytes), ec);
m_ReceiveBufferOffset += moreBytes; m_ReceiveBufferOffset += moreBytes;
numReloads++; m_NumReceivedBytes += moreBytes;
} i2p::transport::transports.UpdateReceivedBytes (moreBytes);
else // process more data
break; // no more data uint8_t * nextBlock = moreBuf;
while (m_ReceiveBufferOffset >= 16)
{
if (!DecryptNextBlock (nextBlock)) // 16 bytes
{
delete[] buf;
Terminate ();
return;
} }
nextBlock += 16;
m_ReceiveBufferOffset -= 16;
} }
while (m_ReceiveBufferOffset >= 16); if (m_ReceiveBufferOffset > 0)
m_Handler.Flush (); memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset); // nextBlock points to memory inside buf
} delete[] buf;
}
m_Handler.Flush ();
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch (); m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
Receive (); Receive ();

Loading…
Cancel
Save