Skip to content

Commit f7ce5de

Browse files
author
KillaMaaki
committed
- Fixed several critical bugs with fragmented packets.
- Fixed issue where allocated buffer for packet header was sometimes too small for actual packet header bytes.
1 parent 6e3bdd8 commit f7ce5de

24 files changed

Lines changed: 1063 additions & 1097 deletions

.vs/ReliableNetcode.NET/v14/.suo

10 KB
Binary file not shown.

ReliableNetcode/MessageChannel.cs

Lines changed: 509 additions & 540 deletions
Large diffs are not rendered by default.

ReliableNetcode/PacketHeader.cs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,62 @@
77

88
namespace ReliableNetcode
99
{
10-
internal class SentPacketData
11-
{
12-
public double time;
13-
public bool acked;
14-
public uint packetBytes;
15-
}
10+
internal class SentPacketData
11+
{
12+
public double time;
13+
public bool acked;
14+
public uint packetBytes;
15+
}
1616

17-
internal class ReceivedPacketData
18-
{
19-
public double time;
20-
public uint packetBytes;
21-
}
17+
internal class ReceivedPacketData
18+
{
19+
public double time;
20+
public uint packetBytes;
21+
}
2222

23-
internal class FragmentReassemblyData
24-
{
25-
public ushort Sequence;
26-
public ushort Ack;
27-
public uint AckBits;
28-
public int NumFragmentsReceived;
29-
public int NumFragmentsTotal;
30-
public ByteBuffer PacketDataBuffer = new ByteBuffer();
31-
public int PacketBytes;
32-
public int PacketHeaderBytes;
33-
public bool[] FragmentReceived = new bool[256];
23+
internal class FragmentReassemblyData
24+
{
25+
public ushort Sequence;
26+
public ushort Ack;
27+
public uint AckBits;
28+
public int NumFragmentsReceived;
29+
public int NumFragmentsTotal;
30+
public ByteBuffer PacketDataBuffer = new ByteBuffer();
31+
public int PacketBytes;
32+
public int HeaderOffset;
33+
public bool[] FragmentReceived = new bool[256];
3434

35-
public void StoreFragmentData(byte channelID, ushort sequence, ushort ack, uint ackBits, int fragmentID, int fragmentSize, byte[] fragmentData, int fragmentBytes)
36-
{
37-
int copyOffset = 0;
35+
public void StoreFragmentData(byte channelID, ushort sequence, ushort ack, uint ackBits, int fragmentID, int fragmentSize, byte[] fragmentData, int fragmentBytes)
36+
{
37+
int copyOffset = 0;
3838

39-
if (fragmentID == 0)
40-
{
41-
byte[] packetHeader = BufferPool.GetBuffer(Defines.MAX_PACKET_HEADER_BYTES);
42-
this.PacketHeaderBytes = PacketIO.WritePacketHeader(packetHeader, channelID, sequence, ack, ackBits);
43-
44-
this.PacketDataBuffer.SetSize(this.PacketHeaderBytes + fragmentSize);
39+
if (fragmentID == 0) {
40+
byte[] packetHeader = BufferPool.GetBuffer(Defines.MAX_PACKET_HEADER_BYTES);
41+
int headerBytes = PacketIO.WritePacketHeader(packetHeader, channelID, sequence, ack, ackBits);
42+
this.HeaderOffset = Defines.MAX_PACKET_HEADER_BYTES - headerBytes;
4543

46-
this.PacketDataBuffer.BufferCopy(packetHeader, 0, 0, this.PacketHeaderBytes);
47-
copyOffset = this.PacketHeaderBytes;
48-
49-
fragmentBytes -= this.PacketHeaderBytes;
44+
if (this.PacketDataBuffer.Length < (Defines.MAX_PACKET_HEADER_BYTES + fragmentSize))
45+
this.PacketDataBuffer.SetSize(Defines.MAX_PACKET_HEADER_BYTES + fragmentSize);
5046

51-
BufferPool.ReturnBuffer(packetHeader);
52-
}
47+
this.PacketDataBuffer.BufferCopy(packetHeader, 0, this.HeaderOffset, headerBytes);
48+
copyOffset = headerBytes;
5349

54-
int writePos = this.PacketHeaderBytes + fragmentID * fragmentSize;
55-
int end = writePos + fragmentBytes;
56-
this.PacketDataBuffer.SetSize(end);
50+
fragmentBytes -= headerBytes;
5751

58-
if (fragmentID == NumFragmentsTotal - 1)
59-
{
60-
this.PacketBytes = (this.NumFragmentsTotal - 1) * fragmentSize + fragmentBytes;
61-
}
52+
BufferPool.ReturnBuffer(packetHeader);
53+
}
6254

63-
this.PacketDataBuffer.BufferCopy(fragmentData, copyOffset, this.PacketHeaderBytes + fragmentID * fragmentSize, fragmentBytes);
64-
}
65-
}
55+
int writePos = Defines.MAX_PACKET_HEADER_BYTES + fragmentID * fragmentSize;
56+
int end = writePos + fragmentBytes;
57+
58+
if (this.PacketDataBuffer.Length < end)
59+
this.PacketDataBuffer.SetSize(end);
60+
61+
if (fragmentID == NumFragmentsTotal - 1) {
62+
this.PacketBytes = (this.NumFragmentsTotal - 1) * fragmentSize + fragmentBytes;
63+
}
64+
65+
this.PacketDataBuffer.BufferCopy(fragmentData, copyOffset, Defines.MAX_PACKET_HEADER_BYTES + fragmentID * fragmentSize, fragmentBytes);
66+
}
67+
}
6668
}

0 commit comments

Comments
 (0)