From 54df3c0dd19f09741124dd2723b917dc2e874a83 Mon Sep 17 00:00:00 2001 From: Austin Milt Date: Sun, 1 Dec 2024 11:44:34 -0500 Subject: [PATCH] make WebSocketOpen non-async --- .../Api/ClientWebSocketWrapper.cs | 54 +++++++++++++++---- .../lua/CommonLibs/CommLuaLibrary.cs | 21 ++++---- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/BizHawk.Client.Common/Api/ClientWebSocketWrapper.cs b/src/BizHawk.Client.Common/Api/ClientWebSocketWrapper.cs index 5b7ec39d3e4..d239a8e47a2 100644 --- a/src/BizHawk.Client.Common/Api/ClientWebSocketWrapper.cs +++ b/src/BizHawk.Client.Common/Api/ClientWebSocketWrapper.cs @@ -33,28 +33,62 @@ public Task Close( return task; } - /// calls - public async Task Receive(int bufferSize, int maxMessages) + public async Task Connect(int bufferSize, int maxMessages) { + _w ??= new(); + if ((_w != null) && (_w.State != WebSocketState.Open)) + { + await _w.ConnectAsync(_uri, CancellationToken.None); + await Receive(bufferSize, maxMessages); + } + } + + /// opens a connection to the configured server and passes messages to [consumer] + public async Task Connect(Action consumer, int bufferSize = 1024) + { + _w ??= new(); + if ((_w != null) && (_w.State != WebSocketState.Open)) + { + await _w.ConnectAsync(_uri, CancellationToken.None); + } + var buffer = new ArraySegment(new byte[bufferSize]); while ((_w != null) && (_w.State == WebSocketState.Open)) { - WebSocketReceiveResult result; - result = await _w.ReceiveAsync(buffer, CancellationToken.None); - if (maxMessages == 0 || _receivedMessages.Count < maxMessages) - { - _receivedMessages.Enqueue(Encoding.UTF8.GetString(buffer.Array, 0, result.Count)); - } + var result = await _w.ReceiveAsync(buffer, CancellationToken.None); + string message = Encoding.UTF8.GetString(buffer.Array, 0, result.Count); + consumer(message); } } - public async Task Connect(int bufferSize, int maxMessages) + /// opens a connection to the configured server and passes messages to [consumer] + public async Task Connect(Action consumer, int bufferSize = 2048) { _w ??= new(); if ((_w != null) && (_w.State != WebSocketState.Open)) { await _w.ConnectAsync(_uri, CancellationToken.None); - await Receive(bufferSize, maxMessages); + } + + var buffer = new ArraySegment(new byte[bufferSize]); + while ((_w != null) && (_w.State == WebSocketState.Open)) + { + _ = await _w.ReceiveAsync(buffer, CancellationToken.None); + consumer(buffer.Array); + } + } + + /// calls + public async Task Receive(int bufferSize, int maxMessages) + { + var buffer = new ArraySegment(new byte[bufferSize]); + while ((_w != null) && (_w.State == WebSocketState.Open)) + { + var result = await _w.ReceiveAsync(buffer, CancellationToken.None); + if (maxMessages == 0 || _receivedMessages.Count < maxMessages) + { + _receivedMessages.Enqueue(Encoding.UTF8.GetString(buffer.Array, 0, result.Count)); + } } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs index 4e7a931f56b..839dc4c58af 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Net.WebSockets; using System.Text; -using System.Threading.Tasks; using NLua; namespace BizHawk.Client.Common @@ -254,9 +253,13 @@ private void CheckHttp() } } - [LuaMethod("ws_open", "Opens a websocket and returns the id so that it can be retrieved later. If an id is provided, reconnects to the ")] + [LuaMethod("ws_open", "Opens a websocket and returns the id so that it can be retrieved later. If an id is provided, reconnects to the server")] [LuaMethodExample("local ws_id = comm.ws_open(\"wss://echo.websocket.org\");")] - public async Task WebSocketOpen(string uri, string guid = null, int bufferSize = 1024, int maxMessages = 20) + public string WebSocketOpen( + string uri, + string guid = null, + int bufferSize = 1024, + int maxMessages = 20) { var wsServer = APIs.Comm.WebSockets; if (wsServer == null) @@ -264,15 +267,9 @@ public async Task WebSocketOpen(string uri, string guid = null, int buff return null; } var localGuid = guid == null ? Guid.NewGuid() : Guid.Parse(guid); - if (guid == null) - { - _websockets[localGuid] = wsServer.Open(new Uri(uri)); - await _websockets[localGuid].Connect(bufferSize, maxMessages); - } - else - { - await _websockets[localGuid].Connect(bufferSize, maxMessages); - } + + _websockets[localGuid] ??= wsServer.Open(new Uri(uri)); + _websockets[localGuid].Connect(bufferSize, maxMessages).Wait(500); return localGuid.ToString(); }