mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-14 06:14:45 +01:00
Cache regionlist and update config structure for servers
This commit is contained in:
@@ -14,8 +14,8 @@ public class Config {
|
||||
|
||||
public KeystoreInfo keystore = new KeystoreInfo();
|
||||
|
||||
public ServerConfig httpServer = new ServerConfig("127.0.0.1", 443);
|
||||
public GameServerConfig gameServer = new GameServerConfig("127.0.0.1", 23301);
|
||||
public HttpServerConfig httpServer = new HttpServerConfig(443);
|
||||
public GameServerConfig gameServer = new GameServerConfig(23301);
|
||||
|
||||
public ServerOptions serverOptions = new ServerOptions();
|
||||
public LogOptions logOptions = new LogOptions();
|
||||
@@ -45,17 +45,25 @@ public class Config {
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class ServerConfig {
|
||||
private static class ServerConfig {
|
||||
public String bindAddress = "0.0.0.0";
|
||||
public String publicAddress = "127.0.0.1";
|
||||
public int port;
|
||||
public boolean useSSL = true;
|
||||
|
||||
public ServerConfig(String address, int port) {
|
||||
this.publicAddress = address;
|
||||
public ServerConfig(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class HttpServerConfig extends ServerConfig {
|
||||
public boolean useSSL = true;
|
||||
public long regionListRefresh = 60_000; // Time in milliseconds to wait before refreshing region list cache again
|
||||
|
||||
public HttpServerConfig(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
public String getDisplayAddress() {
|
||||
return (useSSL ? "https" : "http") + "://" + publicAddress + ":" + port;
|
||||
}
|
||||
@@ -67,8 +75,8 @@ public class Config {
|
||||
public String name = "Test";
|
||||
public String description = "Test Server";
|
||||
|
||||
public GameServerConfig(String address, int port) {
|
||||
super(address, port);
|
||||
public GameServerConfig(int port) {
|
||||
super(port);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ public class RegionInfo {
|
||||
private String desc;
|
||||
|
||||
private String gateAddress;
|
||||
private String gameAddress;
|
||||
|
||||
@Setter private boolean up;
|
||||
|
||||
@@ -29,7 +28,6 @@ public class RegionInfo {
|
||||
this.name = server.getServerConfig().getName();
|
||||
this.desc = server.getServerConfig().getDescription();
|
||||
this.gateAddress = LunarRail.getHttpServer().getServerConfig().getDisplayAddress();
|
||||
this.gameAddress = server.getServerConfig().getDisplayAddress();
|
||||
this.up = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package emu.lunarcore.server.http;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.eclipse.jetty.server.HttpConfiguration;
|
||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||
@@ -11,11 +10,13 @@ import org.eclipse.jetty.server.SecureRequestCustomizer;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
|
||||
import emu.lunarcore.Config.ServerConfig;
|
||||
import emu.lunarcore.Config.HttpServerConfig;
|
||||
import emu.lunarcore.LunarRail;
|
||||
import emu.lunarcore.LunarRail.ServerType;
|
||||
import emu.lunarcore.proto.DispatchRegionDataOuterClass.DispatchRegionData;
|
||||
import emu.lunarcore.server.game.RegionInfo;
|
||||
import emu.lunarcore.server.http.handlers.*;
|
||||
import emu.lunarcore.util.Utils;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
@@ -31,6 +32,7 @@ public class HttpServer {
|
||||
|
||||
private long nextRegionUpdate;
|
||||
private Object2ObjectMap<String, RegionInfo> regions;
|
||||
private String regionList;
|
||||
|
||||
public HttpServer(ServerType type) {
|
||||
this.type = type;
|
||||
@@ -49,7 +51,7 @@ public class HttpServer {
|
||||
return type;
|
||||
}
|
||||
|
||||
public ServerConfig getServerConfig() {
|
||||
public HttpServerConfig getServerConfig() {
|
||||
return LunarRail.getConfig().getHttpServer();
|
||||
}
|
||||
|
||||
@@ -74,21 +76,30 @@ public class HttpServer {
|
||||
this.nextRegionUpdate = 0;
|
||||
}
|
||||
|
||||
public Object2ObjectMap<String, RegionInfo> getRegions() {
|
||||
public String getRegionList() {
|
||||
synchronized (this.regions) {
|
||||
if (System.currentTimeMillis() > this.nextRegionUpdate) {
|
||||
// Check if region list needs to be cached
|
||||
if (System.currentTimeMillis() > this.nextRegionUpdate || this.regionList == null) {
|
||||
// Clear regions first
|
||||
this.regions.clear();
|
||||
|
||||
// Pull region infos from database
|
||||
LunarRail.getAccountDatabase().getObjects(RegionInfo.class)
|
||||
.forEach(region -> {
|
||||
this.regions.put(region.getId(), region);
|
||||
});
|
||||
|
||||
// Serialize to proto
|
||||
DispatchRegionData regionData = DispatchRegionData.newInstance();
|
||||
regions.values().stream().map(RegionInfo::toProto).forEach(regionData::addRegionList);
|
||||
|
||||
this.nextRegionUpdate = System.currentTimeMillis() + 60_000;
|
||||
// Set region list cache
|
||||
this.regionList = Utils.base64Encode(regionData.toByteArray());
|
||||
this.nextRegionUpdate = System.currentTimeMillis() + getServerConfig().regionListRefresh;
|
||||
}
|
||||
|
||||
return regions;
|
||||
}
|
||||
|
||||
return regionList;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
|
||||
@@ -3,11 +3,8 @@ package emu.lunarcore.server.http.handlers;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import emu.lunarcore.LunarRail;
|
||||
import emu.lunarcore.proto.DispatchRegionDataOuterClass.DispatchRegionData;
|
||||
import emu.lunarcore.proto.RegionEntryOuterClass.RegionEntry;
|
||||
import emu.lunarcore.server.game.RegionInfo;
|
||||
import emu.lunarcore.server.http.HttpServer;
|
||||
import emu.lunarcore.util.Utils;
|
||||
|
||||
import io.javalin.http.Context;
|
||||
import io.javalin.http.Handler;
|
||||
|
||||
@@ -25,18 +22,8 @@ public class QueryDispatchHandler implements Handler {
|
||||
LunarRail.getLogger().info("Client request: query_dispatch");
|
||||
}
|
||||
|
||||
// Build region list
|
||||
DispatchRegionData regions = DispatchRegionData.newInstance();
|
||||
|
||||
// Get regions
|
||||
var regionMap = server.getRegions();
|
||||
|
||||
synchronized (regionMap) {
|
||||
regionMap.values().stream().map(RegionInfo::toProto).forEach(regions::addRegionList);
|
||||
}
|
||||
|
||||
// Encode to base64 and send to client
|
||||
ctx.result(Utils.base64Encode(regions.toByteArray()));
|
||||
// Send region list to client
|
||||
ctx.result(server.getRegionList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user