TcpServer
Tcp server implementation
Namespace
using Netly;
Constructors
-
TcpServer TcpServer(bool framing )framing"enable or disableStream protocol message framingtrueNetly will use its own message framing protocolfalseYou will receive raw stream data without framing middleware. For receive raw data useTcpServer.OnData(Action<TcpClient, byte[]> callback)
Properties
-
Returnbool IsOpenedtruewhen the server socket is connected (bind port and listen clients) andfalseif not.
-
Host HostHost classis endpoint container, and contain (ip address, port, ip type), it's endpoint metadata.
-
Returnbool Framingtruewhen the instance is usingNetly MessageFraming protocolandfalsewhen the instance is not usingMessageFraming protocol
-
Returnbool IsEncryptedtruewhen the instance is usingTLS/SSLandfalsewhen isn't using this.
-
Return array of all connected client (TcpClient[] ClientsTcpClient)
Methods
Triggers
-
Open connection (bind and listen client), if connection not be open will call and expose exception onvoid Open(Host host )OnErrorcallback.-
HostNetly host instance (endpoint metadata)
-
-
Close connection (stop receive client buffer), if you need close connection use this method.void Close()
-
Used for enable TLS/SSL from server side.void UseEncryption(byte[] pfxCertificate ,string pfxPassword ,SslProtocols encryptionProtocol )
-
Broadcast raw buffer to all connected clients fromvoid ToData(byte[] buffer )
void ToData(string buffer )Clients array,bufferisstringorbyte[](bytes).
-
Broadcast event (netly-event) to all connected clients fromvoid ToEvent(string name ,byte[] buffer )
void ToEvent(string name ,string buffer )Clients array,nameis event identifier,bufferis event buffer (data), buffer isstringorbyte[](bytes), if send buffer as string, netly will useNE.Defaultas encoding protocol.
Callbacks
-
Event responsible for receiving the connection opening information successfullyvoid OnOpen(Action callback )-
callbackis the "function" responsible for handling the received event.
-
-
Event responsible for receiving an error when opening a connection, thevoid OnError(Action<Exception> callback )Exceptioncontains the error information.-
callbackis the "function" responsible for handling the received event.
-
-
Called when server close connectionvoid OnClose(Action callback )-
callbackis the "function" responsible for handling the received event.
-
-
This event is responsible for receive client when connectedvoid OnEnter(Action<TcpClient> callback )-
callbackis the "function" responsible for handling the received event.
-
-
This event is responsible for detect when a client be disconnectedvoid OnExit(Action<TcpClient> callback )-
callbackis the "function" responsible for handling the received event. -
Wrapper TcpServer.OnEnter((client) =>
{
client.OnClose(() =>
{
Console.WriteLine(client.UUID);
});
});
-
-
Event responsible for receiving raw data (buffer) from the clientvoid OnData(Action<TcpClient, byte[]> callback )-
callbackis the "function" responsible for handling the received event. -
Wrapper
TcpServer.OnEnter((client) =>
{
client.OnData((buffer) =>
{
Console.WriteLine(client.UUID);
});
}); -
-
Event responsible for receiving events (netly-events) from the client.void OnEvent(Action<TcpClient, string, byte[]> callback )-
callbackis the "function" responsible for handling the received event. -
Wrapper
TcpServer.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 toSocketthat will be used internally-
callbackis 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!!!");