Skip to content

Commit

Permalink
Use BlockingCollection instead of ConcurrentQueue. (#79)
Browse files Browse the repository at this point in the history
Use BlockingCollection instead of ConcurrentQueue in
ArchipelagoSocketHelper_system.net.websockets.cs to fix high cpu usage
due to continuosly polling for packets to send.
  • Loading branch information
dariof4 authored Jun 9, 2023
1 parent 70ed08f commit ebcbdab
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class ArchipelagoSocketHelper : IArchipelagoSocketHelper
public event ArchipelagoSocketHelperDelagates.SocketClosedHandler SocketClosed;
public event ArchipelagoSocketHelperDelagates.SocketOpenedHandler SocketOpened;

readonly ConcurrentQueue<Tuple<ArchipelagoPacketBase, TaskCompletionSource<bool>>> sendQueue =
new ConcurrentQueue<Tuple<ArchipelagoPacketBase, TaskCompletionSource<bool>>>();
readonly BlockingCollection<Tuple<ArchipelagoPacketBase, TaskCompletionSource<bool>>> sendQueue =
new BlockingCollection<Tuple<ArchipelagoPacketBase, TaskCompletionSource<bool>>>();

/// <summary>
/// The URL of the host that the socket is connected to.
Expand Down Expand Up @@ -250,7 +250,7 @@ public Task SendMultiplePacketsAsync(params ArchipelagoPacketBase[] packets)
var task = new TaskCompletionSource<bool>();

foreach (var packet in packets)
sendQueue.Enqueue(new Tuple<ArchipelagoPacketBase, TaskCompletionSource<bool>>(packet, task));
sendQueue.Add(new Tuple<ArchipelagoPacketBase, TaskCompletionSource<bool>>(packet, task));

return task.Task;
}
Expand All @@ -260,7 +260,10 @@ async Task HandleSendBuffer()
var packetList = new List<ArchipelagoPacketBase>();
var tasks = new List<TaskCompletionSource<bool>>();

while (sendQueue.TryDequeue(out var packetTuple))
var firstPacketTuple = sendQueue.Take();
packetList.Add(firstPacketTuple.Item1);
tasks.Add(firstPacketTuple.Item2);
while (sendQueue.TryTake(out var packetTuple))
{
packetList.Add(packetTuple.Item1);
tasks.Add(packetTuple.Item2);
Expand Down

0 comments on commit ebcbdab

Please sign in to comment.