Wire up the event handlers

This commit is contained in:
derek committed 2026-07-03 11:37:41 -07:00
1 parent a04516c470
commit a689f2656f
11 files changed
+90 -76

No files matched your search

@@ -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<UDPEndpoint> onUserConnected;
public void InvokeOnUserConnected(UDPEndpoint ConnectedUser){
onUserConnected?.Invoke(ConnectedUser, new EventArgs());
onUserConnected?.Invoke(ConnectedUser);
}
public event EventHandler onReceived;
public event Action<dynamic> onDataReceived;
public void InvokeOnReceived(dynamic DataPacket){
onReceived?.Invoke(DataPacket, new EventArgs());
onDataReceived?.Invoke(DataPacket);
}
public event EventHandler onUserDisconnected;
public event Action<UDPEndpoint> 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();
}
}
@@ -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
}
}
@@ -0,0 +1,8 @@
using Mistox.Attributes;
[mPacket(0xFF)]
public class UserConnectionPacket {
string UserName { get; set; }
long RemoteIP { get; set; }
int Port { get; set; }
}
+6 -16
View File
@@ -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;
}
+14 -12
View File
@@ -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>( T Data, IPEndPoint remoteHost ) {
public async Task Send<T>( 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;
}