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

Main MonoBehaviour singleton class handling the client-side networking logic (TCP, UDP) in Unity. More...

Inheritance diagram for Client:

Classes

class  TCP
 Handles TCP connection. More...
class  UDP
 Handles UDP connection and communication for the client. More...

Public Member Functions

bool IsLocal (int checkId)
 Checks if the specified ID belongs to this local client.
void SetToken (string token)
 Sets the authentication token.
string GetToken ()
 Gets the authentication token.
byte[] GetHmacKey ()
 Gets the HMAC key used for secure communication.
void SetHmacKey (byte[] newHmacKey)
 Sets the HMAC key for secure communication.
string GetUsername ()
 Gets the current username.
void SetUsername (string newUsername)
 Sets the current username.
int GetId ()
 Gets the current client ID.
void SetId (int newId)
 Sets the client ID.
DefaultNetworkSettings GetDefaultSettings ()
 Gets default network settings used for connection.
void ConnectToServer ()
 Starts the connection process to the server.
void Disconnect ()
 Disconnects the client from the server.
void ResetNetwork ()
 Resets client network components and reloads the initial scene.
void MeassureRequest (int pcktId)
 Sends a ping measurement request to the server.
void MeassureAnswer (int pcktId)
 Handles the server’s ping reply and triggers the ping event.
void DisconnectRequested ()
 Sends a disconnect request to the server.
void DisconnectOther (int clientid)
 Disconnects another user with the given ID from the scene.
void MessageToUserRequest (string message, int clientId)
 Sends a message to a specific user.
void MessageToAllRequest (string message)
 Sends a message to all users in the session.
void SpawnUserPrefab (int id, string username)
 Spawns the user prefab locally or remotely.
void ReceivedMessage (string message)
 Triggers message-received event with the given message.
void UpdateTransform (Transform objectTransform)
 Sends current transform of a game object to the server.
void ChangeTransform (int id, Vector3 pos, Quaternion rot)
 Updates the transform of a remote player.
void AnimationUpdate (int type, string name, float value)
 Sends animation update to the server.
void ChangeAnimatorState (int id, int type, string name, float value)
 Applies received animation state from the server.
void BandwidthRequest (byte[] data)
 Sends request to measure bandwidth to the server.
void SendThroughputData (int pcktId, byte[] data)
 Sends a packet of data to test throughput.
void ReceivedBandwidth ()
 Called when bandwidth reply is received from the server.
void ReceivedThroughput (int pcktId, int dataSize)
 Called when throughput test result is received.

Static Public Member Functions

static void PacketAction (int packetId, Packet packet)
 Handles the received packet by type by calling methods in ClientReceiveHandler.

Public Attributes

TCP tcp
 Handles TCP communication with the server.
UDP udp
 Handles UDP communication with the server.
bool isConnected = false
 Indicates whether the client is currently connected to the server.

Static Public Attributes

static Client instance
 Singleton instance of the client.

Events

Action OnConnected
 Event triggered when the client successfully connects to the server.
Action< string > OnConnectionFailed
 Event triggered when the client fails to connect, providing an error message.
Action OnDisconnected
 Event triggered when the client is disconnected from the server.
Action< string > OnMessageReceived
 Event triggered when a text message is received from the server.
Action< int, Vector3, Quaternion > OnTransformReceived
 Event triggered when a transform update is received.
Action< int, int, string, float > OnAnimationChangeReceived
 Event triggered when an animation update is received.
Action< int > OnPingReply
 Event triggered when a ping response is received.
Action OnBandwidthReply
 Event triggered when the bandwidth test result is received.
Action< int, int > OnThroughputReply
 Event triggered when throughput test results are received.

Private Member Functions

void Awake ()
 Ensures only one instance of the client exists (Singleton pattern).
void OnApplicationQuit ()
 Disconnects the client when the application quits.
void SetClient ()
 Initializes TCP and UDP communication handlers for this client.

Private Attributes

DefaultNetworkSettings defaultNetworkSettings
 Network settings shared between client and server (IP, port, security, ...).
ClientSettings ClientSettings
 Client-specific configuration used during runtime.
int id = 0
 The unique client id assigned by the server.
string username = ""
 The current username of the client.
Coroutine latencyMeassure
 Reference to the coroutine responsible for measuring latency.
byte[] hmacKey
 HMAC key used for message authentication.
string authToken
 Authentication token used to verify client identity.

Static Private Attributes

static int DefaultBufferSize = 4096
 Default buffer size used for sending and receiving data (in bytes).

Detailed Description

Main MonoBehaviour singleton class handling the client-side networking logic (TCP, UDP) in Unity.

Definition at line 15 of file Client.cs.

Member Function Documentation

◆ AnimationUpdate()

void Client.AnimationUpdate ( int type,
string name,
float value )

Sends animation update to the server.

Definition at line 677 of file Client.cs.

678 {
679 ClientSendHandler.SendAnimationUpdate(type, name, value);
680 }

◆ Awake()

void Client.Awake ( )
private

Ensures only one instance of the client exists (Singleton pattern).

Definition at line 104 of file Client.cs.

105 {
106 if(instance == null)
107 {
108 instance = this;
109 }
110 else if(instance != this)
111 {
112 Destroy(this);
113 }
114 }
static Client instance
Singleton instance of the client.
Definition Client.cs:20

◆ BandwidthRequest()

void Client.BandwidthRequest ( byte[] data)

Sends request to measure bandwidth to the server.

Definition at line 691 of file Client.cs.

692 {
693 ClientSendHandler.SendBandwidthRequest(data);
694 }

◆ ChangeAnimatorState()

void Client.ChangeAnimatorState ( int id,
int type,
string name,
float value )

Applies received animation state from the server.

Definition at line 684 of file Client.cs.

685 {
686 instance.OnAnimationChangeReceived?.Invoke(id, type, name, value);
687 }

◆ ChangeTransform()

void Client.ChangeTransform ( int id,
Vector3 pos,
Quaternion rot )

Updates the transform of a remote player.

Definition at line 670 of file Client.cs.

671 {
672 instance.OnTransformReceived?.Invoke(id, pos, rot);
673 }

◆ ConnectToServer()

void Client.ConnectToServer ( )

Starts the connection process to the server.

Definition at line 283 of file Client.cs.

284 {
285 SetClient();
286 tcp.Connect();
287 }
TCP tcp
Handles TCP communication with the server.
Definition Client.cs:44
void SetClient()
Initializes TCP and UDP communication handlers for this client.
Definition Client.cs:199

◆ Disconnect()

void Client.Disconnect ( )

Disconnects the client from the server.

Definition at line 555 of file Client.cs.

556 {
557 if (isConnected == true)
558 {
559 isConnected = false;
560 tcp.Socket.Close();
561 udp.Socket.Close();
562 ThreadsController.StartOnMainThread(() =>
563 {
564 ResetNetwork();
565 });
566 instance.OnDisconnected?.Invoke();
567 }
568 }
UDP udp
Handles UDP communication with the server.
Definition Client.cs:48
void ResetNetwork()
Resets client network components and reloads the initial scene.
Definition Client.cs:572
bool isConnected
Indicates whether the client is currently connected to the server.
Definition Client.cs:52

◆ DisconnectOther()

void Client.DisconnectOther ( int clientid)

Disconnects another user with the given ID from the scene.

Definition at line 616 of file Client.cs.

617 {
618 NetworkManager.networkManager.DisconnectForeignUser(clientid);
619 }

◆ DisconnectRequested()

void Client.DisconnectRequested ( )

Sends a disconnect request to the server.

Definition at line 606 of file Client.cs.

607 {
608 if(isConnected == true)
609 {
610 ClientSendHandler.DisconnectRequest();
611 }
612 }

◆ GetDefaultSettings()

DefaultNetworkSettings Client.GetDefaultSettings ( )

Gets default network settings used for connection.

Definition at line 185 of file Client.cs.

186 {
188 }
DefaultNetworkSettings defaultNetworkSettings
Network settings shared between client and server (IP, port, security, ...).
Definition Client.cs:28

◆ GetHmacKey()

byte[] Client.GetHmacKey ( )

Gets the HMAC key used for secure communication.

Definition at line 143 of file Client.cs.

144 {
145 return hmacKey;
146 }
byte[] hmacKey
HMAC key used for message authentication.
Definition Client.cs:60

◆ GetId()

int Client.GetId ( )

Gets the current client ID.

Definition at line 171 of file Client.cs.

172 {
173 return id;
174 }
int id
The unique client id assigned by the server.
Definition Client.cs:36

◆ GetToken()

string Client.GetToken ( )

Gets the authentication token.

Definition at line 136 of file Client.cs.

137 {
138 return authToken;
139 }
string authToken
Authentication token used to verify client identity.
Definition Client.cs:64

◆ GetUsername()

string Client.GetUsername ( )

Gets the current username.

Definition at line 157 of file Client.cs.

158 {
159 return username;
160 }
string username
The current username of the client.
Definition Client.cs:40

◆ IsLocal()

bool Client.IsLocal ( int checkId)

Checks if the specified ID belongs to this local client.

Definition at line 118 of file Client.cs.

119 {
120 if(id == checkId) {
121 return true;
122 } else {
123 return false;
124 }
125 }

◆ MeassureAnswer()

void Client.MeassureAnswer ( int pcktId)

Handles the server’s ping reply and triggers the ping event.

Definition at line 599 of file Client.cs.

600 {
601 instance.OnPingReply?.Invoke(pcktId);
602 }

◆ MeassureRequest()

void Client.MeassureRequest ( int pcktId)

Sends a ping measurement request to the server.

Definition at line 589 of file Client.cs.

590 {
591 if(isConnected == true)
592 {
593 ClientSendHandler.MeassureRequest(pcktId);
594 }
595 }

◆ MessageToAllRequest()

void Client.MessageToAllRequest ( string message)

Sends a message to all users in the session.

Definition at line 633 of file Client.cs.

634 {
635 if(isConnected == true)
636 {
637 ClientSendHandler.MessageToAllRequest(message);
638 }
639 }

◆ MessageToUserRequest()

void Client.MessageToUserRequest ( string message,
int clientId )

Sends a message to a specific user.

Definition at line 623 of file Client.cs.

624 {
625 if(isConnected == true)
626 {
627 ClientSendHandler.MessageToUserRequest(message, clientId);
628 }
629 }

◆ OnApplicationQuit()

void Client.OnApplicationQuit ( )
private

Disconnects the client when the application quits.

Definition at line 192 of file Client.cs.

193 {
194 Disconnect();
195 }
void Disconnect()
Disconnects the client from the server.
Definition Client.cs:555

◆ PacketAction()

void Client.PacketAction ( int packetId,
Packet packet )
static

Handles the received packet by type by calling methods in ClientReceiveHandler.

Definition at line 215 of file Client.cs.

216 {
217 if (!Enum.IsDefined(typeof(Packet.ServerPackets), packetId))
218 {
219 Debug.LogWarning($"Unknown packet ID: {packetId}");
220 return;
221 }
222
223 Packet.ServerPackets packetType = (Packet.ServerPackets)packetId;
224
225 switch (packetType)
226 {
227 case Packet.ServerPackets.welcome:
228 ClientReceiveHandler.Welcome(packet);
229 break;
230
231 case Packet.ServerPackets.SpawnUser:
232 ClientReceiveHandler.SpawnUser(packet);
233 break;
234
235 case Packet.ServerPackets.DisconnectUser:
236 ClientReceiveHandler.DisconnectUser(packet);
237 break;
238
239 case Packet.ServerPackets.Latency:
240 ClientReceiveHandler.Latency(packet);
241 break;
242
243 case Packet.ServerPackets.Message:
244 ClientReceiveHandler.ReceiveMessage(packet);
245 break;
246
247 case Packet.ServerPackets.HmacKey:
248 ClientReceiveHandler.ReceiveHmacKey(packet);
249 break;
250
251 case Packet.ServerPackets.AuthToken:
252 ClientReceiveHandler.ReceiveToken(packet);
253 break;
254
255 case Packet.ServerPackets.MessageAll:
256 ClientReceiveHandler.ReceiveMessageAll(packet);
257 break;
258
259 case Packet.ServerPackets.TransformUpdate:
260 ClientReceiveHandler.TransformUpdate(packet);
261 break;
262
263 case Packet.ServerPackets.AnimationUpdate:
264 ClientReceiveHandler.AnimationUpdate(packet);
265 break;
266
267 case Packet.ServerPackets.BandwidthReply:
268 ClientReceiveHandler.BandwidthReply(packet);
269 break;
270
271 case Packet.ServerPackets.ThroughputReply:
272 ClientReceiveHandler.ThroughputReply(packet);
273 break;
274
275 default:
276 Debug.LogWarning($"Unhandled packet type: {packetType}");
277 break;
278 }
279 }

◆ ReceivedBandwidth()

void Client.ReceivedBandwidth ( )

Called when bandwidth reply is received from the server.

Definition at line 705 of file Client.cs.

706 {
707 instance.OnBandwidthReply?.Invoke();
708 }

◆ ReceivedMessage()

void Client.ReceivedMessage ( string message)

Triggers message-received event with the given message.

Definition at line 656 of file Client.cs.

657 {
658 instance.OnMessageReceived?.Invoke(message);
659 }

◆ ReceivedThroughput()

void Client.ReceivedThroughput ( int pcktId,
int dataSize )

Called when throughput test result is received.

Definition at line 712 of file Client.cs.

713 {
714 instance.OnThroughputReply?.Invoke(pcktId, dataSize);
715 }

◆ ResetNetwork()

void Client.ResetNetwork ( )

Resets client network components and reloads the initial scene.

This can be changed.

Definition at line 572 of file Client.cs.

573 {
574 Disconnect();
575 if (latencyMeassure != null)
576 {
577 StopCoroutine(latencyMeassure);
578 latencyMeassure = null;
579 }
580 SetClient();
584 SceneManager.LoadScene(0);
585 }
Coroutine latencyMeassure
Reference to the coroutine responsible for measuring latency.
Definition Client.cs:56

◆ SendThroughputData()

void Client.SendThroughputData ( int pcktId,
byte[] data )

Sends a packet of data to test throughput.

Definition at line 698 of file Client.cs.

699 {
700 ClientSendHandler.SendThroughputRequest(pcktId, data);
701 }

◆ SetClient()

void Client.SetClient ( )
private

Initializes TCP and UDP communication handlers for this client.

Definition at line 199 of file Client.cs.

200 {
201 if (tcp != null)
202 {
203 tcp = null;
204 }
205 if (udp != null)
206 {
207 udp = null;
208 }
209 tcp = new TCP();
210 udp = new UDP();
211 }
Handles TCP connection.
Definition Client.cs:292
Handles UDP connection and communication for the client.
Definition Client.cs:469

◆ SetHmacKey()

void Client.SetHmacKey ( byte[] newHmacKey)

Sets the HMAC key for secure communication.

Definition at line 150 of file Client.cs.

151 {
152 hmacKey = newHmacKey;
153 }

◆ SetId()

void Client.SetId ( int newId)

Sets the client ID.

Definition at line 178 of file Client.cs.

179 {
180 id = newId;
181 }

◆ SetToken()

void Client.SetToken ( string token)

Sets the authentication token.

Definition at line 129 of file Client.cs.

130 {
131 authToken = token;
132 }

◆ SetUsername()

void Client.SetUsername ( string newUsername)

Sets the current username.

Definition at line 164 of file Client.cs.

165 {
166 username = newUsername;
167 }

◆ SpawnUserPrefab()

void Client.SpawnUserPrefab ( int id,
string username )

Spawns the user prefab locally or remotely.

Definition at line 643 of file Client.cs.

644 {
645 if (id == instance.GetId())
646 {
647 NetworkManager.networkManager.SpawnUser(id,username,true);
648 } else
649 {
650 NetworkManager.networkManager.SpawnUser(id, username, false);
651 }
652 }

◆ UpdateTransform()

void Client.UpdateTransform ( Transform objectTransform)

Sends current transform of a game object to the server.

Definition at line 663 of file Client.cs.

664 {
665 ClientSendHandler.SendTransform(instance.GetId(), objectTransform);
666 }

Member Data Documentation

◆ authToken

string Client.authToken
private

Authentication token used to verify client identity.

Definition at line 64 of file Client.cs.

◆ ClientSettings

ClientSettings Client.ClientSettings
private

Client-specific configuration used during runtime.

Definition at line 32 of file Client.cs.

◆ DefaultBufferSize

int Client.DefaultBufferSize = 4096
staticprivate

Default buffer size used for sending and receiving data (in bytes).

Definition at line 24 of file Client.cs.

◆ defaultNetworkSettings

DefaultNetworkSettings Client.defaultNetworkSettings
private

Network settings shared between client and server (IP, port, security, ...).

Definition at line 28 of file Client.cs.

◆ hmacKey

byte [] Client.hmacKey
private

HMAC key used for message authentication.

Definition at line 60 of file Client.cs.

◆ id

int Client.id = 0
private

The unique client id assigned by the server.

Definition at line 36 of file Client.cs.

◆ instance

Client Client.instance
static

Singleton instance of the client.

Definition at line 20 of file Client.cs.

◆ isConnected

bool Client.isConnected = false

Indicates whether the client is currently connected to the server.

Definition at line 52 of file Client.cs.

◆ latencyMeassure

Coroutine Client.latencyMeassure
private

Reference to the coroutine responsible for measuring latency.

Definition at line 56 of file Client.cs.

◆ tcp

TCP Client.tcp

Handles TCP communication with the server.

Definition at line 44 of file Client.cs.

◆ udp

UDP Client.udp

Handles UDP communication with the server.

Definition at line 48 of file Client.cs.

◆ username

string Client.username = ""
private

The current username of the client.

Definition at line 40 of file Client.cs.

Event Documentation

◆ OnAnimationChangeReceived

Action<int,int, string, float> Client.OnAnimationChangeReceived

Event triggered when an animation update is received.

Definition at line 88 of file Client.cs.

◆ OnBandwidthReply

Action Client.OnBandwidthReply

Event triggered when the bandwidth test result is received.

Definition at line 96 of file Client.cs.

◆ OnConnected

Action Client.OnConnected

Event triggered when the client successfully connects to the server.

Definition at line 68 of file Client.cs.

◆ OnConnectionFailed

Action<string> Client.OnConnectionFailed

Event triggered when the client fails to connect, providing an error message.

Definition at line 72 of file Client.cs.

◆ OnDisconnected

Action Client.OnDisconnected

Event triggered when the client is disconnected from the server.

Definition at line 76 of file Client.cs.

◆ OnMessageReceived

Action<string> Client.OnMessageReceived

Event triggered when a text message is received from the server.

Definition at line 80 of file Client.cs.

◆ OnPingReply

Action<int> Client.OnPingReply

Event triggered when a ping response is received.

Definition at line 92 of file Client.cs.

◆ OnThroughputReply

Action<int,int> Client.OnThroughputReply

Event triggered when throughput test results are received.

Definition at line 100 of file Client.cs.

◆ OnTransformReceived

Action<int,Vector3,Quaternion> Client.OnTransformReceived

Event triggered when a transform update is received.

Definition at line 84 of file Client.cs.