Networking fixes

- Fixed incorrect disconnection check
- Close the socket after disconnection
- Catch OperationCancelledException
This commit is contained in:
Mitchell Kutchuk 2015-06-26 07:52:29 -07:00
parent 01b3742763
commit 0f715dad15
2 changed files with 21 additions and 8 deletions

View File

@ -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();
}

View File

@ -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)