mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-18 12:40:00 +00:00
eliminate send buffer mutex completely
This commit is contained in:
parent
8d6eb5b6b2
commit
21259204b1
@ -19,11 +19,6 @@ namespace i2p
|
|||||||
{
|
{
|
||||||
namespace stream
|
namespace stream
|
||||||
{
|
{
|
||||||
void SendBufferQueue::Add (const uint8_t * buf, size_t len, SendHandler handler)
|
|
||||||
{
|
|
||||||
Add (std::make_shared<SendBuffer>(buf, len, handler));
|
|
||||||
}
|
|
||||||
|
|
||||||
void SendBufferQueue::Add (std::shared_ptr<SendBuffer> buf)
|
void SendBufferQueue::Add (std::shared_ptr<SendBuffer> buf)
|
||||||
{
|
{
|
||||||
if (buf)
|
if (buf)
|
||||||
@ -115,10 +110,7 @@ namespace stream
|
|||||||
|
|
||||||
void Stream::CleanUp ()
|
void Stream::CleanUp ()
|
||||||
{
|
{
|
||||||
{
|
m_SendBuffer.CleanUp ();
|
||||||
std::unique_lock<std::mutex> l(m_SendBufferMutex);
|
|
||||||
m_SendBuffer.CleanUp ();
|
|
||||||
}
|
|
||||||
while (!m_ReceiveQueue.empty ())
|
while (!m_ReceiveQueue.empty ())
|
||||||
{
|
{
|
||||||
auto packet = m_ReceiveQueue.front ();
|
auto packet = m_ReceiveQueue.front ();
|
||||||
@ -553,91 +545,88 @@ namespace stream
|
|||||||
|
|
||||||
bool isNoAck = m_LastReceivedSequenceNumber < 0; // first packet
|
bool isNoAck = m_LastReceivedSequenceNumber < 0; // first packet
|
||||||
std::vector<Packet *> packets;
|
std::vector<Packet *> packets;
|
||||||
|
while ((m_Status == eStreamStatusNew) || (IsEstablished () && !m_SendBuffer.IsEmpty () && numMsgs > 0))
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(m_SendBufferMutex);
|
Packet * p = m_LocalDestination.NewPacket ();
|
||||||
while ((m_Status == eStreamStatusNew) || (IsEstablished () && !m_SendBuffer.IsEmpty () && numMsgs > 0))
|
uint8_t * packet = p->GetBuffer ();
|
||||||
|
// TODO: implement setters
|
||||||
|
size_t size = 0;
|
||||||
|
htobe32buf (packet + size, m_SendStreamID);
|
||||||
|
size += 4; // sendStreamID
|
||||||
|
htobe32buf (packet + size, m_RecvStreamID);
|
||||||
|
size += 4; // receiveStreamID
|
||||||
|
htobe32buf (packet + size, m_SequenceNumber++);
|
||||||
|
size += 4; // sequenceNum
|
||||||
|
if (isNoAck)
|
||||||
|
htobuf32 (packet + size, 0);
|
||||||
|
else
|
||||||
|
htobe32buf (packet + size, m_LastReceivedSequenceNumber);
|
||||||
|
size += 4; // ack Through
|
||||||
|
if (m_Status == eStreamStatusNew && !m_SendStreamID && m_RemoteIdentity)
|
||||||
{
|
{
|
||||||
Packet * p = m_LocalDestination.NewPacket ();
|
// first SYN packet
|
||||||
uint8_t * packet = p->GetBuffer ();
|
packet[size] = 8;
|
||||||
// TODO: implement setters
|
size++; // NACK count
|
||||||
size_t size = 0;
|
memcpy (packet + size, m_RemoteIdentity->GetIdentHash (), 32);
|
||||||
htobe32buf (packet + size, m_SendStreamID);
|
size += 32;
|
||||||
size += 4; // sendStreamID
|
|
||||||
htobe32buf (packet + size, m_RecvStreamID);
|
|
||||||
size += 4; // receiveStreamID
|
|
||||||
htobe32buf (packet + size, m_SequenceNumber++);
|
|
||||||
size += 4; // sequenceNum
|
|
||||||
if (isNoAck)
|
|
||||||
htobuf32 (packet + size, 0);
|
|
||||||
else
|
|
||||||
htobe32buf (packet + size, m_LastReceivedSequenceNumber);
|
|
||||||
size += 4; // ack Through
|
|
||||||
if (m_Status == eStreamStatusNew && !m_SendStreamID && m_RemoteIdentity)
|
|
||||||
{
|
|
||||||
// first SYN packet
|
|
||||||
packet[size] = 8;
|
|
||||||
size++; // NACK count
|
|
||||||
memcpy (packet + size, m_RemoteIdentity->GetIdentHash (), 32);
|
|
||||||
size += 32;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
packet[size] = 0;
|
|
||||||
size++; // NACK count
|
|
||||||
}
|
|
||||||
packet[size] = m_RTO/1000;
|
|
||||||
size++; // resend delay
|
|
||||||
if (m_Status == eStreamStatusNew)
|
|
||||||
{
|
|
||||||
// initial packet
|
|
||||||
m_Status = eStreamStatusOpen;
|
|
||||||
if (!m_RemoteLeaseSet) m_RemoteLeaseSet = m_LocalDestination.GetOwner ()->FindLeaseSet (m_RemoteIdentity->GetIdentHash ());;
|
|
||||||
if (m_RemoteLeaseSet)
|
|
||||||
{
|
|
||||||
m_RoutingSession = m_LocalDestination.GetOwner ()->GetRoutingSession (m_RemoteLeaseSet, true);
|
|
||||||
m_MTU = m_RoutingSession->IsRatchets () ? STREAMING_MTU_RATCHETS : STREAMING_MTU;
|
|
||||||
}
|
|
||||||
uint16_t flags = PACKET_FLAG_SYNCHRONIZE | PACKET_FLAG_FROM_INCLUDED |
|
|
||||||
PACKET_FLAG_SIGNATURE_INCLUDED | PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED;
|
|
||||||
if (isNoAck) flags |= PACKET_FLAG_NO_ACK;
|
|
||||||
bool isOfflineSignature = m_LocalDestination.GetOwner ()->GetPrivateKeys ().IsOfflineSignature ();
|
|
||||||
if (isOfflineSignature) flags |= PACKET_FLAG_OFFLINE_SIGNATURE;
|
|
||||||
htobe16buf (packet + size, flags);
|
|
||||||
size += 2; // flags
|
|
||||||
size_t identityLen = m_LocalDestination.GetOwner ()->GetIdentity ()->GetFullLen ();
|
|
||||||
size_t signatureLen = m_LocalDestination.GetOwner ()->GetPrivateKeys ().GetSignatureLen ();
|
|
||||||
uint8_t * optionsSize = packet + size; // set options size later
|
|
||||||
size += 2; // options size
|
|
||||||
m_LocalDestination.GetOwner ()->GetIdentity ()->ToBuffer (packet + size, identityLen);
|
|
||||||
size += identityLen; // from
|
|
||||||
htobe16buf (packet + size, m_MTU);
|
|
||||||
size += 2; // max packet size
|
|
||||||
if (isOfflineSignature)
|
|
||||||
{
|
|
||||||
const auto& offlineSignature = m_LocalDestination.GetOwner ()->GetPrivateKeys ().GetOfflineSignature ();
|
|
||||||
memcpy (packet + size, offlineSignature.data (), offlineSignature.size ());
|
|
||||||
size += offlineSignature.size (); // offline signature
|
|
||||||
}
|
|
||||||
uint8_t * signature = packet + size; // set it later
|
|
||||||
memset (signature, 0, signatureLen); // zeroes for now
|
|
||||||
size += signatureLen; // signature
|
|
||||||
htobe16buf (optionsSize, packet + size - 2 - optionsSize); // actual options size
|
|
||||||
size += m_SendBuffer.Get (packet + size, m_MTU); // payload
|
|
||||||
m_LocalDestination.GetOwner ()->Sign (packet, size, signature);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// follow on packet
|
|
||||||
htobuf16 (packet + size, 0);
|
|
||||||
size += 2; // flags
|
|
||||||
htobuf16 (packet + size, 0); // no options
|
|
||||||
size += 2; // options size
|
|
||||||
size += m_SendBuffer.Get(packet + size, m_MTU); // payload
|
|
||||||
}
|
|
||||||
p->len = size;
|
|
||||||
packets.push_back (p);
|
|
||||||
numMsgs--;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
packet[size] = 0;
|
||||||
|
size++; // NACK count
|
||||||
|
}
|
||||||
|
packet[size] = m_RTO/1000;
|
||||||
|
size++; // resend delay
|
||||||
|
if (m_Status == eStreamStatusNew)
|
||||||
|
{
|
||||||
|
// initial packet
|
||||||
|
m_Status = eStreamStatusOpen;
|
||||||
|
if (!m_RemoteLeaseSet) m_RemoteLeaseSet = m_LocalDestination.GetOwner ()->FindLeaseSet (m_RemoteIdentity->GetIdentHash ());;
|
||||||
|
if (m_RemoteLeaseSet)
|
||||||
|
{
|
||||||
|
m_RoutingSession = m_LocalDestination.GetOwner ()->GetRoutingSession (m_RemoteLeaseSet, true);
|
||||||
|
m_MTU = m_RoutingSession->IsRatchets () ? STREAMING_MTU_RATCHETS : STREAMING_MTU;
|
||||||
|
}
|
||||||
|
uint16_t flags = PACKET_FLAG_SYNCHRONIZE | PACKET_FLAG_FROM_INCLUDED |
|
||||||
|
PACKET_FLAG_SIGNATURE_INCLUDED | PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED;
|
||||||
|
if (isNoAck) flags |= PACKET_FLAG_NO_ACK;
|
||||||
|
bool isOfflineSignature = m_LocalDestination.GetOwner ()->GetPrivateKeys ().IsOfflineSignature ();
|
||||||
|
if (isOfflineSignature) flags |= PACKET_FLAG_OFFLINE_SIGNATURE;
|
||||||
|
htobe16buf (packet + size, flags);
|
||||||
|
size += 2; // flags
|
||||||
|
size_t identityLen = m_LocalDestination.GetOwner ()->GetIdentity ()->GetFullLen ();
|
||||||
|
size_t signatureLen = m_LocalDestination.GetOwner ()->GetPrivateKeys ().GetSignatureLen ();
|
||||||
|
uint8_t * optionsSize = packet + size; // set options size later
|
||||||
|
size += 2; // options size
|
||||||
|
m_LocalDestination.GetOwner ()->GetIdentity ()->ToBuffer (packet + size, identityLen);
|
||||||
|
size += identityLen; // from
|
||||||
|
htobe16buf (packet + size, m_MTU);
|
||||||
|
size += 2; // max packet size
|
||||||
|
if (isOfflineSignature)
|
||||||
|
{
|
||||||
|
const auto& offlineSignature = m_LocalDestination.GetOwner ()->GetPrivateKeys ().GetOfflineSignature ();
|
||||||
|
memcpy (packet + size, offlineSignature.data (), offlineSignature.size ());
|
||||||
|
size += offlineSignature.size (); // offline signature
|
||||||
|
}
|
||||||
|
uint8_t * signature = packet + size; // set it later
|
||||||
|
memset (signature, 0, signatureLen); // zeroes for now
|
||||||
|
size += signatureLen; // signature
|
||||||
|
htobe16buf (optionsSize, packet + size - 2 - optionsSize); // actual options size
|
||||||
|
size += m_SendBuffer.Get (packet + size, m_MTU); // payload
|
||||||
|
m_LocalDestination.GetOwner ()->Sign (packet, size, signature);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// follow on packet
|
||||||
|
htobuf16 (packet + size, 0);
|
||||||
|
size += 2; // flags
|
||||||
|
htobuf16 (packet + size, 0); // no options
|
||||||
|
size += 2; // options size
|
||||||
|
size += m_SendBuffer.Get(packet + size, m_MTU); // payload
|
||||||
|
}
|
||||||
|
p->len = size;
|
||||||
|
packets.push_back (p);
|
||||||
|
numMsgs--;
|
||||||
}
|
}
|
||||||
if (packets.size () > 0)
|
if (packets.size () > 0)
|
||||||
{
|
{
|
||||||
|
@ -135,7 +135,6 @@ namespace stream
|
|||||||
SendBufferQueue (): m_Size (0) {};
|
SendBufferQueue (): m_Size (0) {};
|
||||||
~SendBufferQueue () { CleanUp (); };
|
~SendBufferQueue () { CleanUp (); };
|
||||||
|
|
||||||
void Add (const uint8_t * buf, size_t len, SendHandler handler);
|
|
||||||
void Add (std::shared_ptr<SendBuffer> buf);
|
void Add (std::shared_ptr<SendBuffer> buf);
|
||||||
size_t Get (uint8_t * buf, size_t len);
|
size_t Get (uint8_t * buf, size_t len);
|
||||||
size_t GetSize () const { return m_Size; };
|
size_t GetSize () const { return m_Size; };
|
||||||
@ -251,7 +250,6 @@ namespace stream
|
|||||||
size_t m_NumSentBytes, m_NumReceivedBytes;
|
size_t m_NumSentBytes, m_NumReceivedBytes;
|
||||||
uint16_t m_Port;
|
uint16_t m_Port;
|
||||||
|
|
||||||
std::mutex m_SendBufferMutex;
|
|
||||||
SendBufferQueue m_SendBuffer;
|
SendBufferQueue m_SendBuffer;
|
||||||
int m_WindowSize, m_RTT, m_RTO, m_AckDelay;
|
int m_WindowSize, m_RTT, m_RTO, m_AckDelay;
|
||||||
uint64_t m_LastWindowSizeIncreaseTime;
|
uint64_t m_LastWindowSizeIncreaseTime;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user