diff --git a/TrueCraft.Client/MultiplayerClient.cs b/TrueCraft.Client/MultiplayerClient.cs index fab2332..7a4f1e8 100644 --- a/TrueCraft.Client/MultiplayerClient.cs +++ b/TrueCraft.Client/MultiplayerClient.cs @@ -60,7 +60,6 @@ namespace TrueCraft.Client Client = new TcpClient(); PacketReader = new PacketReader(); PacketReader.RegisterCorePackets(); - //NetworkWorker = new Thread(new ThreadStart(DoNetwork)); PacketHandlers = new PacketHandler[0x100]; Handlers.PacketHandlers.RegisterHandlers(this); World = new ReadOnlyWorld(); @@ -194,7 +193,14 @@ namespace TrueCraft.Client if (Client != null && !Client.Client.ReceiveAsync(newArgs)) OperationCompleted(this, newArgs); - sem.Wait(cancel.Token); + try + { + sem.Wait(cancel.Token); + } + catch (OperationCanceledException) + { + return; + } var packets = PacketReader.ReadPackets(this, e.Buffer, e.Offset, e.BytesTransferred, false); @@ -203,7 +209,7 @@ namespace TrueCraft.Client if (PacketHandlers[packet.ID] != null) PacketHandlers[packet.ID](packet, this); } - + if (sem != null) sem.Release(); } diff --git a/TrueCraft/RemoteClient.cs b/TrueCraft/RemoteClient.cs index 2a11315..7a2d8d2 100644 --- a/TrueCraft/RemoteClient.cs +++ b/TrueCraft/RemoteClient.cs @@ -281,10 +281,14 @@ namespace TrueCraft Server.DisconnectClient(this); e.SetBuffer(null, 0, 0); + break; + case SocketAsyncOperation.Disconnect: + Connection.Close(); + break; } - if(Connection != null) + if (Connection != null) if (!Connection.Connected && !Disconnected) Server.DisconnectClient(this); } @@ -345,15 +349,18 @@ namespace TrueCraft public void Disconnect() { - if (!Disconnected) + if (Disconnected) return; Disconnected = true; - SocketAsyncEventArgs args = new SocketAsyncEventArgs(); - Connection.DisconnectAsync(args); - cancel.Cancel(); + + Connection.Shutdown(SocketShutdown.Send); + + SocketAsyncEventArgs args = new SocketAsyncEventArgs(); + args.Completed += OperationCompleted; + Connection.DisconnectAsync(args); } public void SendMessage(string message)