mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Bug fix: Can't reconnect after connection lost
This commit is contained in:
parent
db64515b78
commit
98dd645fb5
4 changed files with 31 additions and 18 deletions
|
|
@ -446,6 +446,8 @@ namespace MinecraftClient
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
|
public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
|
||||||
{
|
{
|
||||||
|
handler.Dispose();
|
||||||
|
|
||||||
world.Clear();
|
world.Clear();
|
||||||
|
|
||||||
if (timeoutdetector != null)
|
if (timeoutdetector != null)
|
||||||
|
|
@ -761,7 +763,7 @@ namespace MinecraftClient
|
||||||
int callingThreadId = Thread.CurrentThread.ManagedThreadId;
|
int callingThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||||
if (handler != null)
|
if (handler != null)
|
||||||
{
|
{
|
||||||
return handler.GetNetReadThreadId() != callingThreadId;
|
return handler.GetNetMainThreadId() != callingThreadId;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -218,7 +218,7 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
/// Get net read thread (main thread) ID
|
/// Get net read thread (main thread) ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Net read thread ID</returns>
|
/// <returns>Net read thread ID</returns>
|
||||||
public int GetNetReadThreadId()
|
public int GetNetMainThreadId()
|
||||||
{
|
{
|
||||||
return netRead != null ? netRead.Item1.ManagedThreadId : -1;
|
return netRead != null ? netRead.Item1.ManagedThreadId : -1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,8 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
PacketTypePalette packetPalette;
|
PacketTypePalette packetPalette;
|
||||||
SocketWrapper socketWrapper;
|
SocketWrapper socketWrapper;
|
||||||
DataTypes dataTypes;
|
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;
|
ILogger log;
|
||||||
RandomNumberGenerator randomGen;
|
RandomNumberGenerator randomGen;
|
||||||
|
|
||||||
|
|
@ -172,7 +173,9 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void Updater(object? o)
|
private void Updater(object? o)
|
||||||
{
|
{
|
||||||
if (((CancellationToken)o!).IsCancellationRequested)
|
CancellationToken cancelToken = (CancellationToken)o!;
|
||||||
|
|
||||||
|
if (cancelToken.IsCancellationRequested)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|
@ -180,7 +183,7 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
Stopwatch stopWatch = new();
|
Stopwatch stopWatch = new();
|
||||||
while (!packetQueue.IsAddingCompleted)
|
while (!packetQueue.IsAddingCompleted)
|
||||||
{
|
{
|
||||||
((CancellationToken)o!).ThrowIfCancellationRequested();
|
cancelToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
handler.OnUpdate();
|
handler.OnUpdate();
|
||||||
|
|
||||||
|
|
@ -210,7 +213,7 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
catch (ObjectDisposedException) { }
|
catch (ObjectDisposedException) { }
|
||||||
catch (OperationCanceledException) { }
|
catch (OperationCanceledException) { }
|
||||||
|
|
||||||
if (((CancellationToken)o!).IsCancellationRequested)
|
if (cancelToken.IsCancellationRequested)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
|
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
|
||||||
|
|
@ -219,13 +222,14 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read and decompress packets.
|
/// Read and decompress packets.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal void PacketReader()
|
internal void PacketReader(object? o)
|
||||||
{
|
{
|
||||||
while (socketWrapper.IsConnected())
|
CancellationToken cancelToken = (CancellationToken)o!;
|
||||||
|
while (socketWrapper.IsConnected() && !cancelToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (socketWrapper.HasDataAvailable())
|
while (socketWrapper.HasDataAvailable() && !cancelToken.IsCancellationRequested)
|
||||||
packetQueue.Add(ReadNextPacket());
|
packetQueue.Add(ReadNextPacket());
|
||||||
}
|
}
|
||||||
catch (System.IO.IOException) { break; }
|
catch (System.IO.IOException) { break; }
|
||||||
|
|
@ -1449,20 +1453,24 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void StartUpdating()
|
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());
|
Thread threadReader = new Thread(new ParameterizedThreadStart(PacketReader));
|
||||||
netRead.Item1.Name = "ProtocolPacketHandler";
|
threadReader.Name = "ProtocolPacketReader";
|
||||||
netRead.Item1.Start(netRead.Item2.Token);
|
netReader = new Tuple<Thread, CancellationTokenSource>(threadReader, new CancellationTokenSource());
|
||||||
|
threadReader.Start(netReader.Item2.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get net read thread (main thread) ID
|
/// Get net read thread (main thread) ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Net read thread ID</returns>
|
/// <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>
|
/// <summary>
|
||||||
|
|
@ -1472,9 +1480,12 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (netRead != null)
|
if (netMain != null)
|
||||||
{
|
{
|
||||||
netRead.Item2.Cancel();
|
netMain.Item2.Cancel();
|
||||||
|
}
|
||||||
|
if (netReader != null){
|
||||||
|
netReader.Item2.Cancel();
|
||||||
socketWrapper.Disconnect();
|
socketWrapper.Disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -252,6 +252,6 @@ namespace MinecraftClient.Protocol
|
||||||
/// Get net read thread (main thread) ID
|
/// Get net read thread (main thread) ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Net read thread ID</returns>
|
/// <returns>Net read thread ID</returns>
|
||||||
int GetNetReadThreadId();
|
int GetNetMainThreadId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue