- UDP.Server
- UDP.Server.Start
- UDP.Server.Restart
- UDP.Server.Stop
- UDP.Server.SendBytes
- UDP.Server.SendBytesGroup
- UDP.Server.SendBytesAll
- UDP.Server.DisconnectClient
- UDP.Server.DisconnectClientGroup
- UDP.Server.DisconnectClientAll
- UDP.Server.ChangeLimitMaxByteSizeGroupID
- UDP.Server.ChangeLimitMaxPacketsPerSecondsGroupID
- UDP.Server.ChangeBlockIP
- UDP.Server.Socket
- UDP.Server.LimitMaxByteReceive
- UDP.Server.LimitMaxPacketsPerSeconds
- UDP.Server.Status
- UDP.Server.LostPackets
- UDP.Server.MaxClients
- UDP.Server.PacketsPerSeconds
- UDP.Server.PacketsBytesReceived
- UDP.Server.PacketsBytesSent
- UDP.Server.ReceiveAndSendTimeOut
- UDP.Server.ShowUnityNetworkStatistics
- UDP.Server.ClientsCount
- UDP.Server.ShowDebugConsole
- UDP.Server.OnConnectedClient
- UDP.Server.OnDisconnectedClient
- UDP.Server.ServerStatusConnection
- UDP.Server.OnReceivedBytes;
- UDP.Client
- UDP.Client.Connect
- UDP.Client.SendBytes
- UDP.Client.DisconnectServer
- UDP.Client.Socket
- UDP.Client.Status
- UDP.Client.LostPackets
- UDP.Client.PacketsPerSeconds
- UDP.Client.PacketsBytesReceived
- UDP.Client.PacketsBytesSent
- UDP.Client.ReceiveAndSendTimeOut
- UDP.Client.ConnectTimeOut
- UDP.Client.ShowUnityNetworkStatistics
- UDP.Client.ShowDebugConsole
- UDP.Client.Ping
- UDP.Client.OnReceivedBytes
- UDP.Client.ClientStatusConnection
- UDP.Client.PublicKeyRSA
- UDP.Client.PrivateKeyAES
- Others
- FAQ
UDP.Server.Start(IPAddress _ip, int _port, int _symmetricSizeRSA = 86)
UDP.Server.Start(IPAddress.Any, 25000, 16);
Start the server with specific IP, Port and sets the size of SymmetricSizeRSA if needed. If the server has already been started and then stopped you can call UDP.Server.Start();
without defining _host and _symmetricSizeRSA to start the server with the previous settings.
UDP.Server.Restart()
UDP.Server.Restart();
If the server is running, you can restart it, all connected clients will be disconnected from the server and new RSA and AES keys will be generated again.
UDP.Server.Stop()
UDP.Server.Stop();
If the server is running, you can stop it, all connected clients will be disconnected from the server and if you start the server again new RSA and AES keys will be generated.
UDP.Server.SendBytes(byte[] _byte, int _groupID, DataClient _dataClient, TypeShipping _typeShipping = TypeShipping.None, bool _holdConnection = false)
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
// Sending only groupID to client, without encryption and without HoldConnection
UDP.Server.SendBytes(null, 4, _dataClient);
var _bytes = System.Text.Encoding.ASCII.GetBytes("Hello world!");
// Sending bytes with groupID to client, without encryption and without HoldConnection
UDP.Server.SendBytes(_byte, 4, _dataClient);
// Sending bytes with groupID to client, with encryption AES and without HoldConnection
UDP.Server.SendBytes(_byte, 4, _dataClient, TypeShipping.AES);
// Sending bytes with groupID to client, with encryption RSA and with HoldConnection
UDP.Server.SendBytes(_byte, 4, _dataClient, TypeShipping.RSA, true);
}
To send bytes to a client, it is necessary to define the bytes, GroupID and DataClient, the other sending resources such as TypeShipping and HoldConnection are optional.
UDP.Server.SendBytesGroup(byte[] _byte, int _groupID, ConcurrentQueue<DataClient> _dataClients, TypeShipping _typeShipping = TypeShipping.None, DataClient _skipDataClient = null, bool _holdConnection = false)
using System.Collections.Concurrent;
static ConcurrentQueue<DataClient> PlayersLobby = new ConcurrentQueue<DataClient>();
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
// Sending only groupID to all clients on the list, without encryption and without HoldConnection
UDP.Server.SendBytesGroup(null, 4, _dataClient);
var _bytes = System.Text.Encoding.ASCII.GetBytes("Play Game");
// Sending bytes with groupID to all clients on the list
UDP.Server.SendBytesGroup(_byte, 4, PlayersLobby);
// Sending bytes with groupID to all clients on the list, except for the sending client.
UDP.Server.SendBytesGroup(_byte, 4, PlayersLobby, _skipDataClient: _dataClient);
}
To send bytes to a group client, it is necessary to define the bytes, GroupID and List DataClient, the other sending resources such as TypeShipping, SkipDataClient and HoldConnection are optional.
UDP.Server.SendBytesAll(byte[] _byte, int _groupID, TypeShipping _typeShipping = TypeShipping.None, DataClient _skipDataClient = null, bool _holdConnection = false)
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
// Sending only groupID to all clients connected, without encryption and without HoldConnection
UDP.Server.SendBytesAll(null, 4, _dataClient);
var _bytes = System.Text.Encoding.ASCII.GetBytes("Play Game");
// Sending bytes with groupID to all clients connected.
UDP.Server.SendBytesAll(_byte, 4);
// Sending bytes with groupID to all clients connected, except for the sending client.
UDP.Server.SendBytesAll(_byte, 4, PlayersLobby, _skipDataClient: _dataClient);
}
To send bytes to all clients, it is necessary to define the bytes, GroupID, the other sending resources such as TypeShipping, SkipDataClient and HoldConnection are optional.
UDP.Server.DisconnectClient(DataClient _dataClient)
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
UDP.Server.DisconnectClient(_dataClient);
}
To disconnect a client from server, it is necessary to inform the DataClient.
UDP.Server.DisconnectClientGroup(<List>DataClient _dataClient)
using System.Collections.Concurrent;
static ConcurrentQueue<DataClient> AFKPlayers = new ConcurrentQueue<DataClient>();
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
UDP.Server.DisconnectClientGroup(AFKPlayers);
}
To disconnect a group clients from server, it is necessary to inform the List DataClient.
UDP.Server.DisconnectClientAll()
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
UDP.Server.DisconnectClientAll();
}
To disconnect alls clients from server.
UDP.Server.ChangeLimitMaxByteSizeGroupID(int _groupID, int _limitBytes)
UDP.Server.ChangeLimitMaxByteSizeGroupID(4, 12);
The ChangeLimitMaxByteSizeGroupID will change the maximum limit of bytes of a GroupID that the server will read when receiving the bytes, if the packet bytes is greater than the limit, the server will not call the UDP.Server.OnReceivedBytes event with the received bytes. The default value _limitBytes is 0 which is unlimited.
UDP.Server.ChangeLimitMaxPacketsPerSecondsGroupID(int _groupID, int _limitPPS)
UDP.Server.ChangeLimitMaxPacketsPerSecondsGroupID(4, 60);
The ChangeLimitMaxPacketsPerSecondsGroupID will change the maximum limit of Packets per seconds (PPS) of a GroupID, if the packets is greater than the limit in 1 second, the server will not call the UDP.Server.OnReceivedBytes event with the received bytes. The default value is _limitBytes 0 which is unlimited.
UDP.Server.ChangeBlockIP(IPEndPoint _ip, int _time)
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
// IP blocked per 1 hour
UDP.Server.ChangeBlockIP(_dataClient.IP, 60000 * 60);
}
ChangeBlockIP blocks a specific IP for the time defined in milliseconds. If the time is 0 the IP will be removed from the server's blocked IP list.
Read-Only Variable
static void OnConnectedClient(DataClient _dataClient){
int onlines = UDP.Server.ClientsCount;
Console.WriteLine("Has a total of {0} players connected.", onlines);
}
The ClientsCount is the total number of clients connected to the server.
Write/Read Variable
// Limit in 12 bytes;
UDP.Server.LimitMaxByteReceive = 12;
The LimitMaxByteReceive will change the maximum limit of bytes that the server will read when receiving, if the packet bytes is greater than the limit, the server will not call the UDP.Server.OnReceivedBytes event with the received bytes. The default value is 0 which is unlimited.
Write/Read Variable
// Limit in 60 pps;
UDP.Server.LimitMaxPacketsPerSeconds = 60;
The LimitMaxPacketsPerSeconds will change the maximum limit of Packets per seconds (PPS), if the packets is greater than the limit in 1 second, the server will not call the UDP.Server.OnReceivedBytes event with the received bytes. The default value is 0 which is unlimited.
Read-Only Variable
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
int LostPackets = UDP.Server.LostPackets;
Console.WriteLine("{0} packets lost", LostPackets);
}
LostPackets is the number of packets lost.
Write/Read Variable
UDP.Server.MaxClients = 32; // Maximum 32 Clients
MaxClients is the maximum number of clients that can connect to the server. If you have many connected clients and you change the value below the number of connected clients, they will not be disconnected, the server will block new connections until the number of connected clients is below or equal to the limit.
Read-Only Variable
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
string packetsPerSeconds = UDP.Server.PacketsPerSeconds;
Console.WriteLine("{0} Packets Per Seconds", packetsPerSeconds);
}
PacketsPerSeconds is the amount of packets per second that happen when the server is sending and receiving.
Read-Only Variable
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
string packetsBytesReceived = UDP.Server.PacketsBytesReceived;
Console.WriteLine("Received: {0}", packetsBytesReceived);
}
PacketsBytesReceived is the amount of bytes received by the server.
Read-Only Variable
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
string packetsBytesSent = UDP.Server.PacketsBytesSent;
Console.WriteLine("Sent: {0}", packetsBytesSent);
}
PacketsBytesSent is the amount of bytes sent by the server.
Write/Read Variable
UDP.Server.ReceiveAndSendTimeOut = 2000;
ReceiveAndSendTimeOut defines the timeout in milliseconds for sending and receiving, if any packet exceeds that sending the server will ignore the receiving or sending. The default and recommended value is 1000.
Write/Read Variable
UDP.Server.ShowDebugConsole = false;
// Or
UDP.Client.ShowDebugConsole = false;
The ShowDebugConsole when declaring false, the logs in Console.Write and Debug.Log of Unity will no longer be displayed. The default value is true.
Write/Read Variable
The Socket is a System.Net.Sockets.UdpClient
variable. This is the main communication variable between Client and server.
Write/Read Variable
UdpServer.ShowUnityNetworkStatistics = true;
When using Nethostfire in Unity and when set the value of ShowUnityNetworkStatistics to true, statistics on server will be displayed in console running batchmode.
Write/Read Variable
if(UDP.Server.Status == ServerStatusConnection.Running){
// UDP.Server Running.
}
The Status is an enum UDP.Server.ServerStatusConnection with it you can know the current state of the server.
Event
static void Main(string[] args){
UDP.Server.OnConnectedClient += OnConnectedClient;
UDP.Server.Start(IPAddress.Any, 25000);
}
static void OnConnectedClient(DataClient _dataClient){
Console.WriteLine(_dataClient.IP + " new client conected!");
}
OnConnectedClient is an event that you can use to receive the DataClient whenever a client connected.
Event
static void Main(string[] args){
UDP.Server.OnDisconnectedClient += OnDisconnectedClient;
UDP.Server.Start(IPAddress.Any, 25000);
}
static void OnDisconnectedClient(DataClient _dataClient){
Console.WriteLine(_dataClient.IP + " new client conected!");
}
OnDisconnectedClient is an event that you can use to receive the DataClient whenever a client disconnected.
Event
static void Main(string[] args){
UDP.Server.ServerStatusConnection += OnServerStatus;
UDP.Server.Start(IPAddress.Any, 25000);
}
static void OnServerStatus(ServerStatusConnection _status){
Console.WriteLine("UDP.Server Status: " + _status);
}
OnServerStatus is an event that returns UDP.Server.ServerStatusConnection whenever the status changes, with which you can use it to know the current status of the server.
Event
static void Main(string[] args){
UDP.Server.OnReceivedBytes += OnReceivedBytesClient;
UDP.Server.Start(IPAddress.Any, 25000);
}
static void OnReceivedBytesClient(byte[] _byte, int _groupID, DataClient _dataClient){
Console.WriteLine("[RECEIVED] GroupID: {0} - Message: {1} | Length: {2}", _groupID, Encoding.ASCII.GetString(_byte), _byte.Length);
}
OnReceivedBytesClient an event that returns bytes received, GroupID and DataClient whenever the received bytes by clients, with it you can manipulate the bytes received.
Connect(IPEndPoint _host, int _symmetricSizeRSA = 86)
UDP.Client.Connect(IPAddress.Parse("127.0.0.1"), 25000, 20);
Connect to a server with IP, Port and sets the size of SymmetricSizeRSA if needed.
SendBytes(byte[] _byte, int _groupID, TypeShipping _typeShipping = TypeShipping.None, bool _holdConnection = false)
var _bytes = System.Text.Encoding.ASCII.GetBytes("Hello world!");
// Sending only groupID, without encryption and without HoldConnection
UDP.Client.SendBytes(null, 11);
// Sending bytes with groupID 4 to server, without encryption and without HoldConnection
UDP.Client.SendBytes(_byte, 4);
// Sending bytes with groupID 4 to server, with encryption AES and without HoldConnection
UDP.Client.SendBytes(_byte, 4, TypeShipping.AES);
// Sending bytes with groupID 4 to server, with encryption RSA and with HoldConnection
UDP.Client.SendBytes(_byte, 4, TypeShipping.RSA, true);
To send bytes to server, it is necessary to define the bytes and GroupID, the other sending resources such as TypeShipping and HoldConnection are optional.
UDP.Client.DisconnectServer()
UDP.Client.DisconnectServer();
With DisconnectServer the client will be disconnected from the server.
Read-Only Variable
static void OnReceivedBytesServer(byte[] _byte, int _groupID, DataClient _dataClient){
int LostPackets = UDP.Client.LostPackets;
Console.WriteLine("{0} packets lost", LostPackets);
}
LostPackets is the number of packets lost.
Read-Only Variable
static void OnReceivedBytesServer(byte[] _byte, int _groupID, DataClient _dataClient){
string packetsPerSeconds = UDP.Client.PacketsPerSeconds;
Console.WriteLine("{0} Packets Per Seconds", packetsPerSeconds);
}
PacketsPerSeconds is the amount of packets per second that happen when the client is sending and receiving.
Read-Only Variable
static void OnReceivedBytesServer(byte[] _byte, int _groupID, DataClient _dataClient){
string packetsBytesReceived = UDP.Client.PacketsBytesReceived;
Console.WriteLine("Received: {0}", packetsBytesReceived);
}
PacketsBytesReceived is the amount of bytes received by the client.
Read-Only Variable
static void OnReceivedBytesServer(byte[] _byte, int _groupID, DataClient _dataClient){
string packetsBytesSent = UDP.Client.PacketsBytesSent;
Console.WriteLine("Sent: {0}", packetsBytesSent);
}
PacketsBytesSent is the amount of bytes sent by the client.
Write/Read Variable
UDP.Client.ReceiveAndSendTimeOut = 2000;
ReceiveAndSendTimeOut defines the timeout in milliseconds for sending and receiving, if any packet exceeds that sending the client will ignore the receiving or sending. The default and recommended value is 1000.
Event
static void Main(string[] args){
UDP.Client.OnReceivedBytes += OnReceivedBytesServer;
UDP.Client.Connect(IPAddress.Parse("127.0.0.1"), 25000);
}
static void OnReceivedBytesServer(byte[] _byte, int _groupID){
Console.WriteLine("[RECEIVED] GroupID: {0} - Message: {1} | Length: {2}", _groupID, Encoding.ASCII.GetString(_byte), _byte.Length);
}
OnReceivedBytesServer an event that returns bytes received and GroupID whenever the received bytes by clients, with it you can manipulate the bytes received.
Event
static void Main(string[] args){
UDP.Client.ClientStatusConnection += OnClientStatus;
UDP.Server.Start(IPAddress.Any, 25000);
}
static void OnClientStatus(ClientStatusConnection _status){
Console.WriteLine("Client Status: " + _status);
}
OnClientStatus is an event that returns UDP.Client.ClientStatusConnection whenever the status changes, with which you can use it to know the current status of the server.
Read-Only Variable
static void OnReceivedBytesServer(byte[] _byte, int _groupID, DataClient _dataClient){
string ping = UDP.Client.Ping;
Console.WriteLine("PING: {0}ms", ping);
}
Ping returns an integer value, this value is per milliseconds
Read-Only Variable
static void OnClientStatus(ClientStatusConnection _status){
if(_status == ClientStatusConnection.Connected){
string publicKeyRSA = UDP.Client.PublicKeyRSA;
Console.WriteLine("Public Key RSA: {0}", publicKeyRSA);
}
}
PublicKeyRSA returns the RSA public key obtained by the server after connecting.
Read-Only Variable
static void OnClientStatus(ClientStatusConnection _status){
if(_status == ClientStatusConnection.Connected){
string privateKeyAES = UDP.Client.PrivateKeyAES;
Console.WriteLine("Private Key AES: {0}", privateKeyAES);
}
}
PrivateKeyAES returns the AES private key obtained by the server after connecting.
Write/Read Variable
if(UDP.Client.Status == ClientStatusConnection.Connected){
// Client Connected.
}
The Status is an enum UDP.Client.ClientStatusConnection with it you can know the current state of the client.
Write/Read Variable
UDP.Client.ConnectTimeOut = 15000; // 15s
ConnectTimeOut is the time the client will be reconnecting with the server, the time is defined in milliseconds, if the value is 0 the client will be reconnecting infinitely. The default value is 10000.
Write/Read Variable
UDP.Client.ShowUnityNetworkStatistics = true;
When using Nethostfire in Unity and when set the value of ShowUnityNetworkStatistics to true, statistics on the connection between the client and the server will be displayed during game execution.
SymmetricSizeRSA is the maximum size of bytes you can encrypt with RSA. Remember, the larger the Symmetric, the larger the RSA key, and the longer it will take to encrypt and decrypt. The default value is 86 bytes which represents 1024 bytes of RSA key.
Symmetric Size (bytes) | RSA Key Size (bytes) | Encryption/Decryption Speed (ms) | Security |
---|---|---|---|
16 | 464 | 0,00682 Very Fast | Little Safe |
22 | 512 | 0,00870 Fast | Moderate |
86 | 1024 | 0,01138 Moderate | Safe |
214 | 2048 | 0,01988 Slow | Very Safe |
470 | 4096 | 0,02692 Very Slow | Extremely safe |
The minimum value of symmetricSizeRSA is 16 and the maximum value is 470. Encryption/Decryprion speed may vary depending on your machine's performance
public class DataClient{
public IPEndPoint IP; // IP Address
public int PPS; // Packets per second
public int Ping; // Ping (ms)
public int Time; // Last time updated by the server.
public int TimeLastPacket; // Last time received packet.
public string PublicKeyRSA = null; // RSA key
public byte[] PrivateKeyAES = null; // Private AES key
}
The DataClient class is used to store data from a client on the server. It is with this class that the server uses to define a client. The DataClients can be obtained with the following server events UDP.Server.OnReceivedBytes, UDP.Client.OnReceivedBytes, UDP.Server.OnConnectedClient and UDP.Server.OnDisconnectedClient
public enum ServerStatusConnection{
Stopped = 0,
Stopping = 1,
Running = 2,
Initializing = 3,
Restarting = 4,
}
The ServerStatusConnection is used to define server states. The ServerStatusConnection can be obtained by the UDP.Server.Status variable or with the event UDP.Server.ServerStatusConnection
public enum TypeShipping{
None = 0,
AES = 1,
RSA = 2,
Base64 = 3,
Compress = 4,
OnlyBase64 = 5,
OnlyCompress = 6,
}
The TypeShipping is used to define the type of encryption of the bytes when being sent, Encryptions are automatically decrypted whenever it reaches its destination, to prevent it from being automatically decrypted when it arrives at the server, just select the option that begins with Skip.. Below is some information about using each of them.
TypeShipping | Encryption/Decryption Speed | Security | Shipping Size | Recommended Use |
---|---|---|---|---|
None | 37000pps Extremely Fast | Not Safe | Very Little | Argument/Command Line |
AES | 31000pps Fast | Moderate | Little | Coordinates/Actions of a player (game) |
RSA | 1000ps Extremely Slow | Extremely safe | Big | Login/Messages |
Base64 | 36000pps Very Fast | Not Safe | Little | Infos/Status simples |
Compress | 30000pps Fast | Not Safe | Extremely Little | Video/Voice Call |
OnlyBase64 | 36500pps Very Fast | Not Safe | Little | Infos/Status simples |
OnlyCompress | 33000pps Fast | Not Safe | Extremely Little | Video/Voice Call |
Encryption/Decryprion Speed may vary depending on your machine's performance.
• TypeHoldConnection.None - Value default. (No effect) • TypeHoldConnection.NotEnqueue - With NotEnqueue, shipments are sent to their destination without packet loss, shipments will not be sent in a queue to improve performance. • TypeHoldConnection.Enqueue - With Enqueue, bytes are sent to their destination without packet loss, shipments will be sent in a queue, this feature is not recommended to be used for high demand for shipments, each package can vary between 1ms and 1000ms.
public enum ClientStatusConnection{
Disconnected = 0, // Disconnected by the server
Disconnecting = 1,
Connected = 2,
Connecting = 3,
ConnectionFail = 4, // No connection to the server
IpBlocked = 5, // IP blocked by the server
MaxClientExceeded = 6, // The server has exceeded the limit of connected clients
}
The ClientStatusConnection is used to define client states. The ClientStatusConnection can be obtained by the UDP.Client.Status variable or with the event UDP.Client.ClientStatusConnection
GroupID is a way to organize your shipments with high performance, whenever you send bytes with the UDP.Server or Client the GroupID will be obtained in the following events: UDP.Server.OnReceivedBytes and UDP.Client.OnReceivedBytes.
After the message "Hello World!" was sent with UDP.Client.SendBytes, the bytes were sorted before being sent, for Nethostfire features to work.