Skip to content

Commit

Permalink
Fixed p2p related crash when multiple messages are received in a shor…
Browse files Browse the repository at this point in the history
…t amount of time
  • Loading branch information
Skippeh committed Jan 16, 2022
1 parent e7cce58 commit 469566c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 58 deletions.
25 changes: 19 additions & 6 deletions JKMP.Plugin.Multiplayer/Networking/Framed.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -16,7 +17,8 @@ namespace JKMP.Plugin.Multiplayer.Networking
{
private bool isRunning = true;

private readonly ReusableTCS<TData?> tcs = new();
private TaskCompletionSource<bool>? tcs = new();
private readonly Queue<TData> queuedMessages = new();
private readonly TCodec codec;
private readonly byte[] sendBuffer = new byte[4096];

Expand Down Expand Up @@ -54,8 +56,9 @@ private async Task StartPolling()
Logger.Error(ex, "An unhandled exception was raised when decoding message from {steamId}", packet.SteamId);
continue;
}

await tcs.SetResult(message);

queuedMessages.Enqueue(message);
tcs?.TrySetResult(true);
}

await Task.Delay(33).ConfigureAwait(false); // poll around 30 times per second
Expand All @@ -73,9 +76,19 @@ private async Task StartPolling()
if (!isRunning)
return default;

await tcs;
TData? result = tcs.GetResult();
tcs.Reset();
if (tcs != null)
{
await tcs.Task;
tcs = null;
}

TData result = queuedMessages.Dequeue();

if (queuedMessages.Count == 0)
{
tcs = new();
}

return result;
}

Expand Down
52 changes: 0 additions & 52 deletions JKMP.Plugin.Multiplayer/Networking/ReusableTCS.cs

This file was deleted.

0 comments on commit 469566c

Please sign in to comment.