Rework server to be non static

This commit is contained in:
derek.holloway committed 2026-07-08 13:13:23 -07:00
1 parent 875a5ae3e9
commit 6f910e969a
6 files changed
+97 -79

No files matched your search

+13 -22
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
@@ -8,44 +7,36 @@ using Mistox.Server;
namespace Mistox {
public class ServerInterface {
public static MistoxServer ServerInstance; // Server Data
public static ServerListener Listener; // Server Listener
public static ConcurrentDictionary<string, TcpClientConnection> Connections; // Server Connections
public static ServerEntity ServerInstance; // Server Data
public event Action<TcpClientConnection> onUserConnected; // User connected event handler
public event Action<dynamic> onDataReceived; // Data received event handler
public event Action<TcpClientConnection> onUserDisconnected; // User disconnected event handler
public event Action<ServerConnection> onUserConnected; // User connected event handler
public event Action<dynamic> onDataReceived; // Data received event handler
public event Action<ServerConnection> onUserDisconnected; // User disconnected event handler
public ServerInterface( int port, string serverName ) {
IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port );
// Initilize the server
ServerInstance = new MistoxServer() {
ServerName = serverName,
BindAddress = IPAddress.Any,
Port = port,
MaxClients = 10
};
Connections = new ConcurrentDictionary<string, TcpClientConnection>();
ServerInstance = new ServerEntity(listenerAddress);
ServerInstance.ServerName = serverName;
// Setup the event handlers
ServerInstance.onUserConnected += (TcpClientConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); };
ServerInstance.onUserConnected += (ServerConnection 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();
ServerInstance.onUserDisconnected += (ServerConnection disconnectedUser) => { onUserDisconnected?.Invoke(disconnectedUser); };
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port );
}
public async Task SendAllClients<Packet>(Packet data) {
foreach( KeyValuePair<string, TcpClientConnection> cur in Connections ) {
await cur.Value.Connection.Send(data);
foreach( KeyValuePair<string, ServerConnection> cur in ServerInstance.Connections ) {
await cur.Value.Send(data);
}
}
public async Task SendClient<Packet>(string UserName, Packet data) {
await Connections[UserName].Connection.Send( data );
await ServerInstance.Connections[UserName].Send( data );
}
}
}