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

+6 -12
View File
@@ -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<TcpClientConnection> onUserConnected;
public void InvokeUserConnected( TcpClientConnection user ){ onUserConnected?.Invoke( user ); }
public event Action<dynamic> onDataReceived;
public void InvokeDataReceived( dynamic data ){ onDataReceived?.Invoke( data ); }
public event Action<TcpClientConnection> onUserDisconnected;
public void InvokeUserDisconnected( TcpClientConnection user ){ onUserDisconnected?.Invoke( user ); }
}
}
-1
View File
@@ -35,7 +35,6 @@ namespace Mistox.Server {
};
Console.WriteLine( "New User Connected" );
ServerInterface.ServerInstance.InvokeOnUserConnected( ServerInterface.Connections[connectionID] );
}
}
+11 -10
View File
@@ -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 _ );
}
}
}