sync
This commit is contained in:
parent
fdaa4a57eb
commit
4b65a2459b
|
|
@ -8,6 +8,7 @@ import java.util.UUID;
|
||||||
|
|
||||||
@SuppressWarnings("unused") // API
|
@SuppressWarnings("unused") // API
|
||||||
public class ServerInfo {
|
public class ServerInfo {
|
||||||
|
public static final UUID NULL_UID = UUID.fromString("00000000-0000-0000-0000-000000000000");
|
||||||
@NotNull
|
@NotNull
|
||||||
private final UUID uid;
|
private final UUID uid;
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -15,7 +16,7 @@ public class ServerInfo {
|
||||||
private boolean available;
|
private boolean available;
|
||||||
|
|
||||||
public ServerInfo() {
|
public ServerInfo() {
|
||||||
uid = UUID.fromString("00000000-0000-0000-0000-000000000000");
|
uid = NULL_UID;
|
||||||
server = null;
|
server = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,7 +48,7 @@ public class ServerInfo {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
ServerInfo that = (ServerInfo) o;
|
ServerInfo that = (ServerInfo) o;
|
||||||
return Objects.equals(server, that.server) && Objects.equals(uid, that.uid);
|
return Objects.equals(uid, that.uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -37,9 +37,12 @@ object ProtocolListener : Listener {
|
||||||
val subServer = SubServer(serverInfo.uid, packet.type, serverInfo.server!!.split(":")[1].toInt())
|
val subServer = SubServer(serverInfo.uid, packet.type, serverInfo.server!!.split(":")[1].toInt())
|
||||||
serverCache[serverInfo.uid] = subServer
|
serverCache[serverInfo.uid] = subServer
|
||||||
pluginScope.launch {
|
pluginScope.launch {
|
||||||
|
try {
|
||||||
subServer.initServer()
|
subServer.initServer()
|
||||||
subServer.startServer()
|
subServer.startServer()
|
||||||
NetClient.sendPacket(PPacketServerStarted(serverInfo))
|
} catch (e: Exception) {
|
||||||
|
NetClient.sendPacket(PPacketServerStarted())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package cc.maxmc.msm.child.misc
|
package cc.maxmc.msm.child.misc
|
||||||
|
|
||||||
import cc.maxmc.msm.child.MultiServerMan
|
import cc.maxmc.msm.child.MultiServerMan
|
||||||
|
import cc.maxmc.msm.child.settings.Settings
|
||||||
import cc.maxmc.msm.child.utils.ScriptRunner
|
import cc.maxmc.msm.child.utils.ScriptRunner
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
@ -27,7 +28,7 @@ class SubServer(
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startServer() = suspendCoroutine {
|
suspend fun startServer() = suspendCoroutine {
|
||||||
runner.launch(port.toString())
|
runner.launch(Settings.Patterns.arguments(type).joinToString(" ").replace("\${port}", port.toString()))
|
||||||
runner.onOutput("For help, type \"help\" or \"?\"") {
|
runner.onOutput("For help, type \"help\" or \"?\"") {
|
||||||
it.resume(Unit)
|
it.resume(Unit)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,11 @@ object Settings {
|
||||||
val port: Int
|
val port: Int
|
||||||
get() = config.getInt("parent.port", 23333)
|
get() = config.getInt("parent.port", 23333)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object Patterns {
|
||||||
|
fun arguments(type: String): List<String> {
|
||||||
|
if (!config.contains("patterns.$type.args")) throw IllegalArgumentException("服务端样板 $type 的配置不存在,请检查settings.yml")
|
||||||
|
return config.getStringList("patterns.$type.args")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ import cc.maxmc.msm.common.utils.pluginScope
|
||||||
import cc.maxmc.msm.parent.misc.ChildBungee
|
import cc.maxmc.msm.parent.misc.ChildBungee
|
||||||
import io.netty.channel.Channel
|
import io.netty.channel.Channel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.util.*
|
||||||
import java.util.concurrent.CopyOnWriteArrayList
|
import java.util.concurrent.CopyOnWriteArrayList
|
||||||
|
|
||||||
object ChildManager {
|
object ChildManager {
|
||||||
|
|
@ -32,9 +33,10 @@ object ChildManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestChild(type: String): ChildBungee {
|
fun requestChild(type: String): ChildBungee {
|
||||||
|
val uid = UUID.randomUUID()
|
||||||
children.forEach {
|
children.forEach {
|
||||||
val ports = it.getAvailablePorts()
|
val ports = it.getAvailablePorts()
|
||||||
log("${ports.size} ports: $ports")
|
log("[${uid.toString().substring(0..8)}] ${it.channel.remoteAddress()} has ${ports.size} ports: $ports")
|
||||||
}
|
}
|
||||||
return children.filter { it.getAvailablePorts().isNotEmpty() && it.types.contains(type) }
|
return children.filter { it.getAvailablePorts().isNotEmpty() && it.types.contains(type) }
|
||||||
.maxByOrNull { it.getAvailablePorts().size } ?: throw IllegalStateException("当前无可用端口开启新服务器.")
|
.maxByOrNull { it.getAvailablePorts().size } ?: throw IllegalStateException("当前无可用端口开启新服务器.")
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,7 @@ object ServerManager {
|
||||||
if (!serverMap.containsKey(it)) {
|
if (!serverMap.containsKey(it)) {
|
||||||
serverMap[it] = ArrayList()
|
serverMap[it] = ArrayList()
|
||||||
}
|
}
|
||||||
if (serverMap[it]!!.size <= 2) {
|
if (serverMap[it]!!.size < 2) {
|
||||||
pluginScope.launch {
|
|
||||||
repeat(2) { _ ->
|
repeat(2) { _ ->
|
||||||
requestServer(it)
|
requestServer(it)
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +43,6 @@ object ServerManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun consumeServer(type: String): ServerInfo {
|
fun consumeServer(type: String): ServerInfo {
|
||||||
val servers = getAvailableServers(type)
|
val servers = getAvailableServers(type)
|
||||||
|
|
@ -82,6 +80,8 @@ object ServerManager {
|
||||||
val child = ChildManager.requestChild(type)
|
val child = ChildManager.requestChild(type)
|
||||||
val list = serverMap[type]!!
|
val list = serverMap[type]!!
|
||||||
val server = child.requestServer(type)
|
val server = child.requestServer(type)
|
||||||
|
if (server.uid == ServerInfo.NULL_UID)
|
||||||
|
|
||||||
list.add(server)
|
list.add(server)
|
||||||
childMap[server] = child
|
childMap[server] = child
|
||||||
return server
|
return server
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import cc.maxmc.msm.common.network.BungeePacket
|
||||||
import cc.maxmc.msm.common.network.packet.CPacketRequestServer
|
import cc.maxmc.msm.common.network.packet.CPacketRequestServer
|
||||||
import cc.maxmc.msm.common.network.packet.PPacketServerStarted
|
import cc.maxmc.msm.common.network.packet.PPacketServerStarted
|
||||||
import cc.maxmc.msm.common.utils.awaitPacket
|
import cc.maxmc.msm.common.utils.awaitPacket
|
||||||
|
import cc.maxmc.msm.common.utils.log
|
||||||
import io.netty.channel.Channel
|
import io.netty.channel.Channel
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
@ -28,6 +29,10 @@ class ChildBungee(
|
||||||
val packet = awaitPacket(PPacketServerStarted::class.java) { ch, packet ->
|
val packet = awaitPacket(PPacketServerStarted::class.java) { ch, packet ->
|
||||||
ch == channel && packet.serverInfo == server
|
ch == channel && packet.serverInfo == server
|
||||||
}
|
}
|
||||||
|
if (packet.serverInfo.server == null) {
|
||||||
|
log("§c| §7服务器启动失败,请检查子BC端 §c(${channel.remoteAddress()})§7 日志。")
|
||||||
|
return ServerInfo()
|
||||||
|
}
|
||||||
usedPorts += port
|
usedPorts += port
|
||||||
return packet.serverInfo
|
return packet.serverInfo
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue