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

Represents a server-side connected client, including TCP and UDP communication handlers, user state, and connection security such as HMAC and authentication tokens. More...

Classes

class  TCP
 Class for managing TCP connection. More...
class  UDP
 Class for managing UDP connection. More...

Public Member Functions

string GetToken ()
 Gets the client's current authentication token.
 ServerSideClient (int newClientID)
 Initializes a new client instance and sets up its TCP and UDP handlers.
void Disconnect ()
 Disconnects the client from the server if not already disconnected. Cleans up the user object and resets the client slot.
void DisconnectRequest ()
 Handles a user-initiated disconnect request and broadcasts it to others.
void SendLatencyReply (int pcktId)
 Sends a latency reply (pong) to the client.
void ResendMessage (string message)
 Re-sends a private message back to the same client.
void ResendToAll (string Message)
 Re-sends a message to all connected clients except the sender.
void UpdateTransform (Vector3 pos, Quaternion rot)
 Sends updated transform (position and rotation) to all clients.
void UpdateAnimation (int type, string name, float value)
 Sends updated animation parameters to all clients.
void SendUserToAction (string username)
 Initializes a new user and synchronizes it with all other clients. Sends spawn info to self and others.

Static Public Member Functions

static string GenerateRandomToken (int length=32)
 Generates a random alphanumeric authentication token.
static void ConsoleLog (string message)

Public Attributes

int clientID
 Unique client ID on the server.
string clientUsername
 Username of the connected client.
TCP tcp
 TCP connection handler.
UDP udp
 UDP connection handler.
ServerSideClientInstance user
 Instance of the client's representation in the game world.
DefaultNetworkSettings DefaultSettings

Static Public Attributes

static int BufferSize = 4096
 Size of the data buffer used for transmission.

Private Member Functions

void SetToken ()
 Assigns a new authentication token to the client.

Static Private Member Functions

static byte[] GenerateHmacKey ()
 Generates a secure random 256-bit HMAC key.

Private Attributes

byte[] hmacKey
string auth_token
bool isDisconnected = true

Detailed Description

Represents a server-side connected client, including TCP and UDP communication handlers, user state, and connection security such as HMAC and authentication tokens.

Definition at line 18 of file ServerSideClient.cs.

Constructor & Destructor Documentation

◆ ServerSideClient()

ServerSideClient.ServerSideClient ( int newClientID)

Initializes a new client instance and sets up its TCP and UDP handlers.

Definition at line 76 of file ServerSideClient.cs.

77 {
78 isDisconnected = false;
79 clientID = newClientID;
80 DefaultSettings = ServerLogic.GetSettings();
81 tcp = new TCP(clientID);
82 udp = new UDP(clientID);
83
84 }
Class for managing TCP connection.
Class for managing UDP connection.
int clientID
Unique client ID on the server.
DefaultNetworkSettings DefaultSettings
TCP tcp
TCP connection handler.
UDP udp
UDP connection handler.

Member Function Documentation

◆ ConsoleLog()

void ServerSideClient.ConsoleLog ( string message)
static

Definition at line 429 of file ServerSideClient.cs.

430 {
431 Console.WriteLine(message + "\n");
432 }

◆ Disconnect()

void ServerSideClient.Disconnect ( )

Disconnects the client from the server if not already disconnected. Cleans up the user object and resets the client slot.

Definition at line 89 of file ServerSideClient.cs.

90 {
92 {
93 return;
94 }
95 if (tcp != null && tcp.Socket != null)
96 {
97 ConsoleLog($"{tcp.Socket.Client.RemoteEndPoint} [{clientUsername}] has disconnected from the Server");
98 ServerSendHandler.DisconnectedUser(clientID);
99 tcp.Disconnect();
100 udp.Disconnect();
101 ThreadsController.StartOnMainThread(() =>
102 {
103 UnityEngine.Object.Destroy(user.gameObject);
104 user = null;
105
106 });
107 isDisconnected = true;
108 ServerLogic.clientsList[clientID] = new ServerSideClient(clientID);
109 }
110 else
111 {
112 ConsoleLog($"Client {clientUsername} has already been disconnected.");
113 return;
114 }
115 }
static void ConsoleLog(string message)
ServerSideClient(int newClientID)
Initializes a new client instance and sets up its TCP and UDP handlers.
ServerSideClientInstance user
Instance of the client's representation in the game world.

◆ DisconnectRequest()

void ServerSideClient.DisconnectRequest ( )

Handles a user-initiated disconnect request and broadcasts it to others.

Definition at line 119 of file ServerSideClient.cs.

120 {
121 if (isDisconnected)
122 {
123 ConsoleLog($"[Disconnect] Already disconnected {clientUsername}");
124 return;
125 }
126 ConsoleLog($"{tcp.Socket.Client.RemoteEndPoint} [{clientUsername}] wants to disconnect");
127 string message = "Server message: " + clientUsername + " has disconnected";
128 ServerSendHandler.SendMessageAll(clientID, message);
129 Disconnect();
130 }
void Disconnect()
Disconnects the client from the server if not already disconnected. Cleans up the user object and res...
string clientUsername
Username of the connected client.

◆ GenerateHmacKey()

