Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/TwoTenPvP/Ruffles
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Dec 18, 2019
2 parents 70be6ef + ffefe21 commit af9692b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 104 deletions.
48 changes: 28 additions & 20 deletions Ruffles/Channeling/Channels/ReliableChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void DeAlloc(MemoryManager memoryManager)


// Outgoing sequencing
private ushort _lastOutboundSequenceNumber;
private ushort _lastOutgoingSequence;
private ushort _outgoingLowestAckedSequence;
private readonly HeapableSlidingWindow<PendingOutgoingPacket> _sendSequencer;

// Channel info
Expand Down Expand Up @@ -145,7 +146,7 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h

lock (_lock)
{
PendingOutgoingPacket unsafeOutgoing = _sendSequencer.GetUnsafe(_lastOutboundSequenceNumber + 1, out bool isSafe);
PendingOutgoingPacket unsafeOutgoing = _sendSequencer.GetUnsafe((ushort)(_lastOutgoingSequence + 1), out bool isSafe);

if (unsafeOutgoing.Alive && !isSafe)
{
Expand All @@ -159,7 +160,7 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
}

// Increment the sequence number
_lastOutboundSequenceNumber++;
_lastOutgoingSequence++;

// Set header size
headerSize = 4;
Expand All @@ -172,17 +173,17 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
memory.Buffer[1] = channelId;

// Write the sequence
memory.Buffer[2] = (byte)_lastOutboundSequenceNumber;
memory.Buffer[3] = (byte)(_lastOutboundSequenceNumber >> 8);
memory.Buffer[2] = (byte)_lastOutgoingSequence;
memory.Buffer[3] = (byte)(_lastOutgoingSequence >> 8);

// Copy the payload
Buffer.BlockCopy(payload.Array, payload.Offset, memory.Buffer, 4, payload.Count);

// Add the memory to pending
_sendSequencer[_lastOutboundSequenceNumber] = new PendingOutgoingPacket()
_sendSequencer[_lastOutgoingSequence] = new PendingOutgoingPacket()
{
Alive = true,
Sequence = _lastOutboundSequenceNumber,
Sequence = _lastOutgoingSequence,
Attempts = 1,
LastSent = NetTime.Now,
FirstSent = NetTime.Now,
Expand Down Expand Up @@ -258,11 +259,18 @@ private void HandleAck(ushort sequence)
Alive = false,
Sequence = sequence
};

if (sequence == (ushort)(_outgoingLowestAckedSequence + 1))
{
// This was the next one.
_outgoingLowestAckedSequence++;
}
}

for (ushort i = sequence; _sendSequencer[i].Alive; i++)
// Loop from the lowest ack we got
for (ushort i = _outgoingLowestAckedSequence; !_sendSequencer[i].Alive && SequencingUtils.Distance(i, _lastOutgoingSequence, sizeof(ushort)) <= 0; i++)
{
_incomingLowestAckedSequence = i;
_outgoingLowestAckedSequence = i;
}
}
}
Expand All @@ -271,20 +279,19 @@ public void InternalUpdate()
{
lock (_lock)
{
long distance = SequencingUtils.Distance(_lastOutboundSequenceNumber, _incomingLowestAckedSequence, sizeof(ushort));

for (ushort i = _incomingLowestAckedSequence; i < _incomingLowestAckedSequence + distance; i++)
for (ushort i = (ushort)(_outgoingLowestAckedSequence + 1); SequencingUtils.Distance(i, _lastOutgoingSequence, sizeof(ushort)) < 0; i++)
{
if (_sendSequencer[i].Alive)
{
if (_sendSequencer[i].Attempts > config.ReliabilityMaxResendAttempts)
{
// If they don't ack the message, disconnect them
connection.Disconnect(false);
return;
}
else if ((NetTime.Now - _sendSequencer[i].LastSent).TotalMilliseconds > connection.SmoothRoundtrip * config.ReliabilityResendRoundtripMultiplier && (NetTime.Now - _sendSequencer[i].LastSent).TotalMilliseconds > config.ReliabilityMinPacketResendDelay)
if ((NetTime.Now - _sendSequencer[i].LastSent).TotalMilliseconds > connection.SmoothRoundtrip * config.ReliabilityResendRoundtripMultiplier && (NetTime.Now - _sendSequencer[i].LastSent).TotalMilliseconds > config.ReliabilityMinPacketResendDelay)
{
if (_sendSequencer[i].Attempts > config.ReliabilityMaxResendAttempts)
{
// If they don't ack the message, disconnect them
connection.Disconnect(false);
return;
}

_sendSequencer[i] = new PendingOutgoingPacket()
{
Alive = true,
Expand Down Expand Up @@ -366,7 +373,8 @@ public void Release()

// Clear all outgoing states
_sendSequencer.Release();
_lastOutboundSequenceNumber = 0;
_lastOutgoingSequence = 0;
_outgoingLowestAckedSequence = 0;
}
}

Expand Down
50 changes: 29 additions & 21 deletions Ruffles/Channeling/Channels/ReliableFragmentedChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public void DeAlloc(MemoryManager memoryManager)
private readonly HeapableSlidingWindow<PendingIncomingPacket> _receiveSequencer;

// Outgoing sequencing
private ushort _lastOutboundSequenceNumber;
private ushort _lastOutgoingSequence;
private ushort _outgoingLowestAckedSequence;
private readonly HeapableSlidingWindow<PendingOutgoingPacket> _sendSequencer;

// Channel info
Expand Down Expand Up @@ -318,7 +319,7 @@ public HeapPointers HandleIncomingMessagePoll(ArraySegment<byte> payload, out by
// If this sequence just completed. Return the memory
if (_receiveSequencer[sequence].Alive && _receiveSequencer[sequence].IsComplete)
{
if (sequence == _incomingLowestAckedSequence + 1)
if (sequence == (ushort)(_incomingLowestAckedSequence + 1))
{
// This is the next packet.

Expand Down Expand Up @@ -400,7 +401,7 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h

lock (_lock)
{
PendingOutgoingPacket unsafeOutgoing = _sendSequencer.GetUnsafe(_lastOutboundSequenceNumber + 1, out bool isSafe);
PendingOutgoingPacket unsafeOutgoing = _sendSequencer.GetUnsafe((ushort)(_lastOutgoingSequence + 1), out bool isSafe);

if (unsafeOutgoing.Alive && !isSafe)
{
Expand All @@ -414,7 +415,7 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
}

// Increment the sequence number
_lastOutboundSequenceNumber++;
_lastOutgoingSequence++;

// Set header size
headerSize = 6;
Expand All @@ -437,8 +438,8 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[1] = channelId;

// Write the sequence
((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[2] = (byte)_lastOutboundSequenceNumber;
((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[3] = (byte)(_lastOutboundSequenceNumber >> 8);
((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[2] = (byte)_lastOutgoingSequence;
((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[3] = (byte)(_lastOutgoingSequence >> 8);

// Write the fragment
((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[4] = (byte)(i & 32767);
Expand Down Expand Up @@ -466,13 +467,13 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
Attempts = 1,
LastSent = NetTime.Now,
FirstSent = NetTime.Now,
Sequence = _lastOutboundSequenceNumber,
Sequence = _lastOutgoingSequence,
Memory = ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i])
};
}

// Add the memory to the outgoing sequencer
_sendSequencer[_lastOutboundSequenceNumber] = new PendingOutgoingPacket()
_sendSequencer[_lastOutgoingSequence] = new PendingOutgoingPacket()
{
Alive = true,
Fragments = outgoingFragments
Expand Down Expand Up @@ -538,12 +539,19 @@ public void HandleAck(ArraySegment<byte> payload)
{
Alive = false
};

if (sequence == (ushort)(_outgoingLowestAckedSequence + 1))
{
// This was the next one.
_outgoingLowestAckedSequence++;
}
}
}

for (ushort i = sequence; _sendSequencer[i].Alive && _sendSequencer[i].AllFragmentsAlive; i++)
// Loop from the lowest ack we got
for (ushort i = _outgoingLowestAckedSequence; !_sendSequencer[i].Alive && !_sendSequencer[i].AllFragmentsAlive && SequencingUtils.Distance(i, _lastOutgoingSequence, sizeof(ushort)) <= 0; i++)
{
_incomingLowestAckedSequence = i;
_outgoingLowestAckedSequence = i;
}
}
}
Expand Down Expand Up @@ -600,24 +608,23 @@ public void InternalUpdate()
{
lock (_lock)
{
long distance = SequencingUtils.Distance(_lastOutboundSequenceNumber, _incomingLowestAckedSequence, sizeof(ushort));

for (ushort i = _incomingLowestAckedSequence; i < _incomingLowestAckedSequence + distance; i++)
for (ushort i = (ushort)(_outgoingLowestAckedSequence + 1); SequencingUtils.Distance(i, _lastOutgoingSequence, sizeof(ushort)) < 0; i++)
{
if (_sendSequencer[i].Alive)
{
for (int j = 0; j < _sendSequencer[i].Fragments.VirtualCount; j++)
{
if (_sendSequencer[i].Fragments.Pointers[j] != null && ((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).Alive)
{
if (((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).Attempts > config.ReliabilityMaxResendAttempts)
{
// If they don't ack the message, disconnect them
connection.Disconnect(false);
return;
}
else if ((NetTime.Now - ((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).LastSent).TotalMilliseconds > connection.SmoothRoundtrip * config.ReliabilityResendRoundtripMultiplier && (NetTime.Now - ((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).LastSent).TotalMilliseconds > config.ReliabilityMinPacketResendDelay)
if ((NetTime.Now - ((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).LastSent).TotalMilliseconds > connection.SmoothRoundtrip * config.ReliabilityResendRoundtripMultiplier && (NetTime.Now - ((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).LastSent).TotalMilliseconds > config.ReliabilityMinPacketResendDelay)
{
if (((PendingOutgoingFragment)_sendSequencer[i].Fragments.Pointers[j]).Attempts > config.ReliabilityMaxResendAttempts)
{
// If they don't ack the message, disconnect them
connection.Disconnect(false);
return;
}

_sendSequencer[i].Fragments.Pointers[j] = new PendingOutgoingFragment()
{
Alive = true,
Expand Down Expand Up @@ -650,7 +657,8 @@ public void Release()

// Clear all outgoing states
_sendSequencer.Release();
_lastOutboundSequenceNumber = 0;
_lastOutgoingSequence = 0;
_outgoingLowestAckedSequence = 0;
}
}

Expand Down
29 changes: 14 additions & 15 deletions Ruffles/Channeling/Channels/ReliableOrderedChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void DeAlloc(MemoryManager memoryManager)
private readonly SlidingWindow<NetTime> _lastAckTimes;

// Outgoing sequencing
private ushort _lastOutboundSequenceNumber;
private ushort _lastOutgoingSequence;
private PendingOutgoingPacket _lastOutgoingPacket;

// Channel info
Expand Down Expand Up @@ -81,7 +81,7 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
lock (_lock)
{
// Increment the sequence number
_lastOutboundSequenceNumber++;
_lastOutgoingSequence++;

// Set header size
headerSize = 4;
Expand All @@ -94,8 +94,8 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
memory.Buffer[1] = channelId;

// Write the sequence
memory.Buffer[2] = (byte)_lastOutboundSequenceNumber;
memory.Buffer[3] = (byte)(_lastOutboundSequenceNumber >> 8);
memory.Buffer[2] = (byte)_lastOutgoingSequence;
memory.Buffer[3] = (byte)(_lastOutgoingSequence >> 8);

// Copy the payload
Buffer.BlockCopy(payload.Array, payload.Offset, memory.Buffer, 4, payload.Count);
Expand All @@ -107,7 +107,7 @@ public HeapPointers CreateOutgoingMessage(ArraySegment<byte> payload, out byte h
_lastOutgoingPacket = new PendingOutgoingPacket()
{
Alive = true,
Sequence = _lastOutboundSequenceNumber,
Sequence = _lastOutgoingSequence,
Attempts = 1,
LastSent = NetTime.Now,
FirstSent = NetTime.Now,
Expand Down Expand Up @@ -156,8 +156,6 @@ public void HandleAck(ArraySegment<byte> payload)
Alive = false,
Sequence = sequence
};

_incomingLowestAckedSequence = sequence;
}
}
}
Expand Down Expand Up @@ -211,14 +209,15 @@ public void InternalUpdate()
{
if (_lastOutgoingPacket.Alive)
{
if (_lastOutgoingPacket.Attempts > config.ReliabilityMaxResendAttempts)
{
// If they don't ack the message, disconnect them
connection.Disconnect(false);
return;
}
else if ((NetTime.Now - _lastOutgoingPacket.LastSent).TotalMilliseconds > connection.SmoothRoundtrip * config.ReliabilityResendRoundtripMultiplier && (NetTime.Now - _lastOutgoingPacket.LastSent).TotalMilliseconds > config.ReliabilityMinPacketResendDelay)
if ((NetTime.Now - _lastOutgoingPacket.LastSent).TotalMilliseconds > connection.SmoothRoundtrip * config.ReliabilityResendRoundtripMultiplier && (NetTime.Now - _lastOutgoingPacket.LastSent).TotalMilliseconds > config.ReliabilityMinPacketResendDelay)
{
if (_lastOutgoingPacket.Attempts > config.ReliabilityMaxResendAttempts)
{
// If they don't ack the message, disconnect them
connection.Disconnect(false);
return;
}

_lastOutgoingPacket = new PendingOutgoingPacket()
{
Alive = true,
Expand Down Expand Up @@ -272,7 +271,7 @@ public void Release()
_incomingLowestAckedSequence = 0;

// Clear all outgoing states
_lastOutboundSequenceNumber = 0;
_lastOutgoingSequence = 0;

// Reset the outgoing packet
_lastOutgoingPacket = new PendingOutgoingPacket()
Expand Down
Loading

0 comments on commit af9692b

Please sign in to comment.