Network Library Documentation
Loading...
Searching...
No Matches
Packet Class Reference

Packet class used for data representation - used for reading and writing data that will be send or were received, includes methods for de-/serialization of different data types. More...

Public Types

enum  ServerPackets {
  welcome = 1 , SpawnUser , DisconnectUser , Latency ,
  Message , HmacKey , AuthToken , MessageAll ,
  TransformUpdate , AnimationUpdate , BandwidthReply , ThroughputReply
}
 Defined packets sent to Clients by Server. More...
enum  ClientPackets {
  welcomeReceived = 1 , MeassureRequest , DisconnectRequest , MessageToUserRequest ,
  MessageToAllRequest , HmacKey , AutToken , TransformChange ,
  AnimationChange , BandwidthRequest , ThroughputRequest
}
 Defined packets sent to Server by Client. More...

Public Member Functions

 Packet ()
 Packet (int packetId)
 Packet (byte[] data)
void Write (string text)
 Write string data.
string ReadString (bool ChangeReadPosition=true)
 Read string data.
void Write (int data)
 Write int data.
int ReadInt (bool changeReadPosition=true)
 Read int data.
void Write (float data)
 Write float data.
float ReadFloat (bool changeReadPosition=true)
 Read float data.
void Write (bool data)
 Write bool data.
bool ReadBool (bool changeReadPosition=true)
 Read bool data.
void Write (byte data)
 Write byte data.
byte ReadByte (bool changeReadPosition=true)
 Read byte data.
void Write (byte[] data)
 Write byte array data.
byte[] ReadBytes (int arrayLength, bool changeReadPosition=true)
 read byte array data
void Write (short data)
 Write short data.
short ReadShort (bool changeReadPos=true)
 Read short data.
void Write (Vector3 vector)
 Write Vector3 data.
Vector3 ReadVector3 (bool changeReadPos=true)
 Read Vector3 data.
void Write (Quaternion data)
 Write Quaternion data.
Quaternion ReadQuaternion (bool changeReadPos=true)
 Read Quaternion data.
void ClearPacket (bool clear)
byte[] ConvertToArray ()
void AddIntAtStart (int number)
void SizeOfDataAtStart ()
int DataLength ()
int UnreadLength ()
void AddBytesToBuffers (byte[] Data)
byte[] GetDataWithoutHmac ()
 Return data without HMAC.
byte[] GetHmac ()
 return HMAC from data

Private Attributes

List< byte > packetDataBuffer
 Dynamic data list.
byte[] dataBuffer
 Byte array for reading data.
int readPosition

Detailed Description

Packet class used for data representation - used for reading and writing data that will be send or were received, includes methods for de-/serialization of different data types.

Definition at line 9 of file Packet.cs.

Member Enumeration Documentation

◆ ClientPackets

Defined packets sent to Server by Client.

Enumerator
welcomeReceived 
MeassureRequest 
DisconnectRequest 
MessageToUserRequest 
MessageToAllRequest 
HmacKey 
AutToken 
TransformChange 
AnimationChange 
BandwidthRequest 
ThroughputRequest 

Definition at line 351 of file Packet.cs.

352 {
353 welcomeReceived = 1,
354 MeassureRequest,
355 DisconnectRequest,
356 MessageToUserRequest,
357 MessageToAllRequest,
358 HmacKey,
359 AutToken,
360 TransformChange,
361 AnimationChange,
362 BandwidthRequest,
363 ThroughputRequest
364 }

◆ ServerPackets

Defined packets sent to Clients by Server.

Enumerator
welcome 
SpawnUser 
DisconnectUser 
Latency 
Message 
HmacKey 
AuthToken 
MessageAll 
TransformUpdate 
AnimationUpdate 
BandwidthReply 
ThroughputReply 

Definition at line 332 of file Packet.cs.

333 {
334 welcome = 1,
335 SpawnUser,
336 DisconnectUser,
337 Latency,
338 Message,
339 HmacKey,
340 AuthToken,
341 MessageAll,
342 TransformUpdate,
343 AnimationUpdate,
344 BandwidthReply,
345 ThroughputReply
346 }

Constructor & Destructor Documentation

◆ Packet() [1/3]

Packet.Packet ( )

Definition at line 23 of file Packet.cs.

24 {
25 readPosition = 0;
26 packetDataBuffer = new List<byte>();
27 }
int readPosition
Definition Packet.cs:20
List< byte > packetDataBuffer
Dynamic data list.
Definition Packet.cs:14

◆ Packet() [2/3]

Packet.Packet ( int packetId)

Definition at line 28 of file Packet.cs.

29 {
30 readPosition = 0;
31 packetDataBuffer = new List<byte>();
32 Write(packetId);
33 }
void Write(string text)
Write string data.
Definition Packet.cs:44

◆ Packet() [3/3]

Packet.Packet ( byte[] data)

Definition at line 34 of file Packet.cs.

35 {
36 readPosition = 0;
37 packetDataBuffer = new List<byte>();
38 AddBytesToBuffers(data);
39 }
void AddBytesToBuffers(byte[] Data)
Definition Packet.cs:309

Member Function Documentation

◆ AddBytesToBuffers()

void Packet.AddBytesToBuffers ( byte[] Data)

Definition at line 309 of file Packet.cs.

310 {
311 Write(Data);
312 dataBuffer = packetDataBuffer.ToArray();
313 }
byte[] dataBuffer
Byte array for reading data.
Definition Packet.cs:18

◆ AddIntAtStart()

void Packet.AddIntAtStart ( int number)

Definition at line 292 of file Packet.cs.

293 {
294 packetDataBuffer.InsertRange(0, BitConverter.GetBytes(number));
295 }

◆ ClearPacket()

void Packet.ClearPacket ( bool clear)

Definition at line 274 of file Packet.cs.

275 {
276 if (clear == false)
277 {
278 readPosition -= 4;
279 }
280 else
281 {
282 packetDataBuffer.Clear();
283 readPosition = 0;
284 dataBuffer = null;
285 }
286 }

◆ ConvertToArray()

byte[] Packet.ConvertToArray ( )

Definition at line 287 of file Packet.cs.

288 {
289 dataBuffer = packetDataBuffer.ToArray();
290 return dataBuffer;
291 }

◆ DataLength()

int Packet.DataLength ( )

Definition at line 300 of file Packet.cs.

301 {
302 return packetDataBuffer.Count;
303 }

◆ GetDataWithoutHmac()

byte[] Packet.GetDataWithoutHmac ( )

Return data without HMAC.

Definition at line 317 of file Packet.cs.

318 {
319 return packetDataBuffer.GetRange(0, packetDataBuffer.Count - 32).ToArray();
320 }

◆ GetHmac()

byte[] Packet.GetHmac ( )

return HMAC from data

Definition at line 324 of file Packet.cs.

325 {
326 return packetDataBuffer.GetRange(packetDataBuffer.Count - 32, 32).ToArray();
327 }

◆ ReadBool()

bool Packet.ReadBool ( bool changeReadPosition = true)

Read bool data.

Definition at line 134 of file Packet.cs.

135 {
136 if (packetDataBuffer.Count > readPosition)
137 {
138 bool data = BitConverter.ToBoolean(dataBuffer, readPosition);
139 if (changeReadPosition)
140 {
141 readPosition += 1;
142 }
143 return data;
144 }
145 else
146 {
147 throw new Exception("Error reading bool");
148 }
149 }

◆ ReadByte()

byte Packet.ReadByte ( bool changeReadPosition = true)

Read byte data.

Definition at line 161 of file Packet.cs.

162 {
163 if (packetDataBuffer.Count > readPosition)
164 {
165 byte data = dataBuffer[readPosition];
166 if (changeReadPosition == true)
167 {
168 readPosition += 1;
169 }
170 return data;
171 }
172 else
173 {
174 throw new Exception("Error reading byte");
175 }
176 }

◆ ReadBytes()

byte[] Packet.ReadBytes ( int arrayLength,
bool changeReadPosition = true )

read byte array data

Definition at line 188 of file Packet.cs.

189 {
190 if (packetDataBuffer.Count > readPosition)
191 {
192 byte[] ByteArray = packetDataBuffer.GetRange(readPosition, arrayLength).ToArray();
193 if (changeReadPosition == true)
194 {
195 readPosition += arrayLength;
196 }
197 return ByteArray;
198 }
199 else
200 {
201 throw new Exception("Error reading byte array");
202 }
203 }

◆ ReadFloat()

float Packet.ReadFloat ( bool changeReadPosition = true)

Read float data.

Definition at line 107 of file Packet.cs.

108 {
109 if (packetDataBuffer.Count > readPosition)
110 {
111 float floatingPointNum = BitConverter.ToSingle(dataBuffer, readPosition);
112 if (changeReadPosition == true)
113 {
114 readPosition += 4;
115 }
116 return floatingPointNum;
117 }
118 else
119 {
120 throw new Exception("Error reading float");
121 }
122 }

◆ ReadInt()

int Packet.ReadInt ( bool changeReadPosition = true)

Read int data.

Definition at line 80 of file Packet.cs.

81 {
82 if (packetDataBuffer.Count > readPosition)
83 {
84 int data = BitConverter.ToInt32(dataBuffer, readPosition);
85 if (changeReadPosition == true)
86 {
87 readPosition += 4;
88 }
89 return data;
90 }
91 else
92 {
93 throw new Exception("Error reading int");
94 }
95 }

◆ ReadQuaternion()

Quaternion Packet.ReadQuaternion ( bool changeReadPos = true)

Read Quaternion data.

Definition at line 263 of file Packet.cs.

264 {
265 float x = ReadFloat(changeReadPos);
266 float y = ReadFloat(changeReadPos);
267 float z = ReadFloat(changeReadPos);
268 float w = ReadFloat(changeReadPos);
269 return new Quaternion(x, y, z, w);
270 }
float ReadFloat(bool changeReadPosition=true)
Read float data.
Definition Packet.cs:107

◆ ReadShort()

short Packet.ReadShort ( bool changeReadPos = true)

Read short data.

Definition at line 215 of file Packet.cs.

216 {
217 if (packetDataBuffer.Count > readPosition)
218 {
219 short data = BitConverter.ToInt16(dataBuffer, readPosition);
220 if (changeReadPos == true)
221 {
222 readPosition += 2;
223 }
224 return data;
225 }
226 else
227 {
228 throw new Exception("Error reading short");
229 }
230 }

◆ ReadString()

string Packet.ReadString ( bool ChangeReadPosition = true)

Read string data.

Definition at line 52 of file Packet.cs.

53 {
54 try
55 {
56 int textLength = ReadInt();
57 string text = Encoding.ASCII.GetString(dataBuffer, readPosition, textLength);
58 if (ChangeReadPosition == true && text.Length > 0)
59 {
60 readPosition += textLength;
61 }
62 return text;
63 }
64 catch
65 {
66 throw new Exception("Error reading string");
67 }
68 }
int ReadInt(bool changeReadPosition=true)
Read int data.
Definition Packet.cs:80

◆ ReadVector3()

Vector3 Packet.ReadVector3 ( bool changeReadPos = true)

Read Vector3 data.

Definition at line 243 of file Packet.cs.

244 {
245 float x = ReadFloat(changeReadPos);
246 float y = ReadFloat(changeReadPos);
247 float z = ReadFloat(changeReadPos);
248 return new Vector3(x, y, z);
249 }

◆ SizeOfDataAtStart()

void Packet.SizeOfDataAtStart ( )

Definition at line 296 of file Packet.cs.

297 {
298 packetDataBuffer.InsertRange(0, BitConverter.GetBytes(packetDataBuffer.Count));
299 }

◆ UnreadLength()

int Packet.UnreadLength ( )

Definition at line 304 of file Packet.cs.

305 {
306 int actual_lenghth = DataLength();
307 return actual_lenghth - readPosition;
308 }
int DataLength()
Definition Packet.cs:300

◆ Write() [1/9]

void Packet.Write ( bool data)

Write bool data.

Definition at line 127 of file Packet.cs.

128 {
129 packetDataBuffer.AddRange(BitConverter.GetBytes(data));
130 }

◆ Write() [2/9]

void Packet.Write ( byte data)

Write byte data.

Definition at line 154 of file Packet.cs.

155 {
156 packetDataBuffer.Add(data);
157 }

◆ Write() [3/9]

void Packet.Write ( byte[] data)

Write byte array data.

Definition at line 181 of file Packet.cs.

182 {
183 packetDataBuffer.AddRange(data);
184 }

◆ Write() [4/9]

void Packet.Write ( float data)

Write float data.

Definition at line 100 of file Packet.cs.

101 {
102 packetDataBuffer.AddRange(BitConverter.GetBytes(data));
103 }

◆ Write() [5/9]

void Packet.Write ( int data)

Write int data.

Definition at line 73 of file Packet.cs.

74 {
75 packetDataBuffer.AddRange(BitConverter.GetBytes(data));
76 }

◆ Write() [6/9]

void Packet.Write ( Quaternion data)

Write Quaternion data.

Definition at line 253 of file Packet.cs.

254 {
255 Write(data.x);
256 Write(data.y);
257 Write(data.z);
258 Write(data.w);
259 }

◆ Write() [7/9]

void Packet.Write ( short data)

Write short data.

Definition at line 208 of file Packet.cs.

209 {
210 packetDataBuffer.AddRange(BitConverter.GetBytes(data));
211 }

◆ Write() [8/9]

void Packet.Write ( string text)

Write string data.

Definition at line 44 of file Packet.cs.

45 {
46 Write(text.Length);
47 packetDataBuffer.AddRange(Encoding.ASCII.GetBytes(text));
48 }

◆ Write() [9/9]

void Packet.Write ( Vector3 vector)

Write Vector3 data.

Definition at line 234 of file Packet.cs.

235 {
236 Write(vector.x);
237 Write(vector.y);
238 Write(vector.z);
239 }

Member Data Documentation

◆ dataBuffer

byte [] Packet.dataBuffer
private

Byte array for reading data.

Definition at line 18 of file Packet.cs.

◆ packetDataBuffer

List<byte> Packet.packetDataBuffer
private

Dynamic data list.

Definition at line 14 of file Packet.cs.

◆ readPosition

int Packet.readPosition
private

Definition at line 20 of file Packet.cs.