fix Packet
This commit is contained in:
parent
8c41f190c4
commit
2abf1bb7df
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
name: MultiServerMan-Parent
|
||||
name: MultiServerMan-Child
|
||||
main: cc.maxmc.msm.child.MultiServerMan
|
||||
author: TONY_All
|
||||
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
|
@ -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()}")
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue