Added GC functions to WS handle, added clauses to websockets creation

This commit is contained in:
Alessandro Proto 2023-01-17 22:15:11 +01:00
parent 1443c8a49d
commit 19246d2f33
4 changed files with 32 additions and 19 deletions

View file

@ -1,8 +0,0 @@
namespace Capy64.Configuration;
class HTTP
{
public bool Enable { get; set; } = true;
public string[] Blacklist { get; set; }
public WebSockets WebSockets { get; set; }
}

View file

@ -1,7 +0,0 @@
namespace Capy64.Configuration;
class WebSockets
{
public bool Enable { get; set; } = true;
public int MaxActiveConnections { get; set; } = 5;
}

View file

@ -1,4 +1,5 @@
using KeraLua;
using Capy64.LuaRuntime.Libraries;
using KeraLua;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,7 +10,7 @@ using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers;
class WebSocketHandle : IHandle
public class WebSocketHandle : IHandle
{
private ClientWebSocket _client;
private long _requestId;
@ -32,6 +33,16 @@ class WebSocketHandle : IHandle
if (newTable)
L.NewTable();
// metatable
L.NewTable();
L.PushString("__close");
L.PushCFunction(L_CloseAsync);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_CloseAsync);
L.SetTable(-3);
L.SetMetaTable(-2);
foreach (var pair in functions)
{
L.PushString(pair.Key);
@ -84,6 +95,8 @@ class WebSocketHandle : IHandle
_game.LuaRuntime.PushEvent("websocket_close", h._requestId);
});
HTTP.WebSocketConnections.Remove(h);
return 0;
}
}

View file

@ -18,10 +18,11 @@ public class HTTP : IPlugin
private static IGame _game;
private static HttpClient _httpClient;
private static long _requestId;
public static readonly HashSet<WebSocketHandle> WebSocketConnections = new();
public static readonly string UserAgent = $"Capy64/{Capy64.Version}";
private readonly IConfiguration _configuration;
private static IConfiguration _configuration;
private readonly LuaRegister[] HttpLib = new LuaRegister[]
{
new()
@ -251,6 +252,20 @@ public class HTTP : IPlugin
{
var L = Lua.FromIntPtr(state);
var wsSettings = _configuration.GetSection("HTTP:WebSockets");
if (!wsSettings.GetValue<bool>("Enable"))
{
L.Error("WebSockets are disabled");
return 0;
}
if (WebSocketConnections.Count >= wsSettings.GetValue<int>("MaxActiveConnections"))
{
L.Error("Max connections reached");
return 0;
}
var url = L.CheckString(1);
if (!TryGetUri(url, out var uri))
{
@ -308,6 +323,7 @@ public class HTTP : IPlugin
await task;
var handle = new WebSocketHandle(wsClient, requestId, _game);
WebSocketConnections.Add(handle);
_game.LuaRuntime.PushEvent("websocket_connect", L =>
{
@ -325,7 +341,6 @@ public class HTTP : IPlugin
var result = await wsClient.ReceiveAsync(buffer, CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
Console.WriteLine("Closing");
await wsClient.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
_game.LuaRuntime.PushEvent("websocket_close", requestId);
return;