This commit is contained in:
tony_all 2023-04-16 19:13:27 +08:00
parent fdaa4a57eb
commit 4b65a2459b
7 changed files with 31 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import java.util.UUID;
@SuppressWarnings("unused") // API
public class ServerInfo {
public static final UUID NULL_UID = UUID.fromString("00000000-0000-0000-0000-000000000000");
@NotNull
private final UUID uid;
@Nullable
@ -15,7 +16,7 @@ public class ServerInfo {
private boolean available;
public ServerInfo() {
uid = UUID.fromString("00000000-0000-0000-0000-000000000000");
uid = NULL_UID;
server = null;
}
@ -47,7 +48,7 @@ public class ServerInfo {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServerInfo that = (ServerInfo) o;
return Objects.equals(server, that.server) && Objects.equals(uid, that.uid);
return Objects.equals(uid, that.uid);
}
@Override

View File

@ -37,9 +37,12 @@ object ProtocolListener : Listener {
val subServer = SubServer(serverInfo.uid, packet.type, serverInfo.server!!.split(":")[1].toInt())
serverCache[serverInfo.uid] = subServer
pluginScope.launch {
subServer.initServer()
subServer.startServer()
NetClient.sendPacket(PPacketServerStarted(serverInfo))
try {
subServer.initServer()
subServer.startServer()
} catch (e: Exception) {
NetClient.sendPacket(PPacketServerStarted())
}
}
}

View File

@ -1,6 +1,7 @@
package cc.maxmc.msm.child.misc
import cc.maxmc.msm.child.MultiServerMan
import cc.maxmc.msm.child.settings.Settings
import cc.maxmc.msm.child.utils.ScriptRunner
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@ -27,7 +28,7 @@ class SubServer(
}
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 \"?\"") {
it.resume(Unit)
}

View File

@ -17,4 +17,11 @@ object Settings {
val port: Int
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")
}
}
}

View File

@ -8,6 +8,7 @@ import cc.maxmc.msm.common.utils.pluginScope
import cc.maxmc.msm.parent.misc.ChildBungee
import io.netty.channel.Channel
import kotlinx.coroutines.launch
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
object ChildManager {
@ -32,9 +33,10 @@ object ChildManager {
}
fun requestChild(type: String): ChildBungee {
val uid = UUID.randomUUID()
children.forEach {
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) }
.maxByOrNull { it.getAvailablePorts().size } ?: throw IllegalStateException("当前无可用端口开启新服务器.")

View File

@ -35,11 +35,9 @@ object ServerManager {
if (!serverMap.containsKey(it)) {
serverMap[it] = ArrayList()
}
if (serverMap[it]!!.size <= 2) {
pluginScope.launch {
repeat(2) { _ ->
requestServer(it)
}
if (serverMap[it]!!.size < 2) {
repeat(2) { _ ->
requestServer(it)
}
}
}
@ -82,6 +80,8 @@ object ServerManager {
val child = ChildManager.requestChild(type)
val list = serverMap[type]!!
val server = child.requestServer(type)
if (server.uid == ServerInfo.NULL_UID)
list.add(server)
childMap[server] = child
return server

View File

@ -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.PPacketServerStarted
import cc.maxmc.msm.common.utils.awaitPacket
import cc.maxmc.msm.common.utils.log
import io.netty.channel.Channel
import java.net.InetSocketAddress
import java.util.*
@ -28,6 +29,10 @@ class ChildBungee(
val packet = awaitPacket(PPacketServerStarted::class.java) { ch, packet ->
ch == channel && packet.serverInfo == server
}
if (packet.serverInfo.server == null) {
log("§c| §7服务器启动失败请检查子BC端 §c(${channel.remoteAddress()})§7 日志。")
return ServerInfo()
}
usedPorts += port
return packet.serverInfo
}