From 46efb5bd652b5b1a288657df0a4e2b5a1b7f70f6 Mon Sep 17 00:00:00 2001 From: "derek.holloway" Date: Mon, 3 Aug 2026 13:25:28 -0700 Subject: [PATCH] Cleanup unnessisary interface classes --- MistoxServer/Interface/ClientInterface.cs | 56 ----------------------- MistoxServer/Interface/Interface.cs | 41 +++++++++++++---- MistoxServer/Interface/ServerInterface.cs | 42 ----------------- MistoxServer/Server/MistoxServer.cs | 19 ++++++-- MistoxServer/Server/ServerConnection.cs | 6 +-- MistoxServer/Server/ServerListener.cs | 4 +- MistoxServer/mPacket/DataPacket.cs | 5 +- Todo.txt | 2 + 8 files changed, 58 insertions(+), 117 deletions(-) delete mode 100644 MistoxServer/Interface/ClientInterface.cs delete mode 100644 MistoxServer/Interface/ServerInterface.cs diff --git a/MistoxServer/Interface/ClientInterface.cs b/MistoxServer/Interface/ClientInterface.cs deleted file mode 100644 index 3a560bd..0000000 --- a/MistoxServer/Interface/ClientInterface.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Net; -using System.Threading.Tasks; -using Mistox.Client; - -namespace Mistox { - public class ClientInterface { - - public static MistoxClient serverInstance; - - public event Action onServerConnected; - public event Action onUserConnected; - public event Action onDataReceived; - public event Action onUserDisconnected; - public event Action onServerDisconnected; - - public ClientInterface( string UserName, string ConnectionKey, string IpOrHostName, int Port ) { - - // Get Server IP - IPHostEntry host = Dns.GetHostEntry( IpOrHostName ); - IPEndPoint ServerDenstination = new IPEndPoint(0, 0); - foreach( IPAddress entry in host.AddressList ) { - if ( entry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) { - ServerDenstination = new IPEndPoint( entry, Port ); - } - } - - // Start and wire up the Mistox server client - serverInstance = new MistoxClient( UserName, ConnectionKey, 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( mUDPClient cur in serverInstance.clientConnections.Values ){ - await cur.Send( data ); - } - } - - public async Task SendClient(string UserName, Packet data) { - foreach( mUDPClient cur in serverInstance.clientConnections.Values ) { - if (cur.UserName.ToLower() == UserName.ToLower()) { - await cur.Send( data ); - break; - } - } - } - - public async Task SendServer(Packet data) { - await serverInstance.ServerConnection.Send( data ); - } - } -} diff --git a/MistoxServer/Interface/Interface.cs b/MistoxServer/Interface/Interface.cs index 8ea34f5..11ed0bd 100644 --- a/MistoxServer/Interface/Interface.cs +++ b/MistoxServer/Interface/Interface.cs @@ -1,17 +1,30 @@ using System; using System.Net; using Mistox.Attributes; +using Mistox.Client; +using Mistox.Server; namespace Mistox { - public class mServer { + public class GameServer { - public static ServerInterface newServer( string serverName, int port ) { + public static MistoxServer newServer( string serverName, int port ) { + + // Build the network packet list NetworkPacketDictionary.BuildNetworkPacketList(); - return new ServerInterface(port, serverName); + + // Set the listener address + IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port ); + + // Return the initlized server instance + return new MistoxServer(listenerAddress, serverName); } - public static ClientInterface newClient( string UserName, string ServerIPOrHostName, int Port, string ConnectionKey = "" ) { + public static MistoxClient newClient( string UserName, string ServerIPOrHostName, int Port, string ConnectionKey = "" ) { + + // Build the network packet list NetworkPacketDictionary.BuildNetworkPacketList(); + + // Get the remote IP from the DNSName int index = 0; IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName); for( int i=0; i< remoteAddress.Length; i++ ) { @@ -20,13 +33,25 @@ namespace Mistox { } } string ipAddress = remoteAddress[index].ToString(); - if ( remoteAddress.Length > 0) { - return new ClientInterface(UserName, ConnectionKey, ipAddress, Port); - } else { + + // If a remote address exists + if ( remoteAddress.Length == 0) { Console.WriteLine("The server at " + ServerIPOrHostName + " doesn't exit or cannot be found"); return null; } - } + + // Resolve to an IPEndPoint + IPHostEntry host = Dns.GetHostEntry( ipAddress ); + IPEndPoint ServerDenstination = new IPEndPoint(0, 0); + foreach( IPAddress entry in host.AddressList ) { + if ( entry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) { + ServerDenstination = new IPEndPoint( entry, Port ); + } + } + + // Return the initlized client object + return new MistoxClient( UserName, ConnectionKey, ServerDenstination ); + } } diff --git a/MistoxServer/Interface/ServerInterface.cs b/MistoxServer/Interface/ServerInterface.cs deleted file mode 100644 index c79ae62..0000000 --- a/MistoxServer/Interface/ServerInterface.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Threading.Tasks; -using Mistox.Server; - -namespace Mistox { - public class ServerInterface { - - public static ServerEntity ServerInstance; // Server Data - - 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 ) { - - IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port ); - - // Initilize the server - ServerInstance = new ServerEntity(listenerAddress); - ServerInstance.ServerName = serverName; - - // Setup the event handlers - ServerInstance.onUserConnected += (ServerConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); }; - ServerInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); }; - 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 data) { - foreach( KeyValuePair cur in ServerInstance.Connections ) { - await cur.Value.Send(data); - } - } - - public async Task SendClient(string UserName, Packet data) { - await ServerInstance.Connections[UserName].Send( data ); - } - } -} diff --git a/MistoxServer/Server/MistoxServer.cs b/MistoxServer/Server/MistoxServer.cs index d971b5b..3ae018e 100644 --- a/MistoxServer/Server/MistoxServer.cs +++ b/MistoxServer/Server/MistoxServer.cs @@ -1,14 +1,15 @@ using System; using System.Collections.Concurrent; using System.Net; +using System.Threading.Tasks; namespace Mistox.Server { - public class ServerEntity { + public class MistoxServer { // Class Definitions - public string ServerName { get; set; } = ""; // The name of the server + public string ServerName { get; private set; } = ""; // The name of the server public int MaxClients { get; set; } = 10; // The max players per server public float ConnectionRange { get; set; } = 40; // InGame distance for valid conections public int ConnectionTimeoutSeconds { get; set; } = 10; // If no reply in time kill the connection @@ -17,9 +18,11 @@ namespace Mistox.Server { private readonly ServerListener Listener; // The Connection Listener singleton - public ServerEntity(IPEndPoint ListenerAddress) { + public MistoxServer(IPEndPoint ListenerAddress, string serverName) { Listener = new ServerListener(ListenerAddress, this); Connections = new ConcurrentDictionary(); + ServerName = serverName; + Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port ); } // Functions @@ -38,6 +41,16 @@ namespace Mistox.Server { public void InvokeUserDisconnected( ServerConnection user ){ onUserDisconnected?.Invoke( user ); } + + public async Task SendAllClients(Packet data) { + foreach( ServerConnection cur in Connections.Values ) { + await cur.Send(data); + } + } + + public async Task SendClient(string UserName, Packet data) { + await Connections[UserName].Send( data ); + } } } diff --git a/MistoxServer/Server/ServerConnection.cs b/MistoxServer/Server/ServerConnection.cs index 7775be9..1d2c380 100644 --- a/MistoxServer/Server/ServerConnection.cs +++ b/MistoxServer/Server/ServerConnection.cs @@ -13,20 +13,20 @@ namespace Mistox.Server { public Vector3 Position { get; set; } // The ingame position of the player public bool Connected { get; set; } = true; // Is the connection valid - private readonly ServerEntity _parent; + private readonly MistoxServer _parent; private readonly TcpClient _client; private readonly IPEndPoint _remoteAddress; private readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1); private readonly CancellationTokenSource _cts = new(); - public ServerConnection( TcpClient tcpClient, ServerEntity parent, IPEndPoint remote ) { + public ServerConnection( TcpClient tcpClient, MistoxServer parent, IPEndPoint remote ) { _parent = parent; _remoteAddress = remote; _client = tcpClient; _client.NoDelay = true; _client.ReceiveTimeout = _parent.ConnectionTimeoutSeconds * 1000; - ServerInterface.ServerInstance.InvokeUserConnected( this ); + _parent.InvokeUserConnected( this ); // Start the receive thread Task.Run(ReceiveThread); } diff --git a/MistoxServer/Server/ServerListener.cs b/MistoxServer/Server/ServerListener.cs index a165c54..0327466 100644 --- a/MistoxServer/Server/ServerListener.cs +++ b/MistoxServer/Server/ServerListener.cs @@ -8,11 +8,11 @@ using System.Threading.Tasks; namespace Mistox.Server { public class ServerListener : IDisposable { - private readonly ServerEntity _parent; + private readonly MistoxServer _parent; private readonly IPEndPoint _bindAddress; private readonly TcpListener _Listener; - public ServerListener(IPEndPoint listenerAddress, ServerEntity parent) { + public ServerListener(IPEndPoint listenerAddress, MistoxServer parent) { _bindAddress = listenerAddress; _parent = parent; diff --git a/MistoxServer/mPacket/DataPacket.cs b/MistoxServer/mPacket/DataPacket.cs index e4034c9..35de883 100644 --- a/MistoxServer/mPacket/DataPacket.cs +++ b/MistoxServer/mPacket/DataPacket.cs @@ -1,6 +1,5 @@ using System; using System.Buffers; -using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using MessagePack; @@ -28,7 +27,7 @@ namespace Mistox { // Use an expandable buffer writer from the system stack var writer = new ArrayBufferWriter(256); - // Advance the writer past the 3-byte header slot so MessagePack doesn't overwrite it + // Advance the writer past the header slot so MessagePack doesn't overwrite it writer.Advance(HeaderLength); // Serialize the payload DIRECTLY into the final buffer memory @@ -40,7 +39,7 @@ namespace Mistox { DataLength = (ushort)(writer.WrittenCount - HeaderLength) }; - // Map the struct directly onto the 3-byte reservation we left at the front + // Map the struct directly onto the reservation we left at the front writer.WrittenMemory.Slice(0, HeaderLength); MemoryMarshal.Write(writer.GetSpan(HeaderLength).Slice(0, HeaderLength), in header); diff --git a/Todo.txt b/Todo.txt index 1f1ab0b..0095cf8 100644 --- a/Todo.txt +++ b/Todo.txt @@ -1,3 +1,5 @@ +The server receive thread needs to push the user it was received from up the chain + 1. Allow the server to setup a server name 2. Allow the client to setup a user name 3. Have the client connect to the server with a welcome packet