From a689f2656f1686a75f8eaaf03031b8da676bf4a1 Mon Sep 17 00:00:00 2001 From: derek holloway Date: Fri, 3 Jul 2026 11:37:41 -0700 Subject: [PATCH] Wire up the event handlers --- .../Client/Entities/MistoxServerClient.cs | 28 +++++++++++++++---- MistoxServer/Client/Entities/UDPEndpoint.cs | 1 - .../Client/Entities/UserConnectionPacket.cs | 8 ++++++ MistoxServer/Client/TCPClient.cs | 22 ++++----------- MistoxServer/Client/UDPClient.cs | 26 +++++++++-------- MistoxServer/Interface/ClientInterface.cs | 21 ++++++++------ MistoxServer/Interface/ServerInterface.cs | 18 ++++++------ .../{mExtensions.cs => Serializer.cs} | 2 +- MistoxServer/Server/Entities/MistoxServer.cs | 18 ++++-------- MistoxServer/Server/ServerListener.cs | 1 - MistoxServer/Server/TCPConnection.cs | 21 +++++++------- 11 files changed, 90 insertions(+), 76 deletions(-) create mode 100644 MistoxServer/Client/Entities/UserConnectionPacket.cs rename MistoxServer/{mExtensions.cs => Serializer.cs} (98%) diff --git a/MistoxServer/Client/Entities/MistoxServerClient.cs b/MistoxServer/Client/Entities/MistoxServerClient.cs index 86f69e0..b9931a2 100644 --- a/MistoxServer/Client/Entities/MistoxServerClient.cs +++ b/MistoxServer/Client/Entities/MistoxServerClient.cs @@ -3,21 +3,37 @@ namespace Mistox.Client { public class MistoxServerClient { + public string ServerName { get; set; } = ""; // The Name of the server public bool Alive { get; set; } = true; // Is the server running public int Port { get; set; } = 9126; // What Port to bind on - public event EventHandler onUserConnected; + + public event Action onServerConnected; + public void InvokeOnServerConnected(){ + Alive = true; + onServerConnected?.Invoke(); + } + + public event Action onUserConnected; public void InvokeOnUserConnected(UDPEndpoint ConnectedUser){ - onUserConnected?.Invoke(ConnectedUser, new EventArgs()); + onUserConnected?.Invoke(ConnectedUser); } - public event EventHandler onReceived; + + public event Action onDataReceived; public void InvokeOnReceived(dynamic DataPacket){ - onReceived?.Invoke(DataPacket, new EventArgs()); + onDataReceived?.Invoke(DataPacket); } - public event EventHandler onUserDisconnected; + + public event Action onUserDisconnected; public void InvokeOnUserDisconnected(UDPEndpoint DisconnectedUser){ - onUserDisconnected?.Invoke(DisconnectedUser, new EventArgs()); + onUserDisconnected?.Invoke(DisconnectedUser); + } + + public event Action onServerDisconnected; + public void InvokeOnServerDisconnected(){ + Alive = false; + onServerDisconnected?.Invoke(); } } diff --git a/MistoxServer/Client/Entities/UDPEndpoint.cs b/MistoxServer/Client/Entities/UDPEndpoint.cs index c5b003d..594b865 100644 --- a/MistoxServer/Client/Entities/UDPEndpoint.cs +++ b/MistoxServer/Client/Entities/UDPEndpoint.cs @@ -4,7 +4,6 @@ namespace Mistox.Client { public class UDPEndpoint { public string UserName { get; set; } = ""; // The Players UserName public bool Alive { get; set; } = true; // Is the connection alive - public IPEndPoint Destination { get; set; } // The IP destination for the connection public mUDPClient Connection { get; set; } // The UDP Client Object } } \ No newline at end of file diff --git a/MistoxServer/Client/Entities/UserConnectionPacket.cs b/MistoxServer/Client/Entities/UserConnectionPacket.cs new file mode 100644 index 0000000..c4c52f3 --- /dev/null +++ b/MistoxServer/Client/Entities/UserConnectionPacket.cs @@ -0,0 +1,8 @@ +using Mistox.Attributes; + +[mPacket(0xFF)] +public class UserConnectionPacket { + string UserName { get; set; } + long RemoteIP { get; set; } + int Port { get; set; } +} \ No newline at end of file diff --git a/MistoxServer/Client/TCPClient.cs b/MistoxServer/Client/TCPClient.cs index d926f85..951b5e0 100644 --- a/MistoxServer/Client/TCPClient.cs +++ b/MistoxServer/Client/TCPClient.cs @@ -15,7 +15,7 @@ namespace Mistox.Client { Server = new TcpClient( AddressFamily.InterNetwork ); Server.NoDelay = true; Server.Connect( ServerAddress ); - ClientInterface.ServerClientInstance.Alive = true; + ClientInterface.ServerClientInstance.InvokeOnServerConnected(); Thread RThread = new Thread(async () => { await ReceiveThread(); }); RThread.Start(); } @@ -24,27 +24,17 @@ namespace Mistox.Client { using( NetworkStream ns = Server.GetStream() ) { try { while( ClientInterface.ServerClientInstance.Alive ) { - if( Server.Connected ) { - Console.WriteLine( "Connected to server" ); - ClientInterface.ServerClientInstance.onConnected.Invoke( new object(), new EventArgs() ); - } else if( !Server.Connected ) { - Console.WriteLine( "Disconnected from server" ); - ClientInterface.ServerClientInstance.onDisconnected?.Invoke( new object(), new EventArgs() ); - ClientInterface.ServerClientInstance.Alive = false; - } byte[] StreamData = new byte[1024]; int bytesRead = await ns.ReadAsync(StreamData, 0, StreamData.Length); if( bytesRead > 0 ) { - dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) ); + dynamic data = Serializer.tReceive( StreamData.Sub( 0, bytesRead ) ); if( data != null ) { - ClientInterface.ServerClientInstance.onReceive?.Invoke( data, new EventArgs() ); + ClientInterface.ServerClientInstance.InvokeOnReceived( data ); } } } - } catch( Exception ) { - Console.WriteLine( "You have disconnected from the server" ); - ClientInterface.ServerClientInstance.onDisconnected?.Invoke( new object(), new EventArgs() ); - ClientInterface.ServerClientInstance.Alive = false; + } catch( Exception e ) { + Console.WriteLine( "TCP Receive exception : " + e.Message ); } } } @@ -55,7 +45,7 @@ namespace Mistox.Client { } public void Dispose() { - ClientInterface.ServerClientInstance.Alive = false; + ClientInterface.ServerClientInstance.InvokeOnServerDisconnected(); Server.Close(); Server = null; } diff --git a/MistoxServer/Client/UDPClient.cs b/MistoxServer/Client/UDPClient.cs index 2a02405..f5c8ac7 100644 --- a/MistoxServer/Client/UDPClient.cs +++ b/MistoxServer/Client/UDPClient.cs @@ -11,39 +11,41 @@ namespace Mistox.Client { Socket udpClient; - public mUDPClient( IPEndPoint ServerAddress, string ConnectionID ) { + IPEndPoint remoteHost; + + public mUDPClient( long RemoteIP, int Port ) { + remoteHost = new IPEndPoint( RemoteIP, Port ); udpClient = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp ); udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true ); udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 128 ); - udpClient.Bind( new IPEndPoint( IPAddress.Any, ServerAddress.Port ) ); - Alive = true; - Port = ServerAddress.Port; + udpClient.Bind( new IPEndPoint( IPAddress.Any, Port ) ); + ClientInterface.ServerClientInstance.Alive = true; Thread Client = new Thread(async() => { await ReceiveThread(); }); Client.Start(); } async Task ReceiveThread() { - while( Alive ) { + while( ClientInterface.ServerClientInstance.Alive ) { try { byte[] buffer = new byte[1024]; int bytesRead = await udpClient.ReceiveAsync( buffer ); - dynamic data = mSerialize.uReceive( buffer.Sub(0, bytesRead) ); + dynamic data = Serializer.uReceive( buffer.Sub(0, bytesRead) ); if( data != null ) { - onReceived?.Invoke( data, new EventArgs() ); + ClientInterface.ServerClientInstance.InvokeOnReceived( data ); } - } catch( Exception ) { - + } catch( Exception e ) { + Console.WriteLine( "UDP Receive exception : " + e.Message ); } } } - public async Task Send( T Data, IPEndPoint remoteHost ) { + public async Task Send( T Data ) { byte[] byteData = DataPacket.Serialize( Data ); - await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) ); + await udpClient.SendToAsync( byteData, SocketFlags.None, remoteHost ); } public void Dispose() { - Alive = false; + ClientInterface.ServerClientInstance.Alive = false; udpClient.Close(); udpClient = null; } diff --git a/MistoxServer/Interface/ClientInterface.cs b/MistoxServer/Interface/ClientInterface.cs index 6e43f36..6f10394 100644 --- a/MistoxServer/Interface/ClientInterface.cs +++ b/MistoxServer/Interface/ClientInterface.cs @@ -12,15 +12,23 @@ namespace Mistox { public static mTCPClient serverConnection; public static ConcurrentDictionary clientConnections; - public event EventHandler onUserConnected; - public event EventHandler onReceive; - public event EventHandler onUserDisconnected; + public event Action onServerConnected; + public event Action onUserConnected; + public event Action onDataReceived; + public event Action onUserDisconnected; + public event Action onServerDisconnected; public ClientInterface( string IpOrHostName, int Port ) { + // Wire up the Mistox server client ServerClientInstance = new MistoxServerClient() { Port = Port }; + ServerClientInstance.onServerConnected += onServerConnected; + ServerClientInstance.onUserConnected += (UDPEndpoint endpoint) => { onUserConnected?.Invoke(endpoint); }; + ServerClientInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); }; + ServerClientInstance.onUserDisconnected += (UDPEndpoint endpoint) => { onUserDisconnected?.Invoke(endpoint); }; + ServerClientInstance.onServerDisconnected += onServerDisconnected; // Get Server IP IPHostEntry host = Dns.GetHostEntry( IpOrHostName ); @@ -35,9 +43,6 @@ namespace Mistox { // Make a TCP connection to the server try { serverConnection = new mTCPClient( ServerDenstination ); - serverConnection.on += - serverConnection.onReceive += ( object o, EventArgs e ) => { onReceive?.Invoke( o, e ); }; - serverConnection.onDisconnected += ( object o, EventArgs e ) => { onUserDisconnected?.Invoke( o, e ); }; } catch( Exception e ) { Console.WriteLine( "An error has occured with the connection to the server. Error { " ); Console.WriteLine( e.ToString() ); @@ -47,12 +52,12 @@ namespace Mistox { public async Task SendAllClients(Packet data) { foreach(KeyValuePair cur in clientConnections){ - await cur.Value.Connection.Send( data, cur.Value.Destination ); + await cur.Value.Connection.Send( data ); } } public async Task SendClient(string UserName, Packet data) { - await clientConnections[UserName].Connection.Send( data, clientConnections[UserName].Destination ); + await clientConnections[UserName].Connection.Send( data ); } public async Task SendServer(Packet data) { diff --git a/MistoxServer/Interface/ServerInterface.cs b/MistoxServer/Interface/ServerInterface.cs index 17c18fe..f91fb39 100644 --- a/MistoxServer/Interface/ServerInterface.cs +++ b/MistoxServer/Interface/ServerInterface.cs @@ -8,13 +8,13 @@ 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 static MistoxServer ServerInstance; // Server Data + public static ServerListener Listener; // Server Listener + public static ConcurrentDictionary Connections; // Server Connections - public event EventHandler onUserConnected; - public event EventHandler onReceive; - public event EventHandler onUserDisconnected; + 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 ) { @@ -28,9 +28,9 @@ namespace Mistox { Connections = new ConcurrentDictionary(); // Setup the event handlers - ServerInstance.onUserConnected += onUserConnected; - ServerInstance.onReceived += onReceive; - ServerInstance.onUserDisconnected += onUserDisconnected; + 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(); diff --git a/MistoxServer/mExtensions.cs b/MistoxServer/Serializer.cs similarity index 98% rename from MistoxServer/mExtensions.cs rename to MistoxServer/Serializer.cs index b77266b..2330863 100644 --- a/MistoxServer/mExtensions.cs +++ b/MistoxServer/Serializer.cs @@ -17,7 +17,7 @@ namespace Mistox { } } - public class mSerialize { + public class Serializer { static byte[] TBufferedData = []; public static dynamic tReceive( byte [] BytesRead ) { diff --git a/MistoxServer/Server/Entities/MistoxServer.cs b/MistoxServer/Server/Entities/MistoxServer.cs index 812a5dc..f1df656 100644 --- a/MistoxServer/Server/Entities/MistoxServer.cs +++ b/MistoxServer/Server/Entities/MistoxServer.cs @@ -10,18 +10,12 @@ namespace Mistox.Server { public int MaxClients { get; set; } = 10; // The max players per server public bool Alive { get; set; } = true; // Is the server running - public event EventHandler onUserConnected; - public void InvokeOnUserConnected(TcpClientConnection ConnectedUser){ - onUserConnected?.Invoke(ConnectedUser, new EventArgs()); - } - public event EventHandler onReceived; - public void InvokeOnReceived(dynamic DataPacket){ - onReceived?.Invoke(DataPacket, new EventArgs()); - } - public event EventHandler onUserDisconnected; - public void InvokeOnUserDisconnected(TcpClientConnection DisconnectedUser){ - onUserDisconnected?.Invoke(DisconnectedUser, new EventArgs()); - } + public event Action onUserConnected; + public void InvokeUserConnected( TcpClientConnection user ){ onUserConnected?.Invoke( user ); } + public event Action onDataReceived; + public void InvokeDataReceived( dynamic data ){ onDataReceived?.Invoke( data ); } + public event Action onUserDisconnected; + public void InvokeUserDisconnected( TcpClientConnection user ){ onUserDisconnected?.Invoke( user ); } } } diff --git a/MistoxServer/Server/ServerListener.cs b/MistoxServer/Server/ServerListener.cs index b6655b5..8229097 100644 --- a/MistoxServer/Server/ServerListener.cs +++ b/MistoxServer/Server/ServerListener.cs @@ -35,7 +35,6 @@ namespace Mistox.Server { }; Console.WriteLine( "New User Connected" ); - ServerInterface.ServerInstance.InvokeOnUserConnected( ServerInterface.Connections[connectionID] ); } } diff --git a/MistoxServer/Server/TCPConnection.cs b/MistoxServer/Server/TCPConnection.cs index 763e283..7165df4 100644 --- a/MistoxServer/Server/TCPConnection.cs +++ b/MistoxServer/Server/TCPConnection.cs @@ -7,28 +7,28 @@ namespace Mistox.Server { public class TCPConnection : IDisposable { TcpClient _client; - string _connectionID; + TcpClientConnection _connection; public TCPConnection( TcpClient tcpClient, string ConnectionID ) { _client = tcpClient; - _connectionID = ConnectionID; _client.NoDelay = true; - + _connection = ServerInterface.Connections[ConnectionID]; + ServerInterface.ServerInstance.InvokeUserConnected( ServerInterface.Connections[ConnectionID] ); // Start the receive thread Thread receiveThread = new Thread(async () => await ReceiveThread() ); receiveThread.Start(); } public async Task ReceiveThread() { - while( _client.Connected && ServerInterface.Connections[_connectionID].Connected ) { + while( _client.Connected && _connection.Connected && ServerInterface.ServerInstance.Alive ) { try { - ServerInterface.Connections[_connectionID].LastHeartbeat = DateTime.UtcNow; + _connection.LastHeartbeat = DateTime.UtcNow; byte[] StreamData = new byte[1024]; int bytesRead = await _client.GetStream().ReadAsync(StreamData, 0, StreamData.Length); if( bytesRead > 0 ) { - dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) ); + dynamic data = Serializer.tReceive( StreamData.Sub( 0, bytesRead ) ); if( data != null ) { - ServerInterface.ServerInstance.InvokeOnReceived( data ); + ServerInterface.ServerInstance.InvokeDataReceived( data ); } } } catch( Exception ) { @@ -44,9 +44,10 @@ namespace Mistox.Server { } public void Dispose() { - ServerInterface.Connections[_connectionID].Connected = false; - ServerInterface.ServerInstance.InvokeOnUserDisconnected( ServerInterface.Connections[_connectionID] ); - ServerInterface.Connections.TryRemove( _connectionID, out _ ); + _connection.Connected = false; + ServerInterface.ServerInstance.InvokeUserDisconnected( _connection ); + ServerInterface.ServerInstance.Alive = false; + ServerInterface.Connections.TryRemove( _connection.UserName, out _ ); } } }