Skip to main content
Version: 3.1.0

TcpServer

Tcp server implementation

Namespace

using Netly;

Constructors

  • TcpServer TcpServer(bool framing)
    • framing "enable or disable Stream protocol message framing
      • true Netly will use its own message framing protocol
      • false You will receive raw stream data without framing middleware. For receive raw data use TcpServer.OnData(Action<TcpClient, byte[]> callback)

Properties

  • bool IsOpened
    Return true when the server socket is connected (bind port and listen clients) and false if not.

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

  • bool Framing
    Return true when the instance is using Netly MessageFraming protocol and false when the instance is not using MessageFraming protocol

  • bool IsEncrypted
    Return true when the instance is using TLS/SSL and false when isn't using this.

  • TcpClient[] Clients
    Return array of all connected client (TcpClient)

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 UseEncryption(byte[] pfxCertificate, string pfxPassword, SslProtocols encryptionProtocol)
    Used for enable TLS/SSL from server side.

  • 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<TcpClient> callback)
    This event is responsible for receive client when connected
    • callback is the "function" responsible for handling the received event.

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

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

  • void OnEvent(Action<TcpClient, string, byte[]> callback)
    Event responsible for receiving events (netly-events) from the client.
    • callback is the "function" responsible for handling the received event.
    • Wrapper
    TcpServer.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;


TcpServer server = new TcpServer(framing: true);

server.OnOpen(() =>
{
Console.WriteLine("Server connected!");
});

server.OnError((e) =>
{
// called when connection not opened
Console.WriteLine($"Connection error: {e}");
});

server.OnClose(() =>
{
Console.WriteLine("Server disconnected!");
});

server.OnEvent((client, name, data) =>
{
client.ToEvent(name, data);
Console.WriteLine($"Client event ({name}) -> {NE.GetString(data)}");
});

// open connection
const int backlog = 10;
server.Open(host, backlog);

// block main thread
bool running = false;
white(running)
{
Console.WriteLine("Netly ``TcpServer``\n\tq: Quit\n\tc: Close connection\n\tr: Reconnect");
string input = Console.ReadLine() ?? "";

switch(input)
{
case "q":
server.Close();
running = false;
break;
case "c":
server.Close();
break;
case "r":
server.Open(host, backlog);
break;
}
}

Console.WriteLine("Goodbye!!!");