Skip to content

Commit 53aafc0

Browse files
authored
fix: mtt-1088 review. Safer handling of out-of-order or old messages (Unity-Technologies#1091)
* fix: Addressing latest review comments, making it safer when receiving OOO messages or old messages * fix: Addressing latest review comments, whitespace * fix: removing extraneous cast to ushort
1 parent 084dd8f commit 53aafc0

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

Runtime/Core/SnapshotSystem.cs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -959,26 +959,45 @@ internal void ReadSnapshot(ulong clientId, Stream snapshotStream)
959959
snapshotTick = reader.ReadInt32Packed();
960960
var sequence = reader.ReadUInt16();
961961

962-
// todo: check we didn't miss any and deal with gaps
963-
964-
if (m_ClientData[clientId].ReceivedSequenceMask != 0)
962+
if (sequence >= m_ClientData[clientId].LastReceivedSequence)
965963
{
966-
// since each bit in ReceivedSequenceMask is relative to the last received sequence
967-
// we need to shift all the bits by the difference in sequence
968-
m_ClientData[clientId].ReceivedSequenceMask <<=
969-
(sequence - m_ClientData[clientId].LastReceivedSequence);
970-
}
964+
if (m_ClientData[clientId].ReceivedSequenceMask != 0)
965+
{
966+
// since each bit in ReceivedSequenceMask is relative to the last received sequence
967+
// we need to shift all the bits by the difference in sequence
968+
var shift = sequence - m_ClientData[clientId].LastReceivedSequence;
969+
if (shift < sizeof(ushort) * 8)
970+
{
971+
m_ClientData[clientId].ReceivedSequenceMask <<= shift;
972+
}
973+
else
974+
{
975+
m_ClientData[clientId].ReceivedSequenceMask = 0;
976+
}
977+
}
971978

972-
if (m_ClientData[clientId].LastReceivedSequence != 0)
979+
if (m_ClientData[clientId].LastReceivedSequence != 0)
980+
{
981+
// because the bit we're adding for the previous ReceivedSequenceMask
982+
// was implicit, it needs to be shift by one less
983+
var shift = sequence - 1 - m_ClientData[clientId].LastReceivedSequence;
984+
if (shift < sizeof(ushort) * 8)
985+
{
986+
m_ClientData[clientId].ReceivedSequenceMask |= (ushort)(1 << shift);
987+
}
988+
}
989+
990+
m_ClientData[clientId].LastReceivedSequence = sequence;
991+
}
992+
else
973993
{
974-
// because the bit we're adding for the previous ReceivedSequenceMask
975-
// was implicit, it needs to be shift by one less
976-
m_ClientData[clientId].ReceivedSequenceMask +=
977-
(ushort)(1 << (ushort)((sequence - 1) - m_ClientData[clientId].LastReceivedSequence));
994+
// todo: Missing: dealing with out-of-order message acknowledgments
995+
// we should set m_ClientData[clientId].ReceivedSequenceMask accordingly
996+
// testing this will require a way to reorder SnapshotMessages, which we lack at the moment
997+
//
998+
// without this, we incur extra retransmit, not a catastrophic failure
978999
}
9791000

980-
m_ClientData[clientId].LastReceivedSequence = sequence;
981-
9821001
var sentinel = reader.ReadUInt16();
9831002
if (sentinel != SentinelBefore)
9841003
{

0 commit comments

Comments
 (0)