Server list Endpoint Caching (Thanks FakeByte! :))

This commit is contained in:
Derek S 2021-04-05 23:30:35 -05:00
parent 046cfe3d4f
commit 35bd75bc05
5 changed files with 29 additions and 13 deletions

View file

@ -48,7 +48,7 @@ namespace LightReflectiveMirror.LoadBalancing
WriteLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red); WriteLogMessage("Endpoint server started unsuccessfully", ConsoleColor.Red);
} }
var pingThread = new Thread(new ThreadStart(() => PingServers())); var pingThread = new Thread(new ThreadStart(PingServers));
pingThread.Start(); pingThread.Start();
// keep console alive // keep console alive
@ -99,7 +99,7 @@ namespace LightReflectiveMirror.LoadBalancing
} }
} }
async Task PingServers() async void PingServers()
{ {
while (true) while (true)
{ {

View file

@ -23,7 +23,9 @@ namespace LightReflectiveMirror.Endpoints
[RestResource] [RestResource]
public class Endpoint 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 private RelayStats _stats { get => new RelayStats
{ {
@ -33,6 +35,12 @@ namespace LightReflectiveMirror.Endpoints
Uptime = Program.instance.GetUptime() Uptime = Program.instance.GetUptime()
}; } }; }
public static void RoomsModified()
{
_cachedServerList = JsonConvert.SerializeObject(_rooms, Formatting.Indented);
_cachedCompressedServerList = _cachedServerList.Compress();
}
[RestRoute("Get", "/api/stats")] [RestRoute("Get", "/api/stats")]
public async Task Stats(IHttpContext context) public async Task Stats(IHttpContext context)
{ {
@ -45,8 +53,7 @@ namespace LightReflectiveMirror.Endpoints
{ {
if (Program.conf.EndpointServerList) if (Program.conf.EndpointServerList)
{ {
string json = JsonConvert.SerializeObject(_rooms, Formatting.Indented); await context.Response.SendResponseAsync(_cachedServerList);
await context.Response.SendResponseAsync(json);
} }
else else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden); await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);
@ -57,8 +64,7 @@ namespace LightReflectiveMirror.Endpoints
{ {
if (Program.conf.EndpointServerList) if (Program.conf.EndpointServerList)
{ {
string json = JsonConvert.SerializeObject(_rooms); await context.Response.SendResponseAsync(_cachedCompressedServerList);
await context.Response.SendResponseAsync(json.Compress());
} }
else else
await context.Response.SendResponseAsync(HttpStatusCode.Forbidden); await context.Response.SendResponseAsync(HttpStatusCode.Forbidden);

View file

@ -110,11 +110,12 @@ namespace LightReflectiveMirror
if (conf.UseEndpoint) if (conf.UseEndpoint)
{ {
WriteLogMessage("\nStarting Endpoint Service... ", ConsoleColor.White, true); 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); WriteLogMessage("OK", ConsoleColor.Green);
Endpoint.RoomsModified();
} }
else else
{ {

View file

@ -1,4 +1,5 @@
using Mirror; using LightReflectiveMirror.Endpoints;
using Mirror;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;

View file

@ -1,4 +1,5 @@
using System; using LightReflectiveMirror.Endpoints;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
@ -75,6 +76,7 @@ namespace LightReflectiveMirror
_sendBuffers.Return(sendJoinBuffer); _sendBuffers.Return(sendJoinBuffer);
Endpoint.RoomsModified();
return; return;
} }
else else
@ -86,6 +88,8 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos)); Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos)); Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendJoinBuffer, 0, sendJoinPos));
_sendBuffers.Return(sendJoinBuffer); _sendBuffers.Return(sendJoinBuffer);
Endpoint.RoomsModified();
return; return;
} }
} }
@ -151,6 +155,8 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos)); Program.transport.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
_sendBuffers.Return(sendBuffer); _sendBuffers.Return(sendBuffer);
Endpoint.RoomsModified();
} }
/// <summary> /// <summary>
@ -174,6 +180,7 @@ namespace LightReflectiveMirror
_sendBuffers.Return(sendBuffer); _sendBuffers.Return(sendBuffer);
rooms[i].clients.Clear(); rooms[i].clients.Clear();
rooms.RemoveAt(i); rooms.RemoveAt(i);
Endpoint.RoomsModified();
return; return;
} }
else else
@ -191,6 +198,7 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos)); Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
_sendBuffers.Return(sendBuffer); _sendBuffers.Return(sendBuffer);
Endpoint.RoomsModified();
} }
} }
} }