Rework client to be non static

This commit is contained in:
derek.holloway committed 2026-07-08 13:45:20 -07:00
1 parent 6f910e969a
commit b439805f3e
7 files changed
+106 -107

No files matched your search

@@ -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<UDPEndpoint> onUserConnected;
public void InvokeOnUserConnected(UDPEndpoint ConnectedUser){
onUserConnected?.Invoke(ConnectedUser);
}
public event Action<dynamic> onDataReceived;
public void InvokeOnReceived(dynamic DataPacket){
onDataReceived?.Invoke(DataPacket);
}
public event Action<UDPEndpoint> onUserDisconnected;
public void InvokeOnUserDisconnected(UDPEndpoint DisconnectedUser){
onUserDisconnected?.Invoke(DisconnectedUser);
}
public event Action onServerDisconnected;
public void InvokeOnServerDisconnected(){
Alive = false;
onServerDisconnected?.Invoke();
}
}
}
@@ -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
}
}
+55
View File
@@ -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<string, mUDPClient> 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<mUDPClient> onUserConnected;
public void InvokeOnUserConnected(mUDPClient ConnectedUser){
onUserConnected?.Invoke(ConnectedUser);
}
public event Action<dynamic> onDataReceived;
public void InvokeOnReceived(dynamic DataPacket){
onDataReceived?.Invoke(DataPacket);
}
public event Action<mUDPClient> onUserDisconnected;
public void InvokeOnUserDisconnected(mUDPClient DisconnectedUser){
onUserDisconnected?.Invoke(DisconnectedUser);
}
public event Action onServerDisconnected;
public void InvokeOnServerDisconnected(){
Alive = false;
onServerDisconnected?.Invoke();
}
}
}
+15 -14
View File
@@ -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>(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;
}
}
}
+21 -16
View File
@@ -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<byte> 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>( 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;
}
}
}
+14 -29
View File
@@ -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<string, UDPEndpoint> clientConnections;
public static MistoxClient serverInstance;
public event Action onServerConnected;
public event Action<UDPEndpoint> onUserConnected;
public event Action<mUDPClient> onUserConnected;
public event Action<dynamic> onDataReceived;
public event Action<UDPEndpoint> onUserDisconnected;
public event Action<mUDPClient> 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>(Packet data) {
foreach(KeyValuePair<string, UDPEndpoint> cur in clientConnections){
await cur.Value.Connection.Send( data );
foreach(KeyValuePair<string, mUDPClient> cur in serverInstance.clientConnections){
await cur.Value.Send( data );
}
}
public async Task SendClient<Packet>(string UserName, Packet data) {
await clientConnections[UserName].Connection.Send( data );
await serverInstance.clientConnections[UserName].Send( data );
}
public async Task SendServer<Packet>(Packet data) {
await serverConnection.Send( data );
await serverInstance.ServerConnection.Send( data );
}
}
}
+1 -1
View File
@@ -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);
}