forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetPacket.cs
More file actions
267 lines (229 loc) · 8.3 KB
/
NetPacket.cs
File metadata and controls
267 lines (229 loc) · 8.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using LiteNetLib.Utils;
namespace LiteNetLib
{
// 0 1 2 3 4 5 6 7
// f ccc ppppppppp
//
internal enum PacketProperty : byte
{
Unreliable, //0
ReliableUnordered, //1
Sequenced, //2
ReliableOrdered, //3
AckReliable, //4
AckReliableOrdered, //5
Ping, //6 *
Pong, //7 *
ConnectRequest, //8 *
ConnectAccept, //9 *
Disconnect, //10 *
UnconnectedMessage, //11
NatIntroductionRequest, //12 *
NatIntroduction, //13 *
NatPunchMessage, //14 *
MtuCheck, //15 *
MtuOk, //16 *
DiscoveryRequest, //17 *
DiscoveryResponse, //18 *
Merged, //19
ShutdownOk, //20 *
ReliableSequenced //21
}
internal sealed class NetPacket
{
private const int LastProperty = 21;
//Header
public PacketProperty Property
{
get { return (PacketProperty)(RawData[0] & 0x1F); }
set { RawData[0] = (byte)((RawData[0] & 0xE0) | (byte)value); }
}
// Fragmented
// 0x80 - 1000 0000
// Property
// 0x1F - 0001 1111
// 0xE0 - 1110 0000
// Connection number
// 0x60 - 0110 0000
// 0x9F - 1001 1111
public byte ConnectionNumber
{
get { return (byte)((RawData[0] & 0x60) >> 5); }
set { RawData[0] = (byte) ((RawData[0] & 0x9F) | (value << 5)); }
}
public ushort Sequence
{
get { return BitConverter.ToUInt16(RawData, 1); }
set { FastBitConverter.GetBytes(RawData, 1, value); }
}
public bool IsFragmented
{
get { return (RawData[0] & 0x80) != 0; }
}
public void MarkFragmented()
{
RawData[0] |= 0x80; //set first bit
}
public ushort FragmentId
{
get { return BitConverter.ToUInt16(RawData, 3); }
set { FastBitConverter.GetBytes(RawData, 3, value); }
}
public ushort FragmentPart
{
get { return BitConverter.ToUInt16(RawData, 5); }
set { FastBitConverter.GetBytes(RawData, 5, value); }
}
public ushort FragmentsTotal
{
get { return BitConverter.ToUInt16(RawData, 7); }
set { FastBitConverter.GetBytes(RawData, 7, value); }
}
//Data
public byte[] RawData;
public int Size;
public NetPacket(int size)
{
RawData = new byte[size];
Size = size;
}
public NetPacket(PacketProperty property, int size)
{
size += GetHeaderSize(property);
RawData = new byte[size];
Property = property;
Size = size;
}
public bool Realloc(int toSize)
{
if (RawData.Length < toSize)
{
RawData = new byte[toSize];
return true;
}
return false;
}
public static int GetHeaderSize(PacketProperty property)
{
switch (property)
{
case PacketProperty.ReliableOrdered:
case PacketProperty.ReliableUnordered:
case PacketProperty.Sequenced:
case PacketProperty.Ping:
case PacketProperty.Pong:
case PacketProperty.AckReliable:
case PacketProperty.AckReliableOrdered:
case PacketProperty.ReliableSequenced:
return NetConstants.SequencedHeaderSize;
default:
return NetConstants.HeaderSize;
}
}
public int GetHeaderSize()
{
return GetHeaderSize(Property);
}
public byte[] CopyPacketData()
{
int headerSize = GetHeaderSize(Property);
int dataSize = Size - headerSize;
byte[] data = new byte[dataSize];
Buffer.BlockCopy(RawData, headerSize, data, 0, dataSize);
return data;
}
//Packet contstructor from byte array
public bool FromBytes(byte[] data, int start, int packetSize)
{
//Reading property
byte property = (byte)(data[start] & 0x7F);
bool fragmented = (data[start] & 0x80) != 0;
int headerSize = GetHeaderSize((PacketProperty) property);
if (property > LastProperty || packetSize < headerSize ||
(fragmented && packetSize < headerSize + NetConstants.FragmentHeaderSize))
{
return false;
}
Buffer.BlockCopy(data, start, RawData, 0, packetSize);
Size = packetSize;
return true;
}
}
internal sealed class NetConnectRequestPacket
{
public readonly long ConnectionId;
public readonly byte ConnectionNumber;
public readonly NetDataReader Data;
private NetConnectRequestPacket(long connectionId, byte connectionNumber, NetDataReader data)
{
ConnectionId = connectionId;
ConnectionNumber = connectionNumber;
Data = data;
}
public static NetConnectRequestPacket FromData(NetPacket packet)
{
if (packet.Size < 12 || packet.ConnectionNumber >= NetConstants.MaxConnectionNumber)
return null;
int protoId = BitConverter.ToInt32(packet.RawData, 1);
if (protoId != NetConstants.ProtocolId)
{
NetUtils.DebugWrite(ConsoleColor.Cyan,
"[NM] Peer connect reject. Invalid protocol ID: " + protoId);
return null;
}
//Getting new id for peer
long connectionId = BitConverter.ToInt64(packet.RawData, 5);
// Read data and create request
var reader = new NetDataReader(null, 0, 0);
if (packet.Size > 12)
reader.SetSource(packet.RawData, 13, packet.Size);
return new NetConnectRequestPacket(connectionId, packet.ConnectionNumber, reader);
}
public static NetPacket Make(NetDataWriter connectData, long connectId)
{
//Make initial packet
var packet = new NetPacket(PacketProperty.ConnectRequest, 12 + connectData.Length);
//Add data
FastBitConverter.GetBytes(packet.RawData, 1, NetConstants.ProtocolId);
FastBitConverter.GetBytes(packet.RawData, 5, connectId);
Buffer.BlockCopy(connectData.Data, 0, packet.RawData, 13, connectData.Length);
return packet;
}
}
internal sealed class NetConnectAcceptPacket
{
public readonly long ConnectionId;
public readonly byte ConnectionNumber;
public readonly bool IsReusedPeer;
private NetConnectAcceptPacket(long connectionId, byte connectionNumber, bool isReusedPeer)
{
ConnectionId = connectionId;
ConnectionNumber = connectionNumber;
IsReusedPeer = isReusedPeer;
}
public static NetConnectAcceptPacket FromData(NetPacket packet)
{
if (packet.Size != 11)
return null;
long connectionId = BitConverter.ToInt64(packet.RawData, 1);
//check connect num
byte connectionNumber = packet.RawData[9];
if (connectionNumber >= NetConstants.MaxConnectionNumber)
return null;
//check reused flag
byte isReused = packet.RawData[10];
if (isReused > 1)
return null;
return new NetConnectAcceptPacket(connectionId, connectionNumber, isReused == 1);
}
public static NetPacket Make(long connectId, byte connectNum, bool reusedPeer)
{
var packet = new NetPacket(PacketProperty.ConnectAccept, 10);
FastBitConverter.GetBytes(packet.RawData, 1, connectId);
packet.RawData[9] = connectNum;
packet.RawData[10] = (byte)(reusedPeer ? 1 : 0);
return packet;
}
}
}