Skip to content

Commit

Permalink
fixed deadlock in MinecraftClient
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Jul 27, 2024
1 parent 9947b89 commit 499c767
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions Components/MineSharp.Protocol/MinecraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task<bool> Connect(GameState nextState)
client = await tcpTcpFactory.CreateOpenConnection(ip, Port);
stream = new(client.GetStream(), Data.Version.Protocol);

StreamLoop();
streamLoop = Task.Run(StreamLoop);
Logger.Info("Connected, starting handshake...");
await HandshakeProtocol.PerformHandshake(this, nextState, Data);
}
Expand Down Expand Up @@ -352,26 +352,23 @@ internal void HandleBundleDelimiter()
Logger.Debug("Bundling packets!");
}
}

private void StreamLoop()
private async Task StreamLoop()
{
streamLoop = Task.Run(async () =>
while (!cancellationTokenSource.Token.IsCancellationRequested)
{
while (!cancellationTokenSource.Token.IsCancellationRequested)
try
{
try
{
await ReceivePackets();
await SendPackets();
await ReceivePackets();
await SendPackets();

await Task.Delay(1);
}
catch (Exception ex)
{
Logger.Error(ex, "Encountered error in stream loop");
}
await Task.Delay(1);
}
catch (Exception ex)
{
Logger.Error(ex, "Encountered error in stream loop");
}
}, cancellationTokenSource.Token);
}
}

private async Task ReceivePackets()
Expand Down Expand Up @@ -424,9 +421,12 @@ private Task SendPackets()

DispatchPacket(task.Packet);

task.Task.TrySetResult();

_ = Task.Run(() => HandleOutgoingPacket(task.Packet));
_ = Task.Run(async () =>
{
task.Task.TrySetResult();
await HandleOutgoingPacket(task.Packet);
});

return Task.CompletedTask;
}
Expand Down

0 comments on commit 499c767

Please sign in to comment.