This commit is contained in:
TONY_All 2023-03-15 11:45:35 +08:00
parent 75dfd94e66
commit 49b85cf291
17 changed files with 179 additions and 61 deletions

View File

@ -1,5 +1,6 @@
plugins { plugins {
kotlin("jvm") kotlin("jvm")
id("com.github.johnrengelman.shadow")
} }
group = "cc.maxmc.msm.child" group = "cc.maxmc.msm.child"
@ -11,5 +12,16 @@ repositories {
dependencies { dependencies {
implementation(kotlin("stdlib")) implementation(kotlin("stdlib"))
implementation(project(":api"))
implementation(project(":common"))
@Suppress("VulnerableLibrariesLocal")
compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT") compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT")
} }
tasks.shadowJar {
relocate("kotlin", "cc.maxmc.msm.lib.kotlin")
}
tasks.build {
dependsOn(tasks.shadowJar)
}

View File

@ -0,0 +1,18 @@
package cc.maxmc.msm.child
import cc.maxmc.msm.common.Placeholder
import cc.maxmc.msm.common.network.netty.NetworkRegistry
import net.md_5.bungee.api.plugin.Plugin
class MultiServerMan : Plugin() {
override fun onEnable() {
Placeholder.inject(NetworkRegistry.PacketDirection.CHILD_BOUND)
}
override fun onDisable() {
}
}

View File

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

View File

@ -12,6 +12,7 @@ repositories {
dependencies { dependencies {
implementation(kotlin("stdlib")) implementation(kotlin("stdlib"))
@Suppress("VulnerableLibrariesLocal")
compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT") compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT")
compileOnly("net.md-5:bungeecord-proxy:1.19-R0.1-SNAPSHOT") { compileOnly("net.md-5:bungeecord-proxy:1.19-R0.1-SNAPSHOT") {
isTransitive = false isTransitive = false

View File

@ -1,25 +0,0 @@
package cc.maxmc.msm.common
import cc.maxmc.msm.common.network.ClusterMsgCodec
import io.netty.channel.Channel
import net.md_5.bungee.BungeeCord
object PacketInjector {
private val bungee = BungeeCord.getInstance()
private val field = bungee::class.java.getDeclaredField("listeners").also {
it.isAccessible = true
}
fun inject() {
val channel = getChannel()
channel.pipeline().addFirst("ClusterMsgCodec", ClusterMsgCodec)
}
private fun getChannel(): Channel {
@Suppress("UNCHECKED_CAST")
val channelList = field.get(bungee) as Collection<Channel>
while (true) {
return channelList.firstOrNull() ?: continue
}
}
}

View File

@ -0,0 +1,3 @@
package cc.maxmc.msm.common
object Placeholder

View File

@ -2,8 +2,12 @@ package cc.maxmc.msm.common.network
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
interface BungeePacket: Cloneable { abstract class BungeePacket: Cloneable {
fun encode(buf: ByteBuf) abstract fun encode(buf: ByteBuf)
fun decode(buf: ByteBuf) abstract fun decode(buf: ByteBuf)
public override fun clone(): Any {
return super.clone()
}
} }

View File

@ -1,15 +1,19 @@
package cc.maxmc.msm.common.network package cc.maxmc.msm.common.network
import cc.maxmc.msm.common.network.netty.NetworkRegistry
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
object ClusterMsgCodec : 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))
msg.encode(out) msg.encode(out)
} }
override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: MutableList<Any>) { override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: MutableList<Any>) {
val id = `in`.readInt()
val packet = NetworkRegistry.getPacketByID(current, id)
packet.decode(`in`)
} }
} }

View File

