From ebcbdaba5da058371356a54eaf1041c6b8ea91a5 Mon Sep 17 00:00:00 2001 From: dariof4 Date: Fri, 9 Jun 2023 21:40:52 +0200 Subject: [PATCH] Use BlockingCollection instead of ConcurrentQueue. (#79) Use BlockingCollection instead of ConcurrentQueue in ArchipelagoSocketHelper_system.net.websockets.cs to fix high cpu usage due to continuosly polling for packets to send. --- .../ArchipelagoSocketHelper_system.net.websockets.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Archipelago.MultiClient.Net/Helpers/ArchipelagoSocketHelper_system.net.websockets.cs b/Archipelago.MultiClient.Net/Helpers/ArchipelagoSocketHelper_system.net.websockets.cs index 54fbaa4..1f3e6df 100644 --- a/Archipelago.MultiClient.Net/Helpers/ArchipelagoSocketHelper_system.net.websockets.cs +++ b/Archipelago.MultiClient.Net/Helpers/ArchipelagoSocketHelper_system.net.websockets.cs @@ -31,8 +31,8 @@ public class ArchipelagoSocketHelper : IArchipelagoSocketHelper public event ArchipelagoSocketHelperDelagates.SocketClosedHandler SocketClosed; public event ArchipelagoSocketHelperDelagates.SocketOpenedHandler SocketOpened; - readonly ConcurrentQueue>> sendQueue = - new ConcurrentQueue>>(); + readonly BlockingCollection>> sendQueue = + new BlockingCollection>>(); /// /// The URL of the host that the socket is connected to. @@ -250,7 +250,7 @@ public Task SendMultiplePacketsAsync(params ArchipelagoPacketBase[] packets) var task = new TaskCompletionSource(); foreach (var packet in packets) - sendQueue.Enqueue(new Tuple>(packet, task)); + sendQueue.Add(new Tuple>(packet, task)); return task.Task; } @@ -260,7 +260,10 @@ async Task HandleSendBuffer() var packetList = new List(); var tasks = new List>(); - 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);