Skip to main content
Version: 3.1.0

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 when useConnection is enabled

Properties

  • bool IsOpened
    Return true when connection be connected (when server listen and receive client buffer), and false when connection isn't connected.

  • Host Host
    Host class is endpoint container, and contain (ip address, port, ip type), it's endpoint metadata.

  • int Timeout
    It's timeout value and is used for determine connection timeout, timeout is milliseconds and only be used when UseConnection is enabled (true)

  • bool UseConnection
    It's determine if netly can use connection abstraction, if enabled (true) netly will send frequently ping message, and timeout for detect connection is alive.

  • UdpClient[] Clients
    Return array of all connected client (UdpClient).
    Warning: client will just disconnected when UseConnection is enabled (true).

Methods

Triggers

  • void Open(Host host)
    Open connection (bind and listen client), if connection not be open will call and expose exception on OnError callback.
    • Host Netly host instance (endpoint metadata)

  • void Close()
    Close connection (stop receive client buffer), if you need close connection use this method.

  • void ToData(byte[] buffer)
    void ToData(string buffer)
    Broadcast raw buffer to all connected clients from Clients array, buffer is string or byte[] (bytes).

  • void ToEvent(string name, byte[] buffer)
    void ToEvent(string name, string buffer)
    Broadcast event (netly-event) to all connected clients from Clients array, name is event identifier, buffer is event buffer (data), buffer is string or byte[] (bytes), if send buffer as string, netly will use NE.Default as encoding protocol.

Callbacks

  • void OnOpen(Action callback)
    Event responsible for receiving the connection opening information successfully
    • callback is the "function" responsible for handling the received event.

  • void OnError(Action<Exception> callback)
    Event responsible for receiving an error when opening a connection, the Exception contains the error information.
    • callback is the "function" responsible for handling the received event.

  • void OnClose(Action callback)
    Called when server close connection
    • callback is the "function" responsible for handling the received event.

  • void OnEnter(Action<UdpClient> callback)
    This event is responsible for receive client when connected
    • callback is the "function" responsible for handling the received event.

  • void OnExit(Action<UdpClient> callback)
    This event is responsible for detect when a client be disconnected
    • callback is the "function" responsible for handling the received event.
    • Wrapper
      UdpServer.OnEnter((client) =>
      {
      client.OnClose(() =>
      {
      Console.WriteLine(client.UUID);
      });
      });

  • void OnData(Action<UdpClient, byte[]> callback)
    Event responsible for receiving raw data (buffer) from the client
    • callback is the "function" responsible for handling the received event.
    • Wrapper
    UdpServer.OnEnter((client) =>
    {
    client.OnData((buffer) =>
    {
    Console.WriteLine(client.UUID);
    });
    });

  • void OnEvent(Action<UdpClient, string, byte[]> callback)
    Event responsible for receiving events (netly-events) from the client.
    • callback is the "function" responsible for handling the received event.
    • Wrapper
    UdpServer.OnEnter((client) =>
    {
    client.OnEvent((name, buffer) =>
    {
    Console.WriteLine(client.UUID);
    });
    });

  • void OnModify(Action<Socket> socket)
    This event is responsible for executing modifications in Socket, this event is executed in the connection creation step, and you will have access to Socket 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();