This commit is contained in:
2023-04-05 20:13:19 +08:00
parent e1459c3e8e
commit 5681164469
19 changed files with 301 additions and 9 deletions
+1
View File
@@ -13,6 +13,7 @@ repositories {
dependencies {
api(project(":api"))
implementation(kotlin("stdlib"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0-Beta")
@Suppress("VulnerableLibrariesLocal")
compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT")
compileOnly("net.md-5:bungeecord-proxy:1.19-R0.1-SNAPSHOT") {
@@ -0,0 +1,6 @@
package cc.maxmc.msm.common.event
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
class ChannelActiveEvent(val channel: Channel): Event()
@@ -0,0 +1,6 @@
package cc.maxmc.msm.common.event
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
class ChannelInactiveEvent(val channel: Channel): Event()
@@ -1,7 +1,9 @@
package cc.maxmc.msm.common.network
import cc.maxmc.msm.common.event.ChannelActiveEvent
import cc.maxmc.msm.common.event.ChannelInactiveEvent
import cc.maxmc.msm.common.event.PacketReceiveEvent
import cc.maxmc.msm.common.utils.debug
import cc.maxmc.msm.common.utils.awaiting
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
@@ -9,7 +11,16 @@ import io.netty.channel.SimpleChannelInboundHandler
@Sharable
object ClusterPacketHandler : SimpleChannelInboundHandler<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
// debug("call event")
awaiting.filter { it.first == msg::class.java }
.
PacketReceiveEvent(ctx.channel(), msg).callEvent()
}
override fun channelActive(ctx: ChannelHandlerContext) {
ChannelActiveEvent(ctx.channel()).callEvent()
}
override fun channelInactive(ctx: ChannelHandlerContext) {
ChannelInactiveEvent(ctx.channel()).callEvent()
}
}
@@ -0,0 +1,11 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
class CPacketGetInfo : BungeePacket() {
override fun encode(buf: ByteBuf) {}
override fun decode(buf: ByteBuf) {}
}
@@ -0,0 +1,21 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
import net.md_5.bungee.protocol.DefinedPacket
import java.util.*
class CPacketRequestServer(
var type: String,
var uid: UUID = UUID.randomUUID()
) : BungeePacket() {
override fun encode(buf: ByteBuf) {
DefinedPacket.writeUUID(uid, buf)
DefinedPacket.writeString(type, buf)
}
override fun decode(buf: ByteBuf) {
uid = DefinedPacket.readUUID(buf)
type = DefinedPacket.readString(buf)
}
}
@@ -0,0 +1,29 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
import net.md_5.bungee.protocol.DefinedPacket
class PPacketChildInfo(var portRange: MutableSet<Int> = HashSet(), var types: MutableSet<String>) : BungeePacket() {
override fun encode(buf: ByteBuf) {
DefinedPacket.writeVarInt(portRange.size, buf)
portRange.forEach {
DefinedPacket.writeVarInt(it, buf)
}
DefinedPacket.writeVarInt(types.size, buf)
types.forEach {
DefinedPacket.writeString(it, buf)
}
}
override fun decode(buf: ByteBuf) {
val size = DefinedPacket.readVarInt(buf)
repeat(size) {
portRange.add(DefinedPacket.readVarInt(buf))
}
val typeSize = DefinedPacket.readVarInt(buf)
repeat(typeSize) {
types.add(DefinedPacket.readString(buf))
}
}
}
@@ -0,0 +1,22 @@
package cc.maxmc.msm.common.utils
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.channel.Channel
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
val pluginScope = CoroutineScope(SupervisorJob() + CoroutineExceptionHandler { _, except ->
log("§c执行异步操作时出现异常 ${except.message}")
except.printStackTrace()
})
val awaiting = ArrayList<Triple<Class<out BungeePacket>, (BungeePacket) -> Boolean, Continuation<BungeePacket>>>()
suspend inline fun <T : BungeePacket> awaitPacket(packetClass: Class<T>, noinline filter: (Channel, T) -> Boolean) =
suspendCoroutine<T> {
val triple = Triple(packetClass, filter, it)
@Suppress("UNCHECKED_CAST")
awaiting.add(triple as Triple<Class<out BungeePacket>, (BungeePacket) -> Boolean, Continuation<BungeePacket>>)
}