From b439805f3ed42b629d879d68dd98f305fbab8ccd Mon Sep 17 00:00:00 2001 From: "derek.holloway" Date: Wed, 8 Jul 2026 13:45:20 -0700 Subject: [PATCH] Rework client to be non static --- .../Client/Entities/MistoxServerClient.cs | 40 -------------- MistoxServer/Client/Entities/UDPEndpoint.cs | 7 --- MistoxServer/Client/MistoxClient.cs | 55 +++++++++++++++++++ MistoxServer/Client/TCPClient.cs | 29 +++++----- MistoxServer/Client/UDPClient.cs | 37 +++++++------ MistoxServer/Interface/ClientInterface.cs | 45 +++++---------- MistoxServer/Server/ServerConnection.cs | 2 +- 7 files changed, 107 insertions(+), 108 deletions(-) delete mode 100644 MistoxServer/Client/Entities/MistoxServerClient.cs delete mode 100644 MistoxServer/Client/Entities/UDPEndpoint.cs create mode 100644 MistoxServer/Client/MistoxClient.cs diff --git a/MistoxServer/Client/Entities/MistoxServerClient.cs b/MistoxServer/Client/Entities/MistoxServerClient.cs deleted file mode 100644 index b9931a2..0000000 --- a/MistoxServer/Client/Entities/MistoxServerClient.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; - -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 Action onServerConnected; - public void InvokeOnServerConnected(){ - Alive = true; - onServerConnected?.Invoke(); - } - - public event Action onUserConnected; - public void InvokeOnUserConnected(UDPEndpoint ConnectedUser){ - onUserConnected?.Invoke(ConnectedUser); - } - - public event Action onDataReceived; - public void InvokeOnReceived(dynamic DataPacket){ - onDataReceived?.Invoke(DataPacket); - } - - public event Action onUserDisconnected; - public void InvokeOnUserDisconnected(UDPEndpoint DisconnectedUser){ - 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 deleted file mode 100644 index c77bbd7..0000000 --- a/MistoxServer/Client/Entities/UDPEndpoint.cs +++ /dev/null @@ -1,7 +0,0 @@ -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 mUDPClient Connection { get; set; } // The UDP Client Object - } -} \ No newline at end of file diff --git a/MistoxServer/Client/MistoxClient.cs b/MistoxServer/Client/MistoxClient.cs new file mode 100644 index 0000000..eae8b50 --- /dev/null +++ b/MistoxServer/Client/MistoxClient.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Concurrent; +using System.Net; + +namespace Mistox.Client { + + public class MistoxClient { + + public string ServerName { get; set; } = ""; // The Name of the server + public bool Alive { get; set; } = true; // Is the server running + public mTCPClient ServerConnection; + public ConcurrentDictionary clientConnections; + + public MistoxClient(IPEndPoint Server) { + Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address ); + try { + ServerConnection = new mTCPClient( this, Server ); + Console.WriteLine( "Connected to the server sucessfully" ); + } catch( Exception e ) { + Console.WriteLine( "An error has occured with the connection to the server. Error { " ); + Console.WriteLine( e.ToString() ); + Console.WriteLine( "}" ); + } + Alive = true; + } + + public event Action onServerConnected; + public void InvokeOnServerConnected(){ + Alive = true; + onServerConnected?.Invoke(); + } + + public event Action onUserConnected; + public void InvokeOnUserConnected(mUDPClient ConnectedUser){ + onUserConnected?.Invoke(ConnectedUser); + } + + public event Action onDataReceived; + public void InvokeOnReceived(dynamic DataPacket){ + onDataReceived?.Invoke(DataPacket); + } + + public event Action onUserDisconnected; + public void InvokeOnUserDisconnected(mUDPClient DisconnectedUser){ + onUserDisconnected?.Invoke(DisconnectedUser); + } + + public event Action onServerDisconnected; + public void InvokeOnServerDisconnected(){ + Alive = false; + onServerDisconnected?.Invoke(); + } + } + +} diff --git a/MistoxServer/Client/TCPClient.cs b/MistoxServer/Client/TCPClient.cs index 264732e..fa449a8 100644 --- a/MistoxServer/Client/TCPClient.cs +++ b/MistoxServer/Client/TCPClient.cs @@ -9,23 +9,24 @@ using System.Threading.Tasks; namespace Mistox.Client { public class mTCPClient : IDisposable { - TcpClient Server; + TcpClient _connection; + MistoxClient _parent; - public mTCPClient( IPEndPoint ServerAddress ) { - Server = new TcpClient( AddressFamily.InterNetwork ); - Server.NoDelay = true; - Server.Connect( ServerAddress ); - ClientInterface.ServerClientInstance.InvokeOnServerConnected(); + public mTCPClient( MistoxClient parent, IPEndPoint ServerAddress ) { + _connection = new TcpClient( AddressFamily.InterNetwork ); + _connection.NoDelay = true; + _connection.Connect( ServerAddress ); + _parent.InvokeOnServerConnected(); Thread RThread = new Thread(async () => { await ReceiveThread(); }); RThread.Start(); } async Task ReceiveThread() { - using( NetworkStream ns = Server.GetStream() ) { + using( NetworkStream ns = _connection.GetStream() ) { try { byte[] headerBuffer = new byte[DataPacket.HeaderLength]; byte[] payloadBuffer = new byte[65535]; // Upper limit for safety - while( ClientInterface.ServerClientInstance.Alive ) { + while( _parent.Alive ) { // Read the header off await ns.ReadExactlyAsync(headerBuffer, 0, 3); @@ -46,11 +47,11 @@ namespace Mistox.Client { dynamic dataobject = DataPacket.Deserialize( packetbyte, dataLength, payloadBuffer.AsMemory(DataPacket.HeaderLength, dataLength) ); // Return the object - ClientInterface.ServerClientInstance.InvokeOnReceived(dataobject); + _parent.InvokeOnReceived(dataobject); } } catch (Exception e) { - ClientInterface.ServerClientInstance.Alive = false; + _parent.Alive = false; Dispose(); Console.WriteLine("TCP Receive exception: " + e.Message); } @@ -59,13 +60,13 @@ namespace Mistox.Client { public async Task Send(T packet) { byte[] data = DataPacket.Serialize( packet ); - await Server.GetStream().WriteAsync( data, 0, data.Length ); + await _connection.GetStream().WriteAsync( data, 0, data.Length ); } public void Dispose() { - ClientInterface.ServerClientInstance.InvokeOnServerDisconnected(); - Server.Close(); - Server = null; + _parent.InvokeOnServerDisconnected(); + _connection.Close(); + _connection = null; } } } diff --git a/MistoxServer/Client/UDPClient.cs b/MistoxServer/Client/UDPClient.cs index ab3179b..975b79c 100644 --- a/MistoxServer/Client/UDPClient.cs +++ b/MistoxServer/Client/UDPClient.cs @@ -9,28 +9,33 @@ using System.Threading.Tasks; namespace Mistox.Client { public class mUDPClient : IDisposable { - Socket udpClient; - IPEndPoint remoteHost; + public string UserName { get; set; } = ""; // The remote players UserName + public bool Alive { get; set; } = true; // Is the connection alive - 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, Port ) ); - ClientInterface.ServerClientInstance.Alive = true; + Socket _udpClient; + IPEndPoint _remoteHost; + MistoxClient _parent; + + public mUDPClient( MistoxClient parent, long RemoteIP, int Port ) { + _parent = parent; + _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, Port ) ); + _parent.Alive = true; Thread Client = new Thread(async() => { await ReceiveThread(); }); Client.Start(); } async Task ReceiveThread() { byte[] buffer = new byte[65535]; - while( ClientInterface.ServerClientInstance.Alive ) { + while( _parent.Alive ) { try { // Read the data out of the UDP packet with a buffer that has the maximum possible UDP packet size // As UDP reads the whole next packet then drops. Im guaranteed to receive 1 packet at a time - int bytesRead = await udpClient.ReceiveAsync( buffer ); + int bytesRead = await _udpClient.ReceiveAsync( buffer ); // Create the data readers ReadOnlyMemory dataMemory = buffer.AsMemory(0, bytesRead); @@ -45,7 +50,7 @@ namespace Mistox.Client { dynamic buildObj = DataPacket.Deserialize( dataType, DataLength, data ); // Return the object - ClientInterface.ServerClientInstance.InvokeOnReceived( buildObj ); + _parent.InvokeOnReceived( buildObj ); } catch( Exception e ) { Console.WriteLine( "UDP Receive exception : " + e.Message ); @@ -55,13 +60,13 @@ namespace Mistox.Client { public async Task Send( T Data ) { byte[] byteData = DataPacket.Serialize( Data ); - await udpClient.SendToAsync( byteData, SocketFlags.None, remoteHost ); + await _udpClient.SendToAsync( byteData, SocketFlags.None, _remoteHost ); } public void Dispose() { - ClientInterface.ServerClientInstance.Alive = false; - udpClient.Close(); - udpClient = null; + _parent.Alive = false; + _udpClient.Close(); + _udpClient = null; } } } \ No newline at end of file diff --git a/MistoxServer/Interface/ClientInterface.cs b/MistoxServer/Interface/ClientInterface.cs index 6f10394..c5f72e7 100644 --- a/MistoxServer/Interface/ClientInterface.cs +++ b/MistoxServer/Interface/ClientInterface.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; @@ -8,28 +7,16 @@ using Mistox.Client; namespace Mistox { public class ClientInterface { - public static MistoxServerClient ServerClientInstance; - public static mTCPClient serverConnection; - public static ConcurrentDictionary clientConnections; + public static MistoxClient serverInstance; public event Action onServerConnected; - public event Action onUserConnected; + public event Action onUserConnected; public event Action onDataReceived; - public event Action onUserDisconnected; + 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 ); IPEndPoint ServerDenstination = new IPEndPoint(0, 0); @@ -38,30 +25,28 @@ namespace Mistox { ServerDenstination = new IPEndPoint( entry, Port ); } } - Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + ServerDenstination.Address ); - - // Make a TCP connection to the server - try { - serverConnection = new mTCPClient( ServerDenstination ); - } catch( Exception e ) { - Console.WriteLine( "An error has occured with the connection to the server. Error { " ); - Console.WriteLine( e.ToString() ); - Console.WriteLine( "}" ); - } + + // Start and wire up the Mistox server client + serverInstance = new MistoxClient( ServerDenstination ); + serverInstance.onServerConnected += onServerConnected; + serverInstance.onUserConnected += (mUDPClient endpoint) => { onUserConnected?.Invoke(endpoint); }; + serverInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); }; + serverInstance.onUserDisconnected += (mUDPClient endpoint) => { onUserDisconnected?.Invoke(endpoint); }; + serverInstance.onServerDisconnected += onServerDisconnected; } public async Task SendAllClients(Packet data) { - foreach(KeyValuePair cur in clientConnections){ - await cur.Value.Connection.Send( data ); + foreach(KeyValuePair cur in serverInstance.clientConnections){ + await cur.Value.Send( data ); } } public async Task SendClient(string UserName, Packet data) { - await clientConnections[UserName].Connection.Send( data ); + await serverInstance.clientConnections[UserName].Send( data ); } public async Task SendServer(Packet data) { - await serverConnection.Send( data ); + await serverInstance.ServerConnection.Send( data ); } } } diff --git a/MistoxServer/Server/ServerConnection.cs b/MistoxServer/Server/ServerConnection.cs index ff74abb..8dd8ae3 100644 --- a/MistoxServer/Server/ServerConnection.cs +++ b/MistoxServer/Server/ServerConnection.cs @@ -69,7 +69,7 @@ namespace Mistox.Server { } } catch (Exception e) { - ClientInterface.ServerClientInstance.Alive = false; + _parent.Alive = false; Dispose(); Console.WriteLine("TCP Receive exception: " + e.Message); }