This commit is contained in:
tony_all
2023-03-15 13:34:01 +08:00
parent 49b85cf291
commit a027273378
19 changed files with 248 additions and 36 deletions
@@ -0,0 +1,25 @@
package cc.maxmc.msm.child
import cc.maxmc.msm.child.listener.PacketListener
import cc.maxmc.msm.child.netty.NetClient
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.plugin.Plugin
class MultiServerMan : Plugin() {
init {
instance = this
}
override fun onEnable() {
ProxyServer.getInstance().pluginManager.registerListener(this, PacketListener)
NetClient
}
override fun onDisable() {
NetClient.shutdown()
}
companion object {
lateinit var instance: MultiServerMan
}
}
@@ -0,0 +1,20 @@
package cc.maxmc.msm.child.listener
import cc.maxmc.msm.child.utils.log
import cc.maxmc.msm.common.event.PacketReceiveEvent
import cc.maxmc.msm.common.network.packet.CPacketDebug
import cc.maxmc.msm.common.network.packet.PPacketDebug
import net.md_5.bungee.api.plugin.Listener
import net.md_5.bungee.event.EventHandler
object PacketListener : Listener {
@EventHandler
fun onPacket(evt: PacketReceiveEvent) {
val packet = evt.packet
if (packet !is CPacketDebug) {
return
}
log("§fDEBUG | §7收到: \"${packet.content}\"")
evt.channel.writeAndFlush(PPacketDebug("(${evt.channel.localAddress()}) - ${packet.content}"))
}
}
@@ -0,0 +1,13 @@
package cc.maxmc.msm.child.netty
import cc.maxmc.msm.common.event.PacketReceiveEvent
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
object ClientPacketHandler : SimpleChannelInboundHandler<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
PacketReceiveEvent(ctx.channel(), msg).callEvent()
}
}
@@ -0,0 +1,33 @@
package cc.maxmc.msm.child.netty
import cc.maxmc.msm.child.settings.Settings
import cc.maxmc.msm.child.utils.log
import cc.maxmc.msm.common.utils.pipelineInit
import io.netty.bootstrap.Bootstrap
import io.netty.channel.ChannelFutureListener
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
object NetClient {
private val loop = NioEventLoopGroup()
init {
start()
}
private fun start() {
val parent = Settings.Parent
Bootstrap()
.group(loop)
.handler(pipelineInit(ClientPacketHandler))
.option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(parent.address, parent.port)
.connect().addListener(ChannelFutureListener {
log("§a| §7成功连接到集群的主节点. (${it.channel().remoteAddress()})")
})
}
fun shutdown() {
loop.shutdownGracefully().sync()
}
}
@@ -0,0 +1,15 @@
package cc.maxmc.msm.child.settings
import cc.maxmc.msm.child.settings.SettingsReader.config
object Settings {
val name
get() = config
object Parent {
val address: String
get() = config.getString("parent.address", "localhost")
val port: Int
get() = config.getInt("parent.port", 23333)
}
}
@@ -0,0 +1,25 @@
package cc.maxmc.msm.child.settings
import cc.maxmc.msm.child.MultiServerMan
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.chat.TextComponent
import net.md_5.bungee.config.Configuration
import net.md_5.bungee.config.ConfigurationProvider
import net.md_5.bungee.config.YamlConfiguration
import kotlin.io.path.*
object SettingsReader {
private val file = MultiServerMan.instance.dataFolder.toPath().resolve("settings.yml")
val config: Configuration
init {
if (!file.exists()) {
MultiServerMan.instance.dataFolder.toPath().createDirectories()
val stream = MultiServerMan.instance.getResourceAsStream("settings.yml")
file.createFile()
stream.copyTo(file.outputStream())
ProxyServer.getInstance().console.sendMessage(TextComponent("§b| §7配置文件不存在,正在创建配置文件。"))
}
config = ConfigurationProvider.getProvider(YamlConfiguration::class.java).load(file.inputStream())
}
}
@@ -0,0 +1,8 @@
package cc.maxmc.msm.child.utils
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.chat.TextComponent
fun log(msg: String) {
ProxyServer.getInstance().console.sendMessage(TextComponent(msg))
}
@@ -1,18 +0,0 @@
package cc.maxmc.msm.child
import cc.maxmc.msm.common.Placeholder
import cc.maxmc.msm.common.network.netty.NetworkRegistry
import net.md_5.bungee.api.plugin.Plugin
class MultiServerMan : Plugin() {
override fun onEnable() {
Placeholder.inject(NetworkRegistry.PacketDirection.CHILD_BOUND)
}
override fun onDisable() {
}
}
@@ -0,0 +1,3 @@
parent:
address: 127.0.0.1
port: 23333