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

Handles incoming packets from the server and performs client-side actions accordingly. More...

Inheritance diagram for ClientReceiveHandler:

Static Public Member Functions

static bool ValidateHmac (Packet receivedPacket)
 Validates the HMAC of a received packet using the client's stored key.
static byte[] GenerateHmac (byte[] data, byte[] key)
 Generates a HMAC-SHA256 hash for the provided data using the given key.
static void Welcome (Packet receivedPacket)
 Handles the welcome packet from the server, sets the client ID, and initiates UDP connection.
static void ReceiveHmacKey (Packet receivedPacket)
 Handles receiving the HMAC key from the server and stores it on the client.
static void ReceiveToken (Packet receivedPacket)
 Handles receiving an authentication token from the server and stores it on the client.
static void Latency (Packet receivedPacket)
 Handles a latency response packet from the server.
static void SpawnUser (Packet receivedPacket)
 Spawns a user prefab on the client.
static void BandwidthReply (Packet receivedPacket)
 Handles a bandwidth test response from the server.
static void ThroughputReply (Packet receivedPacket)
 Handles a throughput test response from the server.
static void ReceiveMessage (Packet receivedPacket)
 Receives a private text message from server.
static void ReceiveMessageAll (Packet receivePacket)
 Receives a broadcast message sent to all clients by the server.
static void DisconnectUser (Packet receivedPacket)
 Handles disconnection of a specific client.
static void TransformUpdate (Packet receivedPacket)
 Receives and applies a position and rotation update for a user in the scene.
static void AnimationUpdate (Packet receivedPacket)
 Receives and applies an animation update from the server for a specific user.

Detailed Description

Handles incoming packets from the server and performs client-side actions accordingly.

Definition at line 10 of file ClientReceiveHandler.cs.

Member Function Documentation

◆ AnimationUpdate()

void ClientReceiveHandler.AnimationUpdate ( Packet receivedPacket)
static

Receives and applies an animation update from the server for a specific user.

Definition at line 169 of file ClientReceiveHandler.cs.

170 {
171 int id = receivedPacket.ReadInt();
172 int type = receivedPacket.ReadInt();
173 string paramName = receivedPacket.ReadString();
174 float value;
175 if (type != 2)
176 {
177 value = receivedPacket.ReadFloat();
178 } else {
179 value = 0f;
180 }
181 Client.instance.ChangeAnimatorState(id,type,paramName,value);
182 }
float ReadFloat(bool changeReadPosition=true)
Read float data.
Definition Packet.cs:107
int ReadInt(bool changeReadPosition=true)
Read int data.
Definition Packet.cs:80
string ReadString(bool ChangeReadPosition=true)
Read string data.
Definition Packet.cs:52

◆ BandwidthReply()

void ClientReceiveHandler.BandwidthReply ( Packet receivedPacket)
static

Handles a bandwidth test response from the server.

Definition at line 97 of file ClientReceiveHandler.cs.

98 {
99 if (Client.instance.GetDefaultSettings().useHMAC == true)
100 {
101 if (!ValidateHmac(receivedPacket))
102 {
103 Debug.LogError("Invalid HMAC received for packet. Dropping packet.");
104 return;
105 }
106 }
107 Client.instance.ReceivedBandwidth();
108 }
static bool ValidateHmac(Packet receivedPacket)
Validates the HMAC of a received packet using the client's stored key.

◆ DisconnectUser()

void ClientReceiveHandler.DisconnectUser ( Packet receivedPacket)
static

Handles disconnection of a specific client.

Definition at line 145 of file ClientReceiveHandler.cs.

146 {
147 int clientId = receivedPacket.ReadInt();
148 if(clientId == Client.instance.GetId())
149 {
150 Client.instance.Disconnect();
151 } else
152 {
153 Client.instance.DisconnectOther(clientId);
154 }
155 }

◆ GenerateHmac()

byte[] ClientReceiveHandler.GenerateHmac ( byte[] data,
byte[] key )
static

Generates a HMAC-SHA256 hash for the provided data using the given key.

Definition at line 28 of file ClientReceiveHandler.cs.

29 {
30 using (HMACSHA256 hmac = new HMACSHA256(key))
31 {
32 return hmac.ComputeHash(data);
33 }
34 }

◆ Latency()

void ClientReceiveHandler.Latency ( Packet receivedPacket)
static

Handles a latency response packet from the server.

Definition at line 71 of file ClientReceiveHandler.cs.

72 {
73 int clientId = receivedPacket.ReadInt();
74 int pcktId = receivedPacket.ReadInt();
75 Client.instance.MeassureAnswer(pcktId);
76 }

◆ ReceiveHmacKey()

void ClientReceiveHandler.ReceiveHmacKey ( Packet receivedPacket)
static

Handles receiving the HMAC key from the server and stores it on the client.

Definition at line 54 of file ClientReceiveHandler.cs.

55 {
56 int keyLength = receivedPacket.ReadInt();
57 byte[] hmacKey = receivedPacket.ReadBytes(keyLength);
58 Client.instance.SetHmacKey(hmacKey);
59 }
byte[] ReadBytes(int arrayLength, bool changeReadPosition=true)
read byte array data
Definition Packet.cs:188

◆ ReceiveMessage()

void ClientReceiveHandler.ReceiveMessage ( Packet receivedPacket)
static

Receives a private text message from server.

Definition at line 121 of file ClientReceiveHandler.cs.

122 {
123 if (Client.instance.GetDefaultSettings().useHMAC == true)
124 {
125 if (!ValidateHmac(receivedPacket))
126 {
127 Debug.LogError("Invalid HMAC received for packet. Dropping packet.");
128 return;
129 }
130 }
131 string message = receivedPacket.ReadString();
132 Client.instance.ReceivedMessage(message);
133 }

◆ ReceiveMessageAll()

void ClientReceiveHandler.ReceiveMessageAll ( Packet receivePacket)
static

Receives a broadcast message sent to all clients by the server.

Definition at line 137 of file ClientReceiveHandler.cs.

138 {
139 string message = receivePacket.ReadString();
140 Client.instance.ReceivedMessage(message);
141 }

◆ ReceiveToken()

void ClientReceiveHandler.ReceiveToken ( Packet receivedPacket)
static

Handles receiving an authentication token from the server and stores it on the client.

Definition at line 63 of file ClientReceiveHandler.cs.

64 {
65 string token = receivedPacket.ReadString();
66 Client.instance.SetToken(token);
67 }

◆ SpawnUser()

void ClientReceiveHandler.SpawnUser ( Packet receivedPacket)
static

Spawns a user prefab on the client.

Definition at line 80 of file ClientReceiveHandler.cs.

81 {
82 if (Client.instance.GetDefaultSettings().useHMAC == true)
83 {
84 if (!ValidateHmac(receivedPacket))
85 {
86 Debug.LogError("Invalid HMAC received for packet. Dropping packet.");
87 return;
88 }
89 }
90 int clientId = receivedPacket.ReadInt();
91 string userName = receivedPacket.ReadString();
92 Client.instance.SpawnUserPrefab(clientId, userName);
93 }

◆ ThroughputReply()

void ClientReceiveHandler.ThroughputReply ( Packet receivedPacket)
static

Handles a throughput test response from the server.

Definition at line 112 of file ClientReceiveHandler.cs.

113 {
114 int pcktId = receivedPacket.ReadInt();
115 int dataSize = receivedPacket.ReadInt();
116 Client.instance.ReceivedThroughput(pcktId, dataSize);
117 }

◆ TransformUpdate()

void ClientReceiveHandler.TransformUpdate ( Packet receivedPacket)
static

Receives and applies a position and rotation update for a user in the scene.

Definition at line 159 of file ClientReceiveHandler.cs.

160 {
161 int id = receivedPacket.ReadInt();
162 Vector3 pos = receivedPacket.ReadVector3();
163 Quaternion rot = receivedPacket.ReadQuaternion();
164 Client.instance.ChangeTransform(id, pos, rot);
165 }
Quaternion ReadQuaternion(bool changeReadPos=true)
Read Quaternion data.
Definition Packet.cs:263
Vector3 ReadVector3(bool changeReadPos=true)
Read Vector3 data.
Definition Packet.cs:243

◆ ValidateHmac()

bool ClientReceiveHandler.ValidateHmac ( Packet receivedPacket)
static

Validates the HMAC of a received packet using the client's stored key.

Validity check of HMAC.

Definition at line 15 of file ClientReceiveHandler.cs.

16 {
17 byte[] dataWithoutHmac = receivedPacket.GetDataWithoutHmac();
18 byte[] receivedHmac = receivedPacket.GetHmac();
19 byte[] expectedHmac = GenerateHmac(dataWithoutHmac, Client.instance.GetHmacKey());
23 return StructuralComparisons.StructuralEqualityComparer.Equals(receivedHmac, expectedHmac);
24 }
static byte[] GenerateHmac(byte[] data, byte[] key)
Generates a HMAC-SHA256 hash for the provided data using the given key.
byte[] GetDataWithoutHmac()
Return data without HMAC.
Definition Packet.cs:317
byte[] GetHmac()
return HMAC from data
Definition Packet.cs:324

◆ Welcome()

void ClientReceiveHandler.Welcome ( Packet receivedPacket)
static

Handles the welcome packet from the server, sets the client ID, and initiates UDP connection.

Definition at line 38 of file ClientReceiveHandler.cs.

39 {
40 string ReceivedMessage = receivedPacket.ReadString();
41 int id = receivedPacket.ReadInt();
42 Client.instance.SetId(id);
43 if(ReceivedMessage != "")
44 {
45 Client.instance.ReceivedMessage($"Server message: {ReceivedMessage} [{Client.instance.GetUsername()}]");
46 }
47 ClientSendHandler.WelcomeReceived();
48 Client.instance.isConnected = true;
49 Client.instance.udp.Connect(((IPEndPoint)Client.instance.tcp.Socket.Client.LocalEndPoint).Port);
50 }