Browse Source

allocated bigger buffer for remaining data

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

47
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,9 +508,7 @@ namespace transport
if (m_ReceiveBufferOffset >= 16) if (m_ReceiveBufferOffset >= 16)
{ {
int numReloads = 0; // process received data
do
{
uint8_t * nextBlock = m_ReceiveBuffer; uint8_t * nextBlock = m_ReceiveBuffer;
while (m_ReceiveBufferOffset >= 16) while (m_ReceiveBufferOffset >= 16)
{ {
@ -523,35 +522,43 @@ namespace transport
} }
if (m_ReceiveBufferOffset > 0) if (m_ReceiveBufferOffset > 0)
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset); memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
}
// try to read more // read and process more is available
if (numReloads < 16) // ~16K
{
boost::system::error_code ec; boost::system::error_code ec;
size_t moreBytes = m_Socket.available(ec); size_t moreBytes = m_Socket.available(ec);
if (moreBytes && !ec) if (moreBytes && !ec)
{ {
if (moreBytes > NTCP_BUFFER_SIZE - m_ReceiveBufferOffset) uint8_t * buf = nullptr, * moreBuf = m_ReceiveBuffer;
moreBytes = NTCP_BUFFER_SIZE - m_ReceiveBufferOffset; if (moreBytes + m_ReceiveBufferOffset > NTCP_BUFFER_SIZE)
moreBytes = m_Socket.read_some (boost::asio::buffer (m_ReceiveBuffer + m_ReceiveBufferOffset, moreBytes), ec);
if (ec)
{ {
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;
} }
moreBytes = m_Socket.read_some (boost::asio::buffer (moreBuf + m_ReceiveBufferOffset, moreBytes), ec);
m_ReceiveBufferOffset += moreBytes;
m_NumReceivedBytes += moreBytes; m_NumReceivedBytes += moreBytes;
i2p::transport::transports.UpdateReceivedBytes (moreBytes); i2p::transport::transports.UpdateReceivedBytes (moreBytes);
m_ReceiveBufferOffset += moreBytes; // process more data
numReloads++; uint8_t * nextBlock = moreBuf;
while (m_ReceiveBufferOffset >= 16)
{
if (!DecryptNextBlock (nextBlock)) // 16 bytes
{
delete[] buf;
Terminate ();
return;
} }
else nextBlock += 16;
break; // no more data m_ReceiveBufferOffset -= 16;
} }
if (m_ReceiveBufferOffset > 0)
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset); // nextBlock points to memory inside buf
delete[] buf;
} }
while (m_ReceiveBufferOffset >= 16);
m_Handler.Flush (); m_Handler.Flush ();
}
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch (); m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
Receive (); Receive ();

Loading…
Cancel
Save