Wire up the event handlers
This commit is contained in:
1 parent
a04516c470
commit
a689f2656f
11 files changed
+90
-76
No files matched your search
@@ -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 _ );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user