using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using Mistox.Server; namespace Mistox { public class ServerInterface { public static MistoxServer ServerInstance; // Server Data public static ServerListener Listener; // Server Listener public static ConcurrentDictionary Connections; // Server Connections public event Action onUserConnected; // User connected event handler public event Action onDataReceived; // Data received event handler public event Action onUserDisconnected; // User disconnected event handler public ServerInterface( int port, string serverName ) { // Initilize the server ServerInstance = new MistoxServer() { ServerName = serverName, BindAddress = IPAddress.Any, Port = port, MaxClients = 10 }; Connections = new ConcurrentDictionary(); // Setup the event handlers ServerInstance.onUserConnected += (TcpClientConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); }; ServerInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); }; ServerInstance.onUserDisconnected += (TcpClientConnection disconnectedUser) => { onUserDisconnected?.Invoke(disconnectedUser); }; // Start listening for connections and wire up the server Listener = new ServerListener(); Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port ); } public async Task SendAllClients(Packet data) { foreach( KeyValuePair cur in Connections ) { await cur.Value.Connection.Send(data); } } public async Task SendClient(string UserName, Packet data) { await Connections[UserName].Connection.Send( data ); } } }