-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.cs
More file actions
179 lines (160 loc) · 6.04 KB
/
Copy pathPacket.cs
File metadata and controls
179 lines (160 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.IO;
namespace RakNet;
/// <summary>
/// Protocol-level constants for RakNet frames and MTU limits.
/// </summary>
public static class RakNetConstants
{
public const byte BitFlagDatagram = 0x80;
public const byte BitFlagAck = 0x40;
public const byte BitFlagNack = 0x20;
public const byte BitFlagNeedsBAndAS = 0x04;
public const byte SplitFlag = 0x10;
public const byte ProtocolVersion = 11;
public const ushort MinMTUSize = 400;
public const ushort MaxMTUSize = 1492;
public const int MaxWindowSize = 2048;
public const int PacketAdditionalSize = 1 + 3 + 1 + 2 + 3 + 3 + 1; // 14
public const int SplitAdditionalSize = 4 + 2 + 4; // 10
}
/// <summary>
/// Reliability levels for RakNet packets. Higher reliability means more
/// guarantees about delivery and ordering, at the cost of more overhead.
/// </summary>
public enum Reliability : byte
{
Unreliable = 0,
UnreliableSequenced = 1,
Reliable = 2,
ReliableOrdered = 3,
ReliableSequenced = 4,
}
public static class ReliabilityExtensions
{
public static bool Reliable(this Reliability r) =>
r == Reliability.Reliable ||
r == Reliability.ReliableOrdered ||
r == Reliability.ReliableSequenced;
public static bool Sequenced(this Reliability r) =>
r == Reliability.UnreliableSequenced ||
r == Reliability.ReliableSequenced;
public static bool SequencedOrOrdered(this Reliability r) =>
r.Sequenced() || r == Reliability.ReliableOrdered;
}
/// <summary>
/// An encapsulated packet sent within a datagram after the connection is
/// established. Each packet carries reliability metadata, optional message/
/// sequence/order indices, and optional split fragmentation info.
/// </summary>
public class Packet
{
public Reliability Reliability;
public uint MessageIndex; // uint24
public uint SequenceIndex; // uint24
public uint OrderIndex; // uint24
public byte[] Content = Array.Empty<byte>();
public bool Split;
public uint SplitCount;
public uint SplitIndex;
public ushort SplitId;
/// <summary>
/// Writes the packet encapsulation (header + indices + content) to the
/// stream. Does not write the datagram frame.
/// </summary>
public void Write(MemoryStream buf)
{
byte header = (byte)((int)Reliability << 5);
if (Split) header |= RakNetConstants.SplitFlag;
buf.WriteByte(header);
Binary.WriteUint16(buf, (ushort)(Content.Length << 3));
if (Reliability.Reliable())
Binary.WriteUint24(buf, MessageIndex);
if (Reliability.Sequenced())
Binary.WriteUint24(buf, SequenceIndex);
if (Reliability.SequencedOrOrdered())
{
Binary.WriteUint24(buf, OrderIndex);
buf.WriteByte(0); // Order channel, unused.
}
if (Split)
{
Binary.WriteUint32(buf, SplitCount);
Binary.WriteUint16(buf, SplitId);
Binary.WriteUint32(buf, SplitIndex);
}
buf.Write(Content, 0, Content.Length);
}
/// <summary>
/// Reads a single packet encapsulation from the buffer. Returns the number
/// of bytes consumed.
/// </summary>
public int Read(ReadOnlySpan<byte> b)
{
if (b.Length < 3) throw new EndOfStreamException("packet: too short for header");
byte header = b[0];
Split = (header & RakNetConstants.SplitFlag) != 0;
Reliability = (Reliability)((header & 0xE0) >> 5);
int n = Binary.LoadUint16(b.Slice(1)) >> 3;
if (n == 0) throw new Exception("invalid packet length: cannot be 0");
int offset = 3;
if (Reliability.Reliable())
{
if (b.Length - offset < 3) throw new EndOfStreamException("packet: messageIndex truncated");
MessageIndex = Binary.LoadUint24(b.Slice(offset));
offset += 3;
}
if (Reliability.Sequenced())
{
if (b.Length - offset < 3) throw new EndOfStreamException("packet: sequenceIndex truncated");
SequenceIndex = Binary.LoadUint24(b.Slice(offset));
offset += 3;
}
if (Reliability.SequencedOrOrdered())
{
if (b.Length - offset < 4) throw new EndOfStreamException("packet: orderIndex truncated");
OrderIndex = Binary.LoadUint24(b.Slice(offset));
offset += 4; // orderIndex (3) + order channel (1)
}
if (Split)
{
if (b.Length - offset < 10) throw new EndOfStreamException("packet: split fields truncated");
SplitCount = Binary.LoadUint32(b.Slice(offset));
SplitId = Binary.LoadUint16(b.Slice(offset + 4));
SplitIndex = Binary.LoadUint32(b.Slice(offset + 6));
offset += 10;
}
Content = new byte[n];
if (b.Slice(offset).Length < n) throw new EndOfStreamException("packet: content truncated");
b.Slice(offset, n).CopyTo(Content);
return offset + n;
}
}
public static class PacketSplitter
{
/// <summary>
/// Splits a content buffer into fragments that each fit within the MTU.
/// If splitting occurs, each fragment is further reduced by the split
/// overhead (4+2+4 bytes for splitCount, splitID, splitIndex).
/// </summary>
public static byte[][] Split(byte[] b, ushort mtu)
{
int n = b.Length;
int maxSize = mtu - RakNetConstants.PacketAdditionalSize;
if (n > maxSize)
maxSize -= RakNetConstants.SplitAdditionalSize;
int fragmentCount = n / maxSize + Math.Min(n % maxSize, 1);
byte[][] fragments = new byte[fragmentCount][];
int srcOffset = 0;
for (int i = 0; i < fragmentCount - 1; i++)
{
fragments[i] = new byte[maxSize];
Array.Copy(b, srcOffset, fragments[i], 0, maxSize);
srcOffset += maxSize;
}
int lastLen = n - srcOffset;
fragments[fragmentCount - 1] = new byte[lastLen];
Array.Copy(b, srcOffset, fragments[fragmentCount - 1], 0, lastLen);
return fragments;
}
}