sync
This commit is contained in:
parent
49b85cf291
commit
a027273378
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cc.maxmc.msm.common.event
|
||||||
|
|
||||||
|
import cc.maxmc.msm.common.network.BungeePacket
|
||||||
|
import io.netty.channel.Channel
|
||||||
|
import net.md_5.bungee.api.plugin.Event
|
||||||
|
|
||||||
|
data class PacketReceiveEvent(val channel: Channel, val packet: BungeePacket) : Event()
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package cc.maxmc.msm.common.network
|
package cc.maxmc.msm.common.network.netty
|
||||||
|
|
||||||
import cc.maxmc.msm.common.network.netty.NetworkRegistry
|
import cc.maxmc.msm.common.network.BungeePacket
|
||||||
import io.netty.buffer.ByteBuf
|
import io.netty.buffer.ByteBuf
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import io.netty.handler.codec.ByteToMessageCodec
|
import io.netty.handler.codec.ByteToMessageCodec
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cc.maxmc.msm.common.utils
|
||||||
|
|
||||||
|
import cc.maxmc.msm.common.network.netty.ClusterMsgCodec
|
||||||
|
import cc.maxmc.msm.common.network.netty.NetworkRegistry
|
||||||
|
import io.netty.channel.Channel
|
||||||
|
import io.netty.channel.ChannelInitializer
|
||||||
|
import io.netty.channel.SimpleChannelInboundHandler
|
||||||
|
import io.netty.channel.socket.SocketChannel
|
||||||
|
import net.md_5.bungee.protocol.Varint21FrameDecoder
|
||||||
|
import net.md_5.bungee.protocol.Varint21LengthFieldPrepender
|
||||||
|
|
||||||
|
fun <C : Channel> channelInit(initializer: C.() -> Unit): ChannelInitializer<C> {
|
||||||
|
return object : ChannelInitializer<C>() {
|
||||||
|
override fun initChannel(ch: C) = initializer(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <I> pipelineInit(packetHandler: SimpleChannelInboundHandler<I>) = channelInit<SocketChannel> {
|
||||||
|
pipeline().addLast("frame_decoder", Varint21FrameDecoder())
|
||||||
|
pipeline().addLast("codec", ClusterMsgCodec(NetworkRegistry.PacketDirection.CHILD_BOUND))
|
||||||
|
pipeline().addLast("frame_prepender", Varint21LengthFieldPrepender())
|
||||||
|
pipeline().addLast("packet_handler", packetHandler)
|
||||||
|
}
|
||||||
|
|
@ -1,19 +1,23 @@
|
||||||
package cc.maxmc.msm.parent
|
package cc.maxmc.msm.parent
|
||||||
|
|
||||||
import cc.maxmc.msm.common.Placeholder
|
import cc.maxmc.msm.parent.command.Send
|
||||||
import cc.maxmc.msm.common.network.netty.NetworkRegistry
|
|
||||||
import command.Send
|
|
||||||
import net.md_5.bungee.api.ProxyServer
|
import net.md_5.bungee.api.ProxyServer
|
||||||
import net.md_5.bungee.api.plugin.Plugin
|
import net.md_5.bungee.api.plugin.Plugin
|
||||||
|
|
||||||
class MultiServerMan : Plugin() {
|
class MultiServerMan : Plugin() {
|
||||||
|
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
|
instance = this
|
||||||
ProxyServer.getInstance().pluginManager.registerCommand(this, Send)
|
ProxyServer.getInstance().pluginManager.registerCommand(this, Send)
|
||||||
Placeholder.inject(NetworkRegistry.PacketDirection.PARENT_BOUND)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDisable() {
|
override fun onDisable() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
lateinit var instance: MultiServerMan
|
||||||
|
private set
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
package command
|
package cc.maxmc.msm.parent.command
|
||||||
|
|
||||||
import net.md_5.bungee.api.CommandSender
|
import net.md_5.bungee.api.CommandSender
|
||||||
|
import net.md_5.bungee.api.chat.TextComponent
|
||||||
import net.md_5.bungee.api.plugin.Command
|
import net.md_5.bungee.api.plugin.Command
|
||||||
|
|
||||||
object Send : Command("debugP") {
|
object Send : Command("debugP") {
|
||||||
override fun execute(sender: CommandSender, args: Array<out String>) {
|
override fun execute(sender: CommandSender, args: Array<out String>) {
|
||||||
val content = args.joinToString(" ")
|
val content = args.joinToString(" ")
|
||||||
|
|
||||||
sender.sendMessage("Success.")
|
sender.sendMessage(TextComponent("Success."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cc.maxmc.msm.parent.netty
|
||||||
|
|
||||||
|
import cc.maxmc.msm.common.utils.pipelineInit
|
||||||
|
import cc.maxmc.msm.parent.settings.Settings
|
||||||
|
import io.netty.bootstrap.ServerBootstrap
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup
|
||||||
|
|
||||||
|
object NetManager {
|
||||||
|
val parentGroup = NioEventLoopGroup()
|
||||||
|
val childGroup = NioEventLoopGroup()
|
||||||
|
|
||||||
|
init {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startServer() {
|
||||||
|
ServerBootstrap()
|
||||||
|
.group(parentGroup, childGroup)
|
||||||
|
.childHandler(pipelineInit())
|
||||||
|
.bind(Settings.serverPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cc.maxmc.msm.parent.netty
|
||||||
|
|
||||||
|
object ParentPacketHandler {
|
||||||
|
}
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
package cc.maxmc.msm.parent.network
|
|
||||||
|
|
||||||
import cc.maxmc.msm.common.network.netty.NetworkRegistry
|
|
||||||
|
|
||||||
object NetManager {
|
|
||||||
private val current = NetworkRegistry.PacketDirection.PARENT_BOUND
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cc.maxmc.msm.parent.settings
|
||||||
|
|
||||||
|
import cc.maxmc.msm.parent.settings.SettingsReader.config
|
||||||
|
|
||||||
|
object Settings {
|
||||||
|
val name
|
||||||
|
get() = config
|
||||||
|
val serverPort
|
||||||
|
get() = config.getInt("server_port", 25566)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package cc.maxmc.msm.parent.settings
|
||||||
|
|
||||||
|
import cc.maxmc.msm.parent.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())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue