Server list Endpoint Caching (Thanks FakeByte! :))
This commit is contained in:
parent
046cfe3d4f
commit
35bd75bc05
5 changed files with 29 additions and 13 deletions
|
|
@ -48,7 +48,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
WriteLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red);
|
||||
}
|
||||
|
||||
var pingThread = new Thread(new ThreadStart(() => PingServers()));
|
||||
var pingThread = new Thread(new ThreadStart(PingServers));
|
||||
pingThread.Start();
|
||||
|
||||
// keep console alive
|
||||
|
|
@ -99,7 +99,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
}
|
||||
}
|
||||
|
||||
async Task PingServers()
|
||||
async void PingServers()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
|
|
@ -154,7 +154,7 @@ namespace LightReflectiveMirror.LoadBalancing
|
|||
| | | | | | / \ | | | | w /|
|
||||
| | | | | | / /\ \ | | | | | \
|
||||
| |____ | |__| | / ____ \ | |__| | m m copyright monkesoft 2021
|
||||
|______| \____/ /_/ \_\ |_____/
|
||||
|______| \____/ /_/ \_\ |_____/
|
||||
____ _ _ _ _____ ______ _____
|
||||
| _ \ /\ | | /\ | \ | | / ____| | ____| | __ \
|
||||
| |_) | / \ | | / \ | \| | | | | |__ | |__) |
|
||||
|
|
|
|||
|
|
@ -23,7 +23,9 @@ namespace LightReflectiveMirror.Endpoints
|
|||
[RestResource]
|
||||
public class Endpoint
|
||||
{
|
||||
private List<Room> _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); }
|
||||
private static string _cachedServerList = "[]";
|
||||
private static string _cachedCompressedServerList;
|
||||
private static List<Room> _rooms { get => Program.instance.GetRooms().Where(x => x.isPublic).ToList(); }
|
||||
|
||||
private RelayStats _stats { get => new RelayStats
|
||||
{
|
||||
|
|
@ -33,6 +35,12 @@ namespace LightReflectiveMirror.Endpoints
|
|||
Uptime = Program.instance.GetUptime()
|
||||
}; }
|
||||
|
||||
public static void RoomsModified()
|
||||
{
|
||||
_cachedServerList = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
|
||||
_cachedCompressedServerList = _cachedServerList.Compress();
|
||||
}
|
||||
|
||||
[RestRoute("Get", "/api/stats")]
|
||||
public async Task Stats(IHttpContext context)
|
||||
{
|
||||
|
|
@ -45,8 +53,7 @@ namespace LightReflectiveMirror.Endpoints
|
|||
{
|
||||
if (Program.conf.EndpointServerList)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
|
||||
await context.Response.SendResponseAsync(json);
|
||||
await context.Response.SendResponseAsync(_cachedServerList);
|
||||
}
|
||||
else
|
||||
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
|
||||
|
|
@ -57,8 +64,7 @@ namespace LightReflectiveMirror.Endpoints
|
|||
{
|
||||
if (Program.conf.EndpointServerList)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(_rooms);
|
||||
await context.Response.SendResponseAsync(json.Compress());
|
||||
await context.Response.SendResponseAsync(_cachedCompressedServerList);
|
||||
}
|
||||
else
|
||||
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
|
||||
|
|
|
|||
|
|
@ -110,11 +110,12 @@ namespace LightReflectiveMirror
|
|||
if (conf.UseEndpoint)
|
||||
{
|
||||
WriteLogMessage("\nStarting Endpoint Service... ", ConsoleColor.White, true);
|
||||
var endpoint = new EndpointServer();
|
||||
var endpointService = new EndpointServer();
|
||||
|
||||
if (endpoint.Start(conf.EndpointPort))
|
||||
if (endpointService.Start(conf.EndpointPort))
|
||||
{
|
||||
WriteLogMessage("OK", ConsoleColor.Green);
|
||||
Endpoint.RoomsModified();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Mirror;
|
||||
using LightReflectiveMirror.Endpoints;
|
||||
using Mirror;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
|
@ -12,7 +13,7 @@ namespace LightReflectiveMirror
|
|||
public static Transport transport;
|
||||
public static Program instance;
|
||||
public static Config conf;
|
||||
|
||||
|
||||
private RelayHandler _relay;
|
||||
private MethodInfo _awakeMethod;
|
||||
private MethodInfo _startMethod;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using LightReflectiveMirror.Endpoints;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
||||
|
|
@ -75,6 +76,7 @@ namespace LightReflectiveMirror
|
|||
|
||||
_sendBuffers.Return(sendJoinBuffer);
|
||||
|
||||
Endpoint.RoomsModified();
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
|
@ -86,6 +88,8 @@ namespace LightReflectiveMirror
|
|||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
|
||||
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
|
||||
_sendBuffers.Return(sendJoinBuffer);
|
||||
|
||||
Endpoint.RoomsModified();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -151,6 +155,8 @@ namespace LightReflectiveMirror
|
|||
|
||||
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||
_sendBuffers.Return(sendBuffer);
|
||||
|
||||
Endpoint.RoomsModified();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -174,6 +180,7 @@ namespace LightReflectiveMirror
|
|||
_sendBuffers.Return(sendBuffer);
|
||||
rooms[i].clients.Clear();
|
||||
rooms.RemoveAt(i);
|
||||
Endpoint.RoomsModified();
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
|
@ -191,6 +198,7 @@ namespace LightReflectiveMirror
|
|||
|
||||
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
|
||||
_sendBuffers.Return(sendBuffer);
|
||||
Endpoint.RoomsModified();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue