.NET 5+ Support (#1674)

Implement changes to support .NET 5 onwards.
Co-authored-by: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com>
Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
breadbyte 2022-07-03 22:34:07 +08:00 committed by GitHub
parent b3cc2351ee
commit d9f1a77ac2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 1028 additions and 9058 deletions

View file

@ -23,7 +23,7 @@ namespace MinecraftClient.Protocol.Handlers
private string autocomplete_result = "";
private bool encrypted = false;
private int protocolversion;
private Thread netRead;
private Tuple<Thread, CancellationTokenSource>? netRead = null;
Crypto.IAesStream s;
TcpClient c;
@ -60,20 +60,28 @@ namespace MinecraftClient.Protocol.Handlers
this.c = Client;
}
private void Updater()
private void Updater(object? o)
{
if (((CancellationToken) o!).IsCancellationRequested)
return;
try
{
do
while (!((CancellationToken) o!).IsCancellationRequested)
{
Thread.Sleep(100);
do
{
Thread.Sleep(100);
} while (Update());
}
while (Update());
}
catch (System.IO.IOException) { }
catch (SocketException) { }
catch (ObjectDisposedException) { }
if (((CancellationToken) o!).IsCancellationRequested)
return;
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
}
@ -194,11 +202,11 @@ namespace MinecraftClient.Protocol.Handlers
return true; //packet has been successfully skipped
}
private void StartUpdating()
private void StartUpdating()
{
netRead = new Thread(new ThreadStart(Updater));
netRead.Name = "ProtocolPacketHandler";
netRead.Start();
netRead = new(new Thread(new ParameterizedThreadStart(Updater)), new CancellationTokenSource());
netRead.Item1.Name = "ProtocolPacketHandler";
netRead.Item1.Start(netRead.Item2.Token);
}
/// <summary>
@ -207,7 +215,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>Net read thread ID</returns>
public int GetNetReadThreadId()
{
return netRead != null ? netRead.ManagedThreadId : -1;
return netRead != null ? netRead.Item1.ManagedThreadId : -1;
}
public void Dispose()
@ -216,7 +224,7 @@ namespace MinecraftClient.Protocol.Handlers
{
if (netRead != null)
{
netRead.Abort();
netRead.Item2.Cancel();
c.Close();
}
}

View file

@ -12,7 +12,6 @@ using MinecraftClient.Mapping.BlockPalettes;
using MinecraftClient.Mapping.EntityPalettes;
using MinecraftClient.Protocol.Handlers.Forge;
using MinecraftClient.Inventory;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using MinecraftClient.Inventory.ItemPalettes;
@ -72,7 +71,7 @@ namespace MinecraftClient.Protocol.Handlers
PacketTypePalette packetPalette;
SocketWrapper socketWrapper;
DataTypes dataTypes;
Thread netRead; // main thread
Tuple<Thread, CancellationTokenSource>? netRead = null; // main thread
ILogger log;
public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHandler handler, ForgeInfo forgeInfo)
@ -153,14 +152,22 @@ namespace MinecraftClient.Protocol.Handlers
/// <summary>
/// Separate thread. Network reading loop.
/// </summary>
private void Updater()
private void Updater(object? o)
{
try
if (((CancellationToken) o!).IsCancellationRequested)
return;
try
{
bool keepUpdating = true;
Stopwatch stopWatch = new Stopwatch();
while (keepUpdating)
while (keepUpdating)
{
((CancellationToken)o!).ThrowIfCancellationRequested();
stopWatch.Start();
keepUpdating = Update();
stopWatch.Stop();
@ -173,7 +180,11 @@ namespace MinecraftClient.Protocol.Handlers
catch (System.IO.IOException) { }
catch (SocketException) { }
catch (ObjectDisposedException) { }
catch (OperationCanceledException) { }
if (((CancellationToken) o!).IsCancellationRequested)
return;
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
}
@ -1160,11 +1171,12 @@ namespace MinecraftClient.Protocol.Handlers
/// <summary>
/// Start the updating thread. Should be called after login success.
/// </summary>
private void StartUpdating()
private void StartUpdating()
{
netRead = new Thread(new ThreadStart(Updater));
netRead.Name = "ProtocolPacketHandler";
netRead.Start();
netRead = new Tuple<Thread, CancellationTokenSource>(new Thread(new ParameterizedThreadStart(Updater)), new CancellationTokenSource());
netRead.Item1.Name = "ProtocolPacketHandler";
netRead.Item1.Start(netRead.Item2.Token);
}
/// <summary>
@ -1173,7 +1185,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>Net read thread ID</returns>
public int GetNetReadThreadId()
{
return netRead != null ? netRead.ManagedThreadId : -1;
return netRead != null ? netRead.Item1.ManagedThreadId : -1;
}
/// <summary>
@ -1185,7 +1197,7 @@ namespace MinecraftClient.Protocol.Handlers
{
if (netRead != null)
{
netRead.Abort();
netRead.Item2.Cancel();
socketWrapper.Disconnect();
}
}