UdpServer
Udp server implementation
Namespace
using Netly;
Constructors
-
UdpServer UdpServer(bool useConnection ,int? timeout )useConnection
"enable or disable udp connection (using ping)timeout
is connection timeout (only whenuseConnection
is enabled
Properties
-
Returnbool IsOpenedtrue
when connection be connected (when server listen and receive client buffer), andfalse
when connection isn't connected.
-
Host HostHost class
is endpoint container, and contain (ip address, port, ip type), it's endpoint metadata.
-
It's timeout value and is used for determine connection timeout, timeout isint Timeoutmilliseconds
and only be used whenUseConnection
is enabled (true
)
-
It's determine if netly can use connection abstraction, if enabled (bool UseConnectiontrue
) netly will send frequently ping message, andtimeout
for detect connection is alive.
-
Return array of all connected client (UdpClient[] ClientsUdpClient
).Warning
: client will just disconnected whenUseConnection
is enabled (true
).
Methods
Triggers
-
Open connection (bind and listen client), if connection not be open will call and expose exception onvoid Open(Host host )OnError
callback.-
Host
Netly host instance (endpoint metadata)
-
-
Close connection (stop receive client buffer), if you need close connection use this method.void Close()
-
Broadcast raw buffer to all connected clients fromvoid ToData(byte[] buffer )
void ToData(string buffer )Clients array,buffer
isstring
orbyte[]
(bytes).
-
Broadcast event (netly-event) to all connected clients fromvoid ToEvent(string name ,byte[] buffer )
void ToEvent(string name ,string buffer )Clients array,name
is event identifier,buffer
is event buffer (data), buffer isstring
orbyte[]
(bytes), if send buffer as string, netly will useNE.Default
as encoding protocol.
Callbacks
-
Event responsible for receiving the connection opening information successfullyvoid OnOpen(Action callback )-
callback
is the "function" responsible for handling the received event.
-
-
Event responsible for receiving an error when opening a connection, thevoid OnError(Action<Exception> callback )Exception
contains the error information.-
callback
is the "function" responsible for handling the received event.
-
-
Called when server close connectionvoid OnClose(Action callback )-
callback
is the "function" responsible for handling the received event.
-
-
This event is responsible for receive client when connectedvoid OnEnter(Action<UdpClient> callback )-
callback
is the "function" responsible for handling the received event.
-
-
This event is responsible for detect when a client be disconnectedvoid OnExit(Action<UdpClient> callback )-
callback
is the "function" responsible for handling the received event. -
Wrapper UdpServer.OnEnter((client) =>
{
client.OnClose(() =>
{
Console.WriteLine(client.UUID);
});
});
-
-
Event responsible for receiving raw data (buffer) from the clientvoid OnData(Action<UdpClient, byte[]> callback )-
callback
is the "function" responsible for handling the received event. -
Wrapper
UdpServer.OnEnter((client) =>
{
client.OnData((buffer) =>
{
Console.WriteLine(client.UUID);
});
}); -
-
Event responsible for receiving events (netly-events) from the client.void OnEvent(Action<UdpClient, string, byte[]> callback )-
callback
is the "function" responsible for handling the received event. -
Wrapper
UdpServer.OnEnter((client) =>
{
client.OnEvent((name, buffer) =>
{
Console.WriteLine(client.UUID);
});
}); -
-
This event is responsible for executing modifications invoid OnModify(Action<Socket> socket )Socket
, this event is executed in the connection creation step, and you will have access toSocket
that will be used internally-
callback
is the "function" responsible for handling the received event.
-
Example
using System;
using Netly;
using Netly.Core;
const string ip = "127.0.0.1";
const int port = 3000;
const int myTimeout = 10000; // 10s
Host host = new Host(ip, port);
UdpServer server = new UdpServer(useConnection: true, timeout: myTimeout);
server.OnOpen(() =>
{
// connection opened.
Console.WriteLine("Connection opened.");
});
server.OnError((exception) =>
{
// connection opened.
Console.WriteLine($"Connection error: {exception}.");
});
server.OnClose(() =>
{
// connection closed.
Console.WriteLine("Connection closed.");
});
server.OnEnter((client) =>
{
// client connected.
Console.WriteLine($"Client {client.UUID} connected, host: {client.Host}");
// example. UdpServer.OnClose Wrapper
client.OnClose(() =>
{
Console.WriteLine($"Client {client.UUID} diconneted, host: {client.Host}");
};
// example. Event counter (Easy behaviour replication, because unique scope);
// this example is complex for doing using UdpServer.OnEvent because ``counterValue`` scope
// if you need replicate on UdpServer.OnEvent, you can use key/value class or similar, key is
// client.UUID and value is counter, and you can do loop on all key for increment or get counter.
int counterValue = 0;
client.OnEvent((name, buffer) =>
{
counterValue ++;
client.ToEvent("event counter", counterValue.ToString());
});
});
server.OnExit((client) =>
{
// client disconneted.
Console.WriteLine($"Client {client.UUID} diconneted, host: {client.Host}");
});
server.OnData((client, buffer) =>
{
// server receive raw data from client.
Console.WriteLine($"Client raw data: {client.Host} -> {NE.GetString(buffer)}");
// echo
client.ToData(buffer);
});
server.OnEvent((client, name, buffer) =>
{
Console.WriteLine($"Client event ({name}): {client.Host} -> {NE.GetString(buffer)}");
// echo
client.ToEvent(name, buffer);
// detect close message
if (name == "goodbye")
{
Console.WriteLine($"connection {client.Host}, talk goodbye.");
client.Close();
}
});
// Open connection
server.Open(host);
// Block main thread for not end program (console application)
Console.ReadLine();