This commit is contained in:
TONY_All 2023-03-15 18:56:35 +08:00
parent a027273378
commit 8c41f190c4
12 changed files with 65 additions and 33 deletions

View File

@ -1,6 +1,6 @@
package cc.maxmc.msm.child
import cc.maxmc.msm.child.listener.PacketListener
import cc.maxmc.msm.child.command.Send
import cc.maxmc.msm.child.netty.NetClient
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.plugin.Plugin
@ -11,8 +11,8 @@ class MultiServerMan : Plugin() {
}
override fun onEnable() {
ProxyServer.getInstance().pluginManager.registerListener(this, PacketListener)
NetClient
ProxyServer.getInstance().pluginManager.registerCommand(this, Send)
NetClient.start()
}
override fun onDisable() {

View File

@ -1,5 +1,7 @@
package cc.maxmc.msm.parent.command
package cc.maxmc.msm.child.command
import cc.maxmc.msm.child.netty.NetClient
import cc.maxmc.msm.common.network.packet.PPacketDebug
import net.md_5.bungee.api.CommandSender
import net.md_5.bungee.api.chat.TextComponent
import net.md_5.bungee.api.plugin.Command
@ -7,7 +9,8 @@ import net.md_5.bungee.api.plugin.Command
object Send : Command("debugP") {
override fun execute(sender: CommandSender, args: Array<out String>) {
val content = args.joinToString(" ")
val packet = PPacketDebug(content)
NetClient.sendPacket(packet)
sender.sendMessage(TextComponent("Success."))
}
}

View File

@ -1,32 +1,39 @@
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.network.BungeePacket
import cc.maxmc.msm.common.utils.log
import cc.maxmc.msm.common.utils.pipelineInit
import io.netty.bootstrap.Bootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelFutureListener
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioSocketChannel
object NetClient {
private val loop = NioEventLoopGroup()
lateinit var channel: Channel
private set
init {
start()
}
private fun start() {
fun start() {
val parent = Settings.Parent
Bootstrap()
.channel(NioSocketChannel::class.java)
.group(loop)
.handler(pipelineInit(ClientPacketHandler))
.handler(pipelineInit())
.option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(parent.address, parent.port)
.connect().addListener(ChannelFutureListener {
channel = it.channel()
log("§a| §7成功连接到集群的主节点. (${it.channel().remoteAddress()})")
})
}
fun sendPacket(packet: BungeePacket) {
channel.writeAndFlush(packet)
}
fun shutdown() {
loop.shutdownGracefully().sync()
}

View File

@ -1,13 +1,11 @@
package cc.maxmc.msm.child.netty
package cc.maxmc.msm.common.network
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>() {
object ClusterPacketHandler : SimpleChannelInboundHandler<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
PacketReceiveEvent(ctx.channel(), msg).callEvent()
}
}

View File

@ -1,4 +1,4 @@
package cc.maxmc.msm.child.utils
package cc.maxmc.msm.common.utils
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.chat.TextComponent

View File

@ -1,10 +1,10 @@
package cc.maxmc.msm.common.utils
import cc.maxmc.msm.common.network.ClusterPacketHandler
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
@ -15,9 +15,9 @@ fun <C : Channel> channelInit(initializer: C.() -> Unit): ChannelInitializer<C>
}
}
fun <I> pipelineInit(packetHandler: SimpleChannelInboundHandler<I>) = channelInit<SocketChannel> {
fun pipelineInit() = 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)
pipeline().addLast("packet_handler", ClusterPacketHandler)
}

View File

@ -1,6 +1,7 @@
package cc.maxmc.msm.parent
import cc.maxmc.msm.parent.command.Send
import cc.maxmc.msm.parent.listener.PacketListener
import cc.maxmc.msm.parent.netty.NetManager
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.plugin.Plugin
@ -8,11 +9,12 @@ class MultiServerMan : Plugin() {
override fun onEnable() {
instance = this
ProxyServer.getInstance().pluginManager.registerCommand(this, Send)
ProxyServer.getInstance().pluginManager.registerListener(this, PacketListener)
NetManager.startServer()
}
override fun onDisable() {
NetManager.shutdownServer()
}
companion object {

View File

@ -1,9 +1,9 @@
package cc.maxmc.msm.child.listener
package cc.maxmc.msm.parent.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 cc.maxmc.msm.common.utils.log
import net.md_5.bungee.api.plugin.Listener
import net.md_5.bungee.event.EventHandler
@ -11,10 +11,10 @@ object PacketListener : Listener {
@EventHandler
fun onPacket(evt: PacketReceiveEvent) {
val packet = evt.packet
if (packet !is CPacketDebug) {
if (packet !is PPacketDebug) {
return
}
log("§fDEBUG | §7收到: \"${packet.content}\"")
evt.channel.writeAndFlush(PPacketDebug("(${evt.channel.localAddress()}) - ${packet.content}"))
evt.channel.writeAndFlush(CPacketDebug("(${evt.channel.localAddress()}) - ${packet.content}"))
}
}

View File

@ -1,3 +1,5 @@
package cc.maxmc.msm.parent.misc
data class ChildBungee(val addr: String)
import io.netty.channel.Channel
data class ChildBungee(val channel: Channel)

View File

@ -1,23 +1,31 @@
package cc.maxmc.msm.parent.netty
import cc.maxmc.msm.common.utils.log
import cc.maxmc.msm.common.utils.pipelineInit
import cc.maxmc.msm.parent.settings.Settings
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelFutureListener
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
object NetManager {
val parentGroup = NioEventLoopGroup()
val childGroup = NioEventLoopGroup()
init {
}
fun startServer() {
ServerBootstrap()
.channel(NioServerSocketChannel::class.java)
.group(parentGroup, childGroup)
.childHandler(pipelineInit())
.bind(Settings.serverPort)
.addListener(ChannelFutureListener {
log("§a| §7集群主服务端启动成功. ${it.channel().localAddress()}")
})
}
fun shutdownServer() {
parentGroup.shutdownGracefully().sync()
childGroup.shutdownGracefully().sync()
}
}

View File

@ -1,4 +1,13 @@
package cc.maxmc.msm.parent.netty
object ParentPacketHandler {
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 ParentPacketHandler : SimpleChannelInboundHandler<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
PacketReceiveEvent(ctx.channel(), msg).callEvent()
}
}

View File

@ -0,0 +1,3 @@
parent:
address: 127.0.0.1
port: 23333