From 4a9d2bad348eb822a0d83d3eae2b0c95c6a04114 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 17 Mar 2023 21:00:20 +0100 Subject: [PATCH] Adapt WS to use Task --- Capy64/Runtime/Libraries/HTTP.cs | 15 +++-------- Capy64/Runtime/Objects/WebSocketClient.cs | 33 +++++++++-------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/Capy64/Runtime/Libraries/HTTP.cs b/Capy64/Runtime/Libraries/HTTP.cs index 3fefe02..37694f5 100644 --- a/Capy64/Runtime/Libraries/HTTP.cs +++ b/Capy64/Runtime/Libraries/HTTP.cs @@ -315,19 +315,14 @@ public class HTTP : IComponent L.Pop(1); } + var clientTask = TaskMeta.Push(L, WebSocketClient.ObjectType); var connectTask = wsClient.ConnectAsync(uri, CancellationToken.None); connectTask.ContinueWith(async task => { if (task.IsFaulted || task.IsCanceled) { - _game.LuaRuntime.QueueEvent("websocket_failure", LK => - { - LK.PushInteger(requestId); - LK.PushString(task.Exception?.Message); - - return 2; - }); + clientTask.Reject(task.Exception?.Message); return; } @@ -336,14 +331,10 @@ public class HTTP : IComponent var handle = new WebSocketClient.Client(wsClient, requestId); WebSocketConnections.Add(handle); - _game.LuaRuntime.QueueEvent("websocket_connect", LK => + clientTask.Fulfill(LK => { - LK.PushInteger(requestId); - ObjectManager.PushObject(LK, handle); LK.SetMetaTable(WebSocketClient.ObjectType); - - return 2; }); var buffer = new byte[4096]; diff --git a/Capy64/Runtime/Objects/WebSocketClient.cs b/Capy64/Runtime/Objects/WebSocketClient.cs index 6964460..f45d764 100644 --- a/Capy64/Runtime/Objects/WebSocketClient.cs +++ b/Capy64/Runtime/Objects/WebSocketClient.cs @@ -29,21 +29,6 @@ public class WebSocketClient : IComponent public record Client(ClientWebSocket Socket, long RequestId); - public void LuaInit(Lua L) - { - CreateMeta(L); - } - - public static void CreateMeta(Lua L) - { - L.NewMetaTable(ObjectType); - L.SetFuncs(MetaMethods, 0); - L.NewLibTable(Methods); - L.SetFuncs(Methods, 0); - L.SetField(-2, "__index"); - L.Pop(1); - } - internal static LuaRegister[] Methods = new LuaRegister[] { new() @@ -89,12 +74,20 @@ public class WebSocketClient : IComponent new(), }; - private static readonly Dictionary functions = new() + public void LuaInit(Lua L) { - ["getRequestID"] = L_GetRequestId, - ["send"] = L_Send, - ["closeAsync"] = L_CloseAsync, - }; + CreateMeta(L); + } + + public static void CreateMeta(Lua L) + { + L.NewMetaTable(ObjectType); + L.SetFuncs(MetaMethods, 0); + L.NewLibTable(Methods); + L.SetFuncs(Methods, 0); + L.SetField(-2, "__index"); + L.Pop(1); + } public static Client ToObject(Lua L, bool gc = false) {