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

View File

@ -14,8 +14,9 @@ dependencies {
implementation(kotlin("stdlib")) implementation(kotlin("stdlib"))
implementation(project(":api")) implementation(project(":api"))
implementation(project(":common")) implementation(project(":common"))
implementation("io.netty:netty-all:4.1.90.Final")
@Suppress("VulnerableLibrariesLocal") @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 { tasks.shadowJar {

View File

@ -2,6 +2,8 @@ package cc.maxmc.msm.child
import cc.maxmc.msm.child.command.Send import cc.maxmc.msm.child.command.Send
import cc.maxmc.msm.child.netty.NetClient 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.ProxyServer
import net.md_5.bungee.api.plugin.Plugin import net.md_5.bungee.api.plugin.Plugin
@ -12,7 +14,8 @@ class MultiServerMan : Plugin() {
override fun onEnable() { override fun onEnable() {
ProxyServer.getInstance().pluginManager.registerCommand(this, Send) ProxyServer.getInstance().pluginManager.registerCommand(this, Send)
NetClient.start() NetworkRegistry
NetClient.start(Settings.Parent.address, Settings.Parent.port)
} }
override fun onDisable() { override fun onDisable() {

View File

@ -1,8 +1,7 @@
package cc.maxmc.msm.child.netty 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.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 cc.maxmc.msm.common.utils.pipelineInit
import io.netty.bootstrap.Bootstrap import io.netty.bootstrap.Bootstrap
import io.netty.channel.Channel import io.netty.channel.Channel
@ -16,18 +15,17 @@ object NetClient {
lateinit var channel: Channel lateinit var channel: Channel
private set private set
fun start() { fun start(address: String, port: Int) {
val parent = Settings.Parent val future = Bootstrap()
Bootstrap()
.channel(NioSocketChannel::class.java) .channel(NioSocketChannel::class.java)
.group(loop) .group(loop)
.handler(pipelineInit()) .handler(pipelineInit(NetworkRegistry.PacketDirection.CHILD_BOUND))
.option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(parent.address, parent.port) .remoteAddress(address, port)
.connect().addListener(ChannelFutureListener { .connect().addListener(ChannelFutureListener {
channel = it.channel() println("§a| §7成功连接到集群的主节点. (${it.channel().remoteAddress()})")
log("§a| §7成功连接到集群的主节点. (${it.channel().remoteAddress()})")
}) })
channel = future.channel()
} }
fun sendPacket(packet: BungeePacket) { fun sendPacket(packet: BungeePacket) {

View File

@ -1,3 +1,3 @@
name: MultiServerMan-Parent name: MultiServerMan-Child
main: cc.maxmc.msm.child.MultiServerMan main: cc.maxmc.msm.child.MultiServerMan
author: TONY_All author: TONY_All

View File

@ -1,9 +1,11 @@
package cc.maxmc.msm.common.network package cc.maxmc.msm.common.network
import cc.maxmc.msm.common.event.PacketReceiveEvent import cc.maxmc.msm.common.event.PacketReceiveEvent
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler import io.netty.channel.SimpleChannelInboundHandler
@Sharable
object ClusterPacketHandler : SimpleChannelInboundHandler<BungeePacket>() { object ClusterPacketHandler : SimpleChannelInboundHandler<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) { override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
PacketReceiveEvent(ctx.channel(), msg).callEvent() PacketReceiveEvent(ctx.channel(), msg).callEvent()

View File

@ -1,13 +1,15 @@
package cc.maxmc.msm.common.network.netty package cc.maxmc.msm.common.network.netty
import cc.maxmc.msm.common.network.BungeePacket import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.debug
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
class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) : ByteToMessageCodec<BungeePacket>() { class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) : ByteToMessageCodec<BungeePacket>() {
override fun encode(ctx: ChannelHandlerContext, msg: BungeePacket, out: ByteBuf) { 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) msg.encode(out)
} }

View File

@ -3,18 +3,20 @@ package cc.maxmc.msm.common.network.netty
import cc.maxmc.msm.common.network.BungeePacket import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.network.packet.CPacketDebug import cc.maxmc.msm.common.network.packet.CPacketDebug
import cc.maxmc.msm.common.network.packet.PPacketDebug import cc.maxmc.msm.common.network.packet.PPacketDebug
import cc.maxmc.msm.common.utils.debug
import com.google.common.collect.HashBiMap import com.google.common.collect.HashBiMap
object NetworkRegistry { object NetworkRegistry {
private val parentBoundMap = HashBiMap.create<Int, BungeePacket>() private val parentBoundMap = HashBiMap.create<Int, Class<out BungeePacket>>()
private val childBoundMap = HashBiMap.create<Int, BungeePacket>() private val childBoundMap = HashBiMap.create<Int, Class<out BungeePacket>>()
init { init {
registerPacket(PacketDirection.PARENT_BOUND, PPacketDebug()) debug("init registry")
registerPacket(PacketDirection.CHILD_BOUND, CPacketDebug()) 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) { if (direction == PacketDirection.PARENT_BOUND) {
parentBoundMap parentBoundMap
} else { } else {
@ -25,13 +27,15 @@ object NetworkRegistry {
} }
fun getPacketID(side: PacketDirection, packet: BungeePacket): Int { fun getPacketID(side: PacketDirection, packet: BungeePacket): Int {
val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap debug("start get PID")
return map.inverse()[packet] ?: throw IllegalStateException("Packet does not in registry.") 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 { fun getPacketByID(side: PacketDirection, id: Int): BungeePacket {
val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap 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 { enum class PacketDirection {

View File

@ -3,7 +3,7 @@ package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
class CPacketDebug( data class CPacketDebug(
var content: String var content: String
) : BungeePacket() { ) : BungeePacket() {

View File

@ -1,17 +1,20 @@
package cc.maxmc.msm.common.network.packet package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.debug
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
class PPacketDebug( data class PPacketDebug(
var content: String var content: String
) : BungeePacket() { ) : BungeePacket() {
constructor() : this("") constructor() : this("")
override fun encode(buf: ByteBuf) { override fun encode(buf: ByteBuf) {
debug("start packet encode")
val array = content.encodeToByteArray() val array = content.encodeToByteArray()
buf.writeInt(array.size) buf.writeInt(array.size)
buf.writeBytes(array) buf.writeBytes(array)
debug("end packet encode")
} }
override fun decode(buf: ByteBuf) { override fun decode(buf: ByteBuf) {

View File

@ -5,4 +5,12 @@ import net.md_5.bungee.api.chat.TextComponent
fun log(msg: String) { fun log(msg: String) {
ProxyServer.getInstance().console.sendMessage(TextComponent(msg)) ProxyServer.getInstance().console.sendMessage(TextComponent(msg))
}
val debug = true
fun debug(msg: String) {
if (debug) {
log("§fDEBUG | §7$msg")
}
} }

View File

@ -15,9 +15,10 @@ fun <C : Channel> channelInit(initializer: C.() -> Unit): ChannelInitializer<C>
} }
} }
fun pipelineInit() = channelInit<SocketChannel> { fun pipelineInit(direction: NetworkRegistry.PacketDirection) = channelInit<SocketChannel> {
pipeline().addLast("frame_decoder", Varint21FrameDecoder())
pipeline().addLast("codec", ClusterMsgCodec(NetworkRegistry.PacketDirection.CHILD_BOUND))
pipeline().addLast("frame_prepender", Varint21LengthFieldPrepender()) pipeline().addLast("frame_prepender", Varint21LengthFieldPrepender())
pipeline().addLast("frame_decoder", Varint21FrameDecoder())
pipeline().addLast("codec", ClusterMsgCodec(direction))
pipeline().addLast("packet_handler", ClusterPacketHandler) pipeline().addLast("packet_handler", ClusterPacketHandler)
} }

View File

@ -1,5 +1,6 @@
package cc.maxmc.msm.parent.netty 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.log
import cc.maxmc.msm.common.utils.pipelineInit import cc.maxmc.msm.common.utils.pipelineInit
import cc.maxmc.msm.parent.settings.Settings import cc.maxmc.msm.parent.settings.Settings
@ -9,14 +10,14 @@ import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel
object NetManager { object NetManager {
val parentGroup = NioEventLoopGroup() private val parentGroup = NioEventLoopGroup()
val childGroup = NioEventLoopGroup() private val childGroup = NioEventLoopGroup()
fun startServer() { fun startServer() {
ServerBootstrap() ServerBootstrap()
.channel(NioServerSocketChannel::class.java) .channel(NioServerSocketChannel::class.java)
.group(parentGroup, childGroup) .group(parentGroup, childGroup)
.childHandler(pipelineInit()) .childHandler(pipelineInit(NetworkRegistry.PacketDirection.PARENT_BOUND))
.bind(Settings.serverPort) .bind(Settings.serverPort)
.addListener(ChannelFutureListener { .addListener(ChannelFutureListener {
log("§a| §7集群主服务端启动成功. ${it.channel().localAddress()}") log("§a| §7集群主服务端启动成功. ${it.channel().localAddress()}")

View File

@ -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<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
PacketReceiveEvent(ctx.channel(), msg).callEvent()
}
}