@ -1,23 +0,0 @@
package cc.maxmc.msm.common.network
import com.google.common.collect.HashBiMap
object NetworkRegistry {
val parentBoundMap = HashBiMap.create<Int, BungeePacket>()
val childBoundMap = HashBiMap.create<Int, BungeePacket>()
fun registerPacket(packet: BungeePacket, direction: PacketDirection) {
if (direction == PacketDirection.ParentBound) {
parentBoundMap
} else {
childBoundMap
}.let {
it[it.size] = packet
}
}
enum class PacketDirection {
ParentBound,
ChildBound;
}
}

View File

@ -0,0 +1,45 @@
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 com.google.common.collect.HashBiMap
object NetworkRegistry {
private val parentBoundMap = HashBiMap.create<Int, BungeePacket>()
private val childBoundMap = HashBiMap.create<Int, BungeePacket>()
init {
registerPacket(PacketDirection.PARENT_BOUND, PPacketDebug())
registerPacket(PacketDirection.CHILD_BOUND, CPacketDebug())
}
private fun registerPacket(direction: PacketDirection, packet: BungeePacket) {
if (direction == PacketDirection.PARENT_BOUND) {
parentBoundMap
} else {
childBoundMap
}.let {
it[it.size] = packet
}
}
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.")
}
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.")
}
enum class PacketDirection {
PARENT_BOUND,
CHILD_BOUND;
operator fun not(): PacketDirection {
return if (this == PARENT_BOUND) CHILD_BOUND else PARENT_BOUND
}
}
}

View File

@ -0,0 +1,22 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
class CPacketDebug(
var content: String
) : BungeePacket() {
constructor() : this("")
override fun encode(buf: ByteBuf) {
val array = content.encodeToByteArray()
buf.writeInt(array.size)
buf.writeBytes(array)
}
override fun decode(buf: ByteBuf) {
val size = buf.readInt()
content = buf.readBytes(size).array().decodeToString()
}
}

View File

@ -0,0 +1,21 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
class PPacketDebug(
var content: String
) : BungeePacket() {
constructor() : this("")
override fun encode(buf: ByteBuf) {
val array = content.encodeToByteArray()
buf.writeInt(array.size)
buf.writeBytes(array)
}
override fun decode(buf: ByteBuf) {
val size = buf.readInt()
content = buf.readBytes(size).array().decodeToString()
}
}

View File

@ -1,17 +1,15 @@
package cc.maxmc.msm.parent package cc.maxmc.msm.parent
import cc.maxmc.msm.common.PacketInjector import cc.maxmc.msm.common.Placeholder
import cc.maxmc.msm.common.network.netty.NetworkRegistry
import command.Send
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
class MultiServerMan: Plugin() { class MultiServerMan : Plugin() {
override fun onEnable() { override fun onEnable() {
Thread.sleep(5000) ProxyServer.getInstance().pluginManager.registerCommand(this, Send)
ProxyServer.getInstance().scheduler.runAsync(this) { Placeholder.inject(NetworkRegistry.PacketDirection.PARENT_BOUND)
println("injecting")
PacketInjector.inject()
}
} }
override fun onDisable() { override fun onDisable() {

View File

@ -0,0 +1,11 @@
package cc.maxmc.msm.parent.manager
import cc.maxmc.msm.parent.misc.ChildBungee
object ChildManager {
val children = ArrayList<ChildBungee>()
fun registerChild(child: ChildBungee) {
}
}

View File

@ -0,0 +1,3 @@
package cc.maxmc.msm.parent.misc
data class ChildBungee(val addr: String)

View File

@ -0,0 +1,9 @@
package cc.maxmc.msm.parent.network
import cc.maxmc.msm.common.network.netty.NetworkRegistry
object NetManager {
private val current = NetworkRegistry.PacketDirection.PARENT_BOUND
}

View File

@ -0,0 +1,12 @@
package command
import net.md_5.bungee.api.CommandSender
import net.md_5.bungee.api.plugin.Command
object Send: Command("debugP") {
override fun execute(sender: CommandSender, args: Array<out String>) {
val content = args.joinToString(" ")
sender.sendMessage("Success.")
}
}