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
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user