Browse Source

reuse NTCP2 frame buffers

pull/1221/head
orignal 6 years ago
parent
commit
f7728aa1f6
  1. 11
      libi2pd/NTCP2.cpp
  2. 8
      libi2pd/NTCP2.h

11
libi2pd/NTCP2.cpp

@ -143,7 +143,7 @@ namespace transport
m_Server (server), m_Socket (m_Server.GetService ()), m_Server (server), m_Socket (m_Server.GetService ()),
m_IsEstablished (false), m_IsTerminated (false), m_IsEstablished (false), m_IsTerminated (false),
m_SessionRequestBuffer (nullptr), m_SessionCreatedBuffer (nullptr), m_SessionConfirmedBuffer (nullptr), m_SessionRequestBuffer (nullptr), m_SessionCreatedBuffer (nullptr), m_SessionConfirmedBuffer (nullptr),
m_NextReceivedBuffer (nullptr), m_NextSendBuffer (nullptr), m_NextReceivedLen (0), m_NextReceivedBuffer (nullptr), m_NextSendBuffer (nullptr),
m_ReceiveSequenceNumber (0), m_SendSequenceNumber (0), m_IsSending (false) m_ReceiveSequenceNumber (0), m_SendSequenceNumber (0), m_IsSending (false)
{ {
m_Establisher.reset (new NTCP2Establisher); m_Establisher.reset (new NTCP2Establisher);
@ -648,10 +648,14 @@ namespace transport
else else
{ {
i2p::crypto::Siphash<8> (m_ReceiveIV, m_ReceiveIV, 8, m_ReceiveSipKey); i2p::crypto::Siphash<8> (m_ReceiveIV, m_ReceiveIV, 8, m_ReceiveSipKey);
uint16_t oldLen = m_NextReceivedLen;
m_NextReceivedLen = be16toh (m_NextReceivedLen ^ bufbe16toh(m_ReceiveIV)); m_NextReceivedLen = be16toh (m_NextReceivedLen ^ bufbe16toh(m_ReceiveIV));
LogPrint (eLogDebug, "NTCP2: received length ", m_NextReceivedLen); LogPrint (eLogDebug, "NTCP2: received length ", m_NextReceivedLen);
if (m_NextReceivedLen > oldLen)
{
delete[] m_NextReceivedBuffer; delete[] m_NextReceivedBuffer;
m_NextReceivedBuffer = new uint8_t[m_NextReceivedLen]; m_NextReceivedBuffer = new uint8_t[m_NextReceivedLen];
}
Receive (); Receive ();
} }
} }
@ -784,7 +788,8 @@ namespace transport
{ {
if (!m_SendQueue.empty ()) if (!m_SendQueue.empty ())
{ {
uint8_t * payload = new uint8_t[NTCP2_UNENCRYPTED_FRAME_MAX_SIZE]; auto buf = m_Server.NewNTCP2FrameBuffer ();
uint8_t * payload = buf->data ();
size_t s = 0; size_t s = 0;
// add I2NP blocks // add I2NP blocks
while (!m_SendQueue.empty ()) while (!m_SendQueue.empty ())
@ -815,7 +820,7 @@ namespace transport
s += paddingSize; s += paddingSize;
// send // send
SendNextFrame (payload, s); SendNextFrame (payload, s);
delete[] payload; m_Server.DeleteNTCP2FrameBuffer (buf);
} }
} }

8
libi2pd/NTCP2.h

@ -6,8 +6,10 @@
#include <thread> #include <thread>
#include <list> #include <list>
#include <map> #include <map>
#include <array>
#include <openssl/bn.h> #include <openssl/bn.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "util.h"
#include "RouterInfo.h" #include "RouterInfo.h"
#include "TransportSession.h" #include "TransportSession.h"
@ -57,6 +59,7 @@ namespace transport
}; };
typedef std::array<uint8_t, NTCP2_UNENCRYPTED_FRAME_MAX_SIZE> NTCP2FrameBuffer;
struct NTCP2Establisher struct NTCP2Establisher
{ {
NTCP2Establisher (); NTCP2Establisher ();
@ -187,6 +190,9 @@ namespace transport
void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn); void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn);
NTCP2FrameBuffer * NewNTCP2FrameBuffer () { return m_NTCP2FrameBuffersPool.Acquire(); }
void DeleteNTCP2FrameBuffer (NTCP2FrameBuffer * buf) { return m_NTCP2FrameBuffersPool.Release(buf); }
private: private:
void Run (); void Run ();
@ -210,6 +216,8 @@ namespace transport
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions; std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions; std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions;
i2p::util::MemoryPool<NTCP2FrameBuffer> m_NTCP2FrameBuffersPool;
public: public:
// for HTTP/I2PControl // for HTTP/I2PControl

Loading…
Cancel
Save