fix Packet

This commit is contained in:
tony_all
2023-03-16 00:06:48 +08:00
parent 8c41f190c4
commit 2abf1bb7df
13 changed files with 52 additions and 42 deletions
@@ -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<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
PacketReceiveEvent(ctx.channel(), msg).callEvent()
@@ -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<BungeePacket>() {
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)
}
@@ -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<Int, BungeePacket>()
private val childBoundMap = HashBiMap.create<Int, BungeePacket>()
private val parentBoundMap = HashBiMap.create<Int, Class<out BungeePacket>>()
private val childBoundMap = HashBiMap.create<Int, Class<out BungeePacket>>()
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<out BungeePacket>) {
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 {
@@ -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() {
@@ -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) {
@@ -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")
}
}
@@ -15,9 +15,10 @@ fun <C : Channel> channelInit(initializer: C.() -> Unit): ChannelInitializer<C>
}
}
fun pipelineInit() = channelInit<SocketChannel> {
pipeline().addLast("frame_decoder", Varint21FrameDecoder())
pipeline().addLast("codec", ClusterMsgCodec(NetworkRegistry.PacketDirection.CHILD_BOUND))
fun pipelineInit(direction: NetworkRegistry.PacketDirection) = channelInit<SocketChannel> {
pipeline().addLast("frame_prepender", Varint21LengthFieldPrepender())
pipeline().addLast("frame_decoder", Varint21FrameDecoder())
pipeline().addLast("codec", ClusterMsgCodec(direction))
pipeline().addLast("packet_handler", ClusterPacketHandler)
}