mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-12-16 02:55:44 +00:00
Added GC functions to WS handle, added clauses to websockets creation
This commit is contained in:
parent
1443c8a49d
commit
19246d2f33
4 changed files with 32 additions and 19 deletions
|
|
@ -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; }
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
namespace Capy64.Configuration;
|
|
||||||
|
|
||||||
class WebSockets
|
|
||||||
{
|
|
||||||
public bool Enable { get; set; } = true;
|
|
||||||
public int MaxActiveConnections { get; set; } = 5;
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using KeraLua;
|
using Capy64.LuaRuntime.Libraries;
|
||||||
|
using KeraLua;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -9,7 +10,7 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Capy64.LuaRuntime.Handlers;
|
namespace Capy64.LuaRuntime.Handlers;
|
||||||
|
|
||||||
class WebSocketHandle : IHandle
|
public class WebSocketHandle : IHandle
|
||||||
{
|
{
|
||||||
private ClientWebSocket _client;
|
private ClientWebSocket _client;
|
||||||
private long _requestId;
|
private long _requestId;
|
||||||
|
|
@ -32,6 +33,16 @@ class WebSocketHandle : IHandle
|
||||||
if (newTable)
|
if (newTable)
|
||||||
L.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)
|
foreach (var pair in functions)
|
||||||
{
|
{
|
||||||
L.PushString(pair.Key);
|
L.PushString(pair.Key);
|
||||||
|
|
@ -84,6 +95,8 @@ class WebSocketHandle : IHandle
|
||||||
_game.LuaRuntime.PushEvent("websocket_close", h._requestId);
|
_game.LuaRuntime.PushEvent("websocket_close", h._requestId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
HTTP.WebSocketConnections.Remove(h);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,11 @@ public class HTTP : IPlugin
|
||||||
private static IGame _game;
|
private static IGame _game;
|
||||||
private static HttpClient _httpClient;
|
private static HttpClient _httpClient;
|
||||||
private static long _requestId;
|
private static long _requestId;
|
||||||
|
public static readonly HashSet<WebSocketHandle> WebSocketConnections = new();
|
||||||
|
|
||||||
public static readonly string UserAgent = $"Capy64/{Capy64.Version}";
|
public static readonly string UserAgent = $"Capy64/{Capy64.Version}";
|
||||||
|
|
||||||
private readonly IConfiguration _configuration;
|
private static IConfiguration _configuration;
|
||||||
private readonly LuaRegister[] HttpLib = new LuaRegister[]
|
private readonly LuaRegister[] HttpLib = new LuaRegister[]
|
||||||
{
|
{
|
||||||
new()
|
new()
|
||||||
|
|
@ -251,6 +252,20 @@ public class HTTP : IPlugin
|
||||||
{
|
{
|
||||||
var L = Lua.FromIntPtr(state);
|
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);
|
var url = L.CheckString(1);
|
||||||
if (!TryGetUri(url, out var uri))
|
if (!TryGetUri(url, out var uri))
|
||||||
{
|
{
|
||||||
|
|
@ -308,6 +323,7 @@ public class HTTP : IPlugin
|
||||||
await task;
|
await task;
|
||||||
|
|
||||||
var handle = new WebSocketHandle(wsClient, requestId, _game);
|
var handle = new WebSocketHandle(wsClient, requestId, _game);
|
||||||
|
WebSocketConnections.Add(handle);
|
||||||
|
|
||||||
_game.LuaRuntime.PushEvent("websocket_connect", L =>
|
_game.LuaRuntime.PushEvent("websocket_connect", L =>
|
||||||
{
|
{
|
||||||
|
|
@ -325,7 +341,6 @@ public class HTTP : IPlugin
|
||||||
var result = await wsClient.ReceiveAsync(buffer, CancellationToken.None);
|
var result = await wsClient.ReceiveAsync(buffer, CancellationToken.None);
|
||||||
if (result.MessageType == WebSocketMessageType.Close)
|
if (result.MessageType == WebSocketMessageType.Close)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Closing");
|
|
||||||
await wsClient.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
|
await wsClient.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
|
||||||
_game.LuaRuntime.PushEvent("websocket_close", requestId);
|
_game.LuaRuntime.PushEvent("websocket_close", requestId);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue