Bug fix: Can't reconnect after connection lost

This commit is contained in:
BruceChen 2022-08-31 22:32:38 +08:00
parent db64515b78
commit 98dd645fb5
4 changed files with 31 additions and 18 deletions

View file

@ -446,6 +446,8 @@ namespace MinecraftClient
/// </summary>
public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
{
handler.Dispose();
world.Clear();
if (timeoutdetector != null)
@ -761,7 +763,7 @@ namespace MinecraftClient
int callingThreadId = Thread.CurrentThread.ManagedThreadId;
if (handler != null)
{
return handler.GetNetReadThreadId() != callingThreadId;
return handler.GetNetMainThreadId() != callingThreadId;
}
else
{

View file

@ -218,7 +218,7 @@ namespace MinecraftClient.Protocol.Handlers
/// Get net read thread (main thread) ID
/// </summary>
/// <returns>Net read thread ID</returns>
public int GetNetReadThreadId()
public int GetNetMainThreadId()
{
return netRead != null ? netRead.Item1.ManagedThreadId : -1;
}

View file

@ -76,7 +76,8 @@ namespace MinecraftClient.Protocol.Handlers
PacketTypePalette packetPalette;
SocketWrapper socketWrapper;
DataTypes dataTypes;
Tuple<Thread, CancellationTokenSource>? netRead = null; // main thread
Tuple<Thread, CancellationTokenSource>? netMain = null; // main thread
Tuple<Thread, CancellationTokenSource>? netReader = null; // reader thread
ILogger log;
RandomNumberGenerator randomGen;
@ -172,7 +173,9 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
private void Updater(object? o)
{
if (((CancellationToken)o!).IsCancellationRequested)
CancellationToken cancelToken = (CancellationToken)o!;
if (cancelToken.IsCancellationRequested)
return;
try
@ -180,7 +183,7 @@ namespace MinecraftClient.Protocol.Handlers
Stopwatch stopWatch = new();
while (!packetQueue.IsAddingCompleted)
{
((CancellationToken)o!).ThrowIfCancellationRequested();
cancelToken.ThrowIfCancellationRequested();
handler.OnUpdate();
@ -210,7 +213,7 @@ namespace MinecraftClient.Protocol.Handlers
catch (ObjectDisposedException) { }
catch (OperationCanceledException) { }
if (((CancellationToken)o!).IsCancellationRequested)
if (cancelToken.IsCancellationRequested)
return;
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
@ -219,13 +222,14 @@ namespace MinecraftClient.Protocol.Handlers
/// <summary>
/// Read and decompress packets.
/// </summary>
internal void PacketReader()
internal void PacketReader(object? o)
{
while (socketWrapper.IsConnected())
CancellationToken cancelToken = (CancellationToken)o!;
while (socketWrapper.IsConnected() && !cancelToken.IsCancellationRequested)
{
try
{
while (socketWrapper.HasDataAvailable())
while (socketWrapper.HasDataAvailable() && !cancelToken.IsCancellationRequested)
packetQueue.Add(ReadNextPacket());
}
catch (System.IO.IOException) { break; }
@ -1449,20 +1453,24 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
private void StartUpdating()
{
new Thread(new ThreadStart(PacketReader)).Start();
Thread threadUpdater = new Thread(new ParameterizedThreadStart(Updater));
threadUpdater.Name = "ProtocolPacketHandler";
netMain = new Tuple<Thread, CancellationTokenSource>(threadUpdater, new CancellationTokenSource());
threadUpdater.Start(netMain.Item2.Token);
netRead = new Tuple<Thread, CancellationTokenSource>(new Thread(new ParameterizedThreadStart(Updater)), new CancellationTokenSource());
netRead.Item1.Name = "ProtocolPacketHandler";
netRead.Item1.Start(netRead.Item2.Token);
Thread threadReader = new Thread(new ParameterizedThreadStart(PacketReader));
threadReader.Name = "ProtocolPacketReader";
netReader = new Tuple<Thread, CancellationTokenSource>(threadReader, new CancellationTokenSource());
threadReader.Start(netReader.Item2.Token);
}
/// <summary>
/// Get net read thread (main thread) ID
/// </summary>
/// <returns>Net read thread ID</returns>
public int GetNetReadThreadId()
public int GetNetMainThreadId()
{
return netRead != null ? netRead.Item1.ManagedThreadId : -1;
return netMain != null ? netMain.Item1.ManagedThreadId : -1;
}
/// <summary>
@ -1472,9 +1480,12 @@ namespace MinecraftClient.Protocol.Handlers
{
try
{
if (netRead != null)
if (netMain != null)
{
netRead.Item2.Cancel();
netMain.Item2.Cancel();
}
if (netReader != null){
netReader.Item2.Cancel();
socketWrapper.Disconnect();
}
}

View file

@ -252,6 +252,6 @@ namespace MinecraftClient.Protocol
/// Get net read thread (main thread) ID
/// </summary>
/// <returns>Net read thread ID</returns>
int GetNetReadThreadId();
int GetNetMainThreadId();
}
}