Add all packets handler

This commit is contained in:
2026-09-07 10:07:10 +08:00
parent d7300451f8
commit c0f9679090
25 files changed
+202 -105

No files matched your search

@@ -15,9 +15,7 @@ public class PacketRegistry {
private val idToCodec = mutableMapOf<Int, PacketCodec<out MinecraftPacket>>()
private val classToInfo = mutableMapOf<KClass<out MinecraftPacket>, RegisteredPacket<*>>()
private data class RegisteredPacket<P : MinecraftPacket>(
val id: Int, val codec: PacketCodec<P>,
)
private data class RegisteredPacket<P : MinecraftPacket>(val id: Int, val codec: PacketCodec<P>)
public fun <T : MinecraftPacket> register(id: Int, kClass: KClass<T>, codec: PacketCodec<T>) {
idToCodec[id] = codec
@@ -40,8 +40,3 @@ public expect class BytesBuffer {
@Suppress("NOTHING_TO_INLINE")
public inline fun ByteArray.wrap(): BytesBuffer = BytesBuffer(this)
public suspend fun ReadChannel.readPacketFrame(): BytesBuffer {
val length = this.readVarInt()
return this.readBytes(length).wrap()
}
@@ -7,6 +7,9 @@
package cn.rtast.libmc.stream
/**
* Platform specified raw byte read channel
*/
public expect open class ReadChannel() {
public open suspend fun readByte(): Byte
public open suspend fun readShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short
@@ -16,21 +19,10 @@ public expect open class ReadChannel() {
public open suspend fun readFully(out: ByteArray, start: Int = 0, end: Int = out.size)
}
/**
* Platform specified raw byte write channel
*/
public expect open class WriteChannel() {
public open suspend fun writeFully(value: ByteArray, startIndex: Int = 0, endIndex: Int = value.size)
public open suspend fun flush()
}
public suspend fun ReadChannel.readVarInt(): Int {
var numRead = 0
var result = 0
var read: Byte
do {
read = this.readByte()
val value = (read.toInt() and 0x7F)
result = result or (value shl (7 * numRead))
numRead++
if (numRead > 5) throw IllegalArgumentException("VarInt is too big")
} while ((read.toInt() and 0x80) != 0)
return result
}
@@ -6,7 +6,7 @@
package cn.rtast.libmc.stream
import cn.rtast.libmc.common.LibMCContext
import cn.rtast.libmc.LibMCContext
import io.ktor.network.sockets.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.runBlocking