From 2abf1bb7df24eba01b5976380b847d8672fa8400 Mon Sep 17 00:00:00 2001 From: tony_all Date: Thu, 16 Mar 2023 00:06:48 +0800 Subject: [PATCH] fix Packet --- child-side/build.gradle.kts | 3 ++- .../cc/maxmc/msm/child/MultiServerMan.kt | 5 ++++- .../cc/maxmc/msm/child/netty/NetClient.kt | 16 +++++++-------- child-side/src/main/resources/bungee.yml | 2 +- .../common/network/ClusterPacketHandler.kt | 2 ++ .../common/network/netty/ClusterMsgCodec.kt | 4 +++- .../common/network/netty/NetworkRegistry.kt | 20 +++++++++++-------- .../msm/common/network/packet/CPacketDebug.kt | 2 +- .../msm/common/network/packet/PPacketDebug.kt | 5 ++++- .../cc/maxmc/msm/common/utils/Logging.kt | 8 ++++++++ .../kotlin/cc/maxmc/msm/common/utils/Netty.kt | 7 ++++--- .../cc/maxmc/msm/parent/netty/NetManager.kt | 7 ++++--- .../msm/parent/netty/ParentPacketHandler.kt | 13 ------------ 13 files changed, 52 insertions(+), 42 deletions(-) delete mode 100644 parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/ParentPacketHandler.kt diff --git a/child-side/build.gradle.kts b/child-side/build.gradle.kts index 2fe8a39..2cc32fd 100644 --- a/child-side/build.gradle.kts +++ b/child-side/build.gradle.kts @@ -14,8 +14,9 @@ dependencies { implementation(kotlin("stdlib")) implementation(project(":api")) implementation(project(":common")) + implementation("io.netty:netty-all:4.1.90.Final") @Suppress("VulnerableLibrariesLocal") - compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT") + implementation("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT") } tasks.shadowJar { diff --git a/child-side/src/main/kotlin/cc/maxmc/msm/child/MultiServerMan.kt b/child-side/src/main/kotlin/cc/maxmc/msm/child/MultiServerMan.kt index 7a506c5..e573d6c 100644 --- a/child-side/src/main/kotlin/cc/maxmc/msm/child/MultiServerMan.kt +++ b/child-side/src/main/kotlin/cc/maxmc/msm/child/MultiServerMan.kt @@ -2,6 +2,8 @@ package cc.maxmc.msm.child import cc.maxmc.msm.child.command.Send import cc.maxmc.msm.child.netty.NetClient +import cc.maxmc.msm.child.settings.Settings +import cc.maxmc.msm.common.network.netty.NetworkRegistry import net.md_5.bungee.api.ProxyServer import net.md_5.bungee.api.plugin.Plugin @@ -12,7 +14,8 @@ class MultiServerMan : Plugin() { override fun onEnable() { ProxyServer.getInstance().pluginManager.registerCommand(this, Send) - NetClient.start() + NetworkRegistry + NetClient.start(Settings.Parent.address, Settings.Parent.port) } override fun onDisable() { diff --git a/child-side/src/main/kotlin/cc/maxmc/msm/child/netty/NetClient.kt b/child-side/src/main/kotlin/cc/maxmc/msm/child/netty/NetClient.kt index ebd8bd6..9641ac7 100644 --- a/child-side/src/main/kotlin/cc/maxmc/msm/child/netty/NetClient.kt +++ b/child-side/src/main/kotlin/cc/maxmc/msm/child/netty/NetClient.kt @@ -1,8 +1,7 @@ package cc.maxmc.msm.child.netty -import cc.maxmc.msm.child.settings.Settings import cc.maxmc.msm.common.network.BungeePacket -import cc.maxmc.msm.common.utils.log +import cc.maxmc.msm.common.network.netty.NetworkRegistry import cc.maxmc.msm.common.utils.pipelineInit import io.netty.bootstrap.Bootstrap import io.netty.channel.Channel @@ -16,18 +15,17 @@ object NetClient { lateinit var channel: Channel private set - fun start() { - val parent = Settings.Parent - Bootstrap() + fun start(address: String, port: Int) { + val future = Bootstrap() .channel(NioSocketChannel::class.java) .group(loop) - .handler(pipelineInit()) + .handler(pipelineInit(NetworkRegistry.PacketDirection.CHILD_BOUND)) .option(ChannelOption.TCP_NODELAY, true) - .remoteAddress(parent.address, parent.port) + .remoteAddress(address, port) .connect().addListener(ChannelFutureListener { - channel = it.channel() - log("§a| §7成功连接到集群的主节点. (${it.channel().remoteAddress()})") + println("§a| §7成功连接到集群的主节点. (${it.channel().remoteAddress()})") }) + channel = future.channel() } fun sendPacket(packet: BungeePacket) { diff --git a/child-side/src/main/resources/bungee.yml b/child-side/src/main/resources/bungee.yml index be19e8a..55122a2 100644 --- a/child-side/src/main/resources/bungee.yml +++ b/child-side/src/main/resources/bungee.yml @@ -1,3 +1,3 @@ -name: MultiServerMan-Parent +name: MultiServerMan-Child main: cc.maxmc.msm.child.MultiServerMan author: TONY_All \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterPacketHandler.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterPacketHandler.kt index b19c394..e79faf2 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterPacketHandler.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterPacketHandler.kt @@ -1,9 +1,11 @@ package cc.maxmc.msm.common.network import cc.maxmc.msm.common.event.PacketReceiveEvent +import io.netty.channel.ChannelHandler.Sharable import io.netty.channel.ChannelHandlerContext import io.netty.channel.SimpleChannelInboundHandler +@Sharable object ClusterPacketHandler : SimpleChannelInboundHandler() { override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) { PacketReceiveEvent(ctx.channel(), msg).callEvent() diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/ClusterMsgCodec.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/ClusterMsgCodec.kt index aac211c..8cf8056 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/ClusterMsgCodec.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/ClusterMsgCodec.kt @@ -1,13 +1,15 @@ package cc.maxmc.msm.common.network.netty import cc.maxmc.msm.common.network.BungeePacket +import cc.maxmc.msm.common.utils.debug import io.netty.buffer.ByteBuf import io.netty.channel.ChannelHandlerContext import io.netty.handler.codec.ByteToMessageCodec class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) : ByteToMessageCodec() { override fun encode(ctx: ChannelHandlerContext, msg: BungeePacket, out: ByteBuf) { - out.writeInt(NetworkRegistry.getPacketID(current, msg)) + val id = NetworkRegistry.getPacketID(current, msg) + out.writeInt(id) msg.encode(out) } diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt index 5114379..5973f57 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt @@ -3,18 +3,20 @@ package cc.maxmc.msm.common.network.netty import cc.maxmc.msm.common.network.BungeePacket import cc.maxmc.msm.common.network.packet.CPacketDebug import cc.maxmc.msm.common.network.packet.PPacketDebug +import cc.maxmc.msm.common.utils.debug import com.google.common.collect.HashBiMap object NetworkRegistry { - private val parentBoundMap = HashBiMap.create() - private val childBoundMap = HashBiMap.create() + private val parentBoundMap = HashBiMap.create>() + private val childBoundMap = HashBiMap.create>() init { - registerPacket(PacketDirection.PARENT_BOUND, PPacketDebug()) - registerPacket(PacketDirection.CHILD_BOUND, CPacketDebug()) + debug("init registry") + registerPacket(PacketDirection.PARENT_BOUND, PPacketDebug::class.java) + registerPacket(PacketDirection.CHILD_BOUND, CPacketDebug::class.java) } - private fun registerPacket(direction: PacketDirection, packet: BungeePacket) { + private fun registerPacket(direction: PacketDirection, packet: Class) { if (direction == PacketDirection.PARENT_BOUND) { parentBoundMap } else { @@ -25,13 +27,15 @@ object NetworkRegistry { } fun getPacketID(side: PacketDirection, packet: BungeePacket): Int { - val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap - return map.inverse()[packet] ?: throw IllegalStateException("Packet does not in registry.") + debug("start get PID") + val map = if (side == PacketDirection.PARENT_BOUND) childBoundMap else parentBoundMap + debug("map use $map") + return map.inverse()[packet::class.java] ?: throw IllegalStateException("Packet does not in registry.") } fun getPacketByID(side: PacketDirection, id: Int): BungeePacket { val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap - return map[id]?.clone() as? BungeePacket ?: throw IllegalStateException("Packet does not in registry.") + return map[id]?.newInstance() ?: throw IllegalStateException("Packet does not in registry.") } enum class PacketDirection { diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt index 0cf4c10..0827619 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt @@ -3,7 +3,7 @@ package cc.maxmc.msm.common.network.packet import cc.maxmc.msm.common.network.BungeePacket import io.netty.buffer.ByteBuf -class CPacketDebug( +data class CPacketDebug( var content: String ) : BungeePacket() { diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt index 37391d1..3c41a34 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt @@ -1,17 +1,20 @@ package cc.maxmc.msm.common.network.packet import cc.maxmc.msm.common.network.BungeePacket +import cc.maxmc.msm.common.utils.debug import io.netty.buffer.ByteBuf -class PPacketDebug( +data class PPacketDebug( var content: String ) : BungeePacket() { constructor() : this("") override fun encode(buf: ByteBuf) { + debug("start packet encode") val array = content.encodeToByteArray() buf.writeInt(array.size) buf.writeBytes(array) + debug("end packet encode") } override fun decode(buf: ByteBuf) { diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/utils/Logging.kt b/common/src/main/kotlin/cc/maxmc/msm/common/utils/Logging.kt index 1927970..54a47b5 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/utils/Logging.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/utils/Logging.kt @@ -5,4 +5,12 @@ import net.md_5.bungee.api.chat.TextComponent fun log(msg: String) { ProxyServer.getInstance().console.sendMessage(TextComponent(msg)) +} + +val debug = true + +fun debug(msg: String) { + if (debug) { + log("§fDEBUG | §7$msg") + } } \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/utils/Netty.kt b/common/src/main/kotlin/cc/maxmc/msm/common/utils/Netty.kt index c483cb2..d267d88 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/utils/Netty.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/utils/Netty.kt @@ -15,9 +15,10 @@ fun channelInit(initializer: C.() -> Unit): ChannelInitializer } } -fun pipelineInit() = channelInit { - pipeline().addLast("frame_decoder", Varint21FrameDecoder()) - pipeline().addLast("codec", ClusterMsgCodec(NetworkRegistry.PacketDirection.CHILD_BOUND)) +fun pipelineInit(direction: NetworkRegistry.PacketDirection) = channelInit { pipeline().addLast("frame_prepender", Varint21LengthFieldPrepender()) + pipeline().addLast("frame_decoder", Varint21FrameDecoder()) + pipeline().addLast("codec", ClusterMsgCodec(direction)) pipeline().addLast("packet_handler", ClusterPacketHandler) + } \ No newline at end of file diff --git a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/NetManager.kt b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/NetManager.kt index 90b80c3..a95cf59 100644 --- a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/NetManager.kt +++ b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/NetManager.kt @@ -1,5 +1,6 @@ package cc.maxmc.msm.parent.netty +import cc.maxmc.msm.common.network.netty.NetworkRegistry import cc.maxmc.msm.common.utils.log import cc.maxmc.msm.common.utils.pipelineInit import cc.maxmc.msm.parent.settings.Settings @@ -9,14 +10,14 @@ import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel object NetManager { - val parentGroup = NioEventLoopGroup() - val childGroup = NioEventLoopGroup() + private val parentGroup = NioEventLoopGroup() + private val childGroup = NioEventLoopGroup() fun startServer() { ServerBootstrap() .channel(NioServerSocketChannel::class.java) .group(parentGroup, childGroup) - .childHandler(pipelineInit()) + .childHandler(pipelineInit(NetworkRegistry.PacketDirection.PARENT_BOUND)) .bind(Settings.serverPort) .addListener(ChannelFutureListener { log("§a| §7集群主服务端启动成功. ${it.channel().localAddress()}") diff --git a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/ParentPacketHandler.kt b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/ParentPacketHandler.kt deleted file mode 100644 index 5710588..0000000 --- a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/netty/ParentPacketHandler.kt +++ /dev/null @@ -1,13 +0,0 @@ -package cc.maxmc.msm.parent.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 ParentPacketHandler : SimpleChannelInboundHandler() { - override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) { - PacketReceiveEvent(ctx.channel(), msg).callEvent() - } - -} \ No newline at end of file