byte[] ServerSideClient.GenerateHmacKey ( )
staticprivate

Generates a secure random 256-bit HMAC key.

Definition at line 197 of file ServerSideClient.cs.

198 {
199 using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
200 {
201 byte[] key = new byte[32]; // 256-bitový HMAC kľúč
202 rng.GetBytes(key);
203 return key;
204 }
205 }

◆ GenerateRandomToken()

string ServerSideClient.GenerateRandomToken ( int length = 32)
static

Generates a random alphanumeric authentication token.

Definition at line 46 of file ServerSideClient.cs.

47 {
48 const string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
49 byte[] randomBytes = new byte[length];
50 using (var rng = RandomNumberGenerator.Create())
51 {
52 rng.GetBytes(randomBytes);
53 }
54
55 char[] chars = new char[length];
56 for (int i = 0; i < length; i++)
57 {
58 chars[i] = validChars[randomBytes[i] % validChars.Length];
59 }
60 return new string(chars);
61 }

◆ GetToken()

string ServerSideClient.GetToken ( )

Gets the client's current authentication token.

Definition at line 68 of file ServerSideClient.cs.

69 {
70 return auth_token;
71 }

◆ ResendMessage()

void ServerSideClient.ResendMessage ( string message)

Re-sends a private message back to the same client.

Definition at line 141 of file ServerSideClient.cs.

142 {
143 ServerSendHandler.SendMessage(clientID, message);
144 }

◆ ResendToAll()

void ServerSideClient.ResendToAll ( string Message)

Re-sends a message to all connected clients except the sender.

Definition at line 148 of file ServerSideClient.cs.

149 {
150 ServerSendHandler.SendMessageAll(clientID, Message);
151 }

◆ SendLatencyReply()

void ServerSideClient.SendLatencyReply ( int pcktId)

Sends a latency reply (pong) to the client.

Definition at line 134 of file ServerSideClient.cs.

135 {
136 ServerSendHandler.LatencyToUser(clientID, pcktId);
137 }

◆ SendUserToAction()

void ServerSideClient.SendUserToAction ( string username)

Initializes a new user and synchronizes it with all other clients. Sends spawn info to self and others.

Definition at line 170 of file ServerSideClient.cs.

171 {
172 user = NetworkServer.instance.NewUser();
173 user.UserInitialization(clientID, username, 1);
174 foreach (ServerSideClient client in ServerLogic.clientsList.Values)
175 {
176 if (client.user != null)
177 {
178 if (client.clientID != clientID)
179 {
180 ServerSendHandler.SpawnUser(clientID, client.user);
181 }
182 }
183 }
184 foreach (ServerSideClient client in ServerLogic.clientsList.Values)
185 {
186 if (client.user != null)
187 {
188 ServerSendHandler.SpawnUser(client.clientID, user);
189 }
190 }
191 string message = "Server message: " + username + " has connected";
192 ServerSendHandler.SendMessageAll(clientID, message);
193 }

◆ SetToken()

void ServerSideClient.SetToken ( )
private

Assigns a new authentication token to the client.

Definition at line 63 of file ServerSideClient.cs.

64 {
66 }
static string GenerateRandomToken(int length=32)
Generates a random alphanumeric authentication token.

◆ UpdateAnimation()

void ServerSideClient.UpdateAnimation ( int type,
string name,
float value )

Sends updated animation parameters to all clients.

Definition at line 162 of file ServerSideClient.cs.

163 {
164 ServerSendHandler.SendUpdatedAnimation(clientID, type, name, value);
165 }

◆ UpdateTransform()

void ServerSideClient.UpdateTransform ( Vector3 pos,
Quaternion rot )

Sends updated transform (position and rotation) to all clients.

Definition at line 155 of file ServerSideClient.cs.

156 {
157 ServerSendHandler.SendUpdatedTransform(clientID, pos, rot);
158 }

Member Data Documentation

◆ auth_token

string ServerSideClient.auth_token
private

Definition at line 39 of file ServerSideClient.cs.

◆ BufferSize

int ServerSideClient.BufferSize = 4096
static

Size of the data buffer used for transmission.

Definition at line 33 of file ServerSideClient.cs.

◆ clientID

int ServerSideClient.clientID

Unique client ID on the server.

Definition at line 21 of file ServerSideClient.cs.

◆ clientUsername

string ServerSideClient.clientUsername

Username of the connected client.

Definition at line 24 of file ServerSideClient.cs.

◆ DefaultSettings

DefaultNetworkSettings ServerSideClient.DefaultSettings

Definition at line 40 of file ServerSideClient.cs.

◆ hmacKey

byte [] ServerSideClient.hmacKey
private

Definition at line 38 of file ServerSideClient.cs.

◆ isDisconnected

bool ServerSideClient.isDisconnected = true
private

Definition at line 41 of file ServerSideClient.cs.

◆ tcp

TCP ServerSideClient.tcp

TCP connection handler.

Definition at line 27 of file ServerSideClient.cs.

◆ udp

UDP ServerSideClient.udp

UDP connection handler.

Definition at line 30 of file ServerSideClient.cs.

◆ user

ServerSideClientInstance ServerSideClient.user

Instance of the client's representation in the game world.

Definition at line 36 of file ServerSideClient.cs.