Split Socket implementation, improve performance
This commit is contained in:
318 files changed
+1109
-2722
No files matched your search
@@ -1,14 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc
|
||||
|
||||
/**
|
||||
* An object class used to pass some parameters
|
||||
* that exists only on native targets
|
||||
*/
|
||||
public expect class LibMCContext()
|
||||
@@ -8,6 +8,6 @@
|
||||
package cn.rtast.libmc.crypto
|
||||
|
||||
public interface NetworkCipher {
|
||||
public suspend fun encrypt(buffer: ByteArray, offset: Int, length: Int)
|
||||
public suspend fun decrypt(buffer: ByteArray, offset: Int, length: Int)
|
||||
public fun encrypt(buffer: ByteArray, offset: Int, length: Int)
|
||||
public fun decrypt(buffer: ByteArray, offset: Int, length: Int)
|
||||
}
|
||||
@@ -7,18 +7,23 @@
|
||||
|
||||
package cn.rtast.libmc.crypto
|
||||
|
||||
import cn.rtast.libmc.network.SocketContext
|
||||
import cn.rtast.libmc.network.SocketEngine
|
||||
|
||||
public data class ProtocolContext(
|
||||
val rsaEncryptor: RSA1024Encryptor,
|
||||
val sha1Hasher: Sha1Hasher,
|
||||
val cipherFactory: (sharedKey: ByteArray) -> NetworkCipher,
|
||||
val authProvider: AuthenticationProvider?,
|
||||
)
|
||||
override val engine: SocketEngine,
|
||||
) : SocketContext()
|
||||
|
||||
public class ProtocolContextBuilder(private val onlineMode: Boolean) {
|
||||
public lateinit var rsaEncryptor: RSA1024Encryptor
|
||||
public lateinit var sha1Hasher: Sha1Hasher
|
||||
public lateinit var cipherFactory: (sharedKey: ByteArray) -> NetworkCipher
|
||||
public lateinit var authProvider: AuthenticationProvider
|
||||
public lateinit var socketEngine: SocketEngine
|
||||
|
||||
public fun build(): ProtocolContext =
|
||||
ProtocolContext(
|
||||
@@ -27,14 +32,15 @@ public class ProtocolContextBuilder(private val onlineMode: Boolean) {
|
||||
cipherFactory = if (::cipherFactory.isInitialized) cipherFactory else error("cipherFactory is required"),
|
||||
authProvider = if (onlineMode) {
|
||||
if (::authProvider.isInitialized) authProvider else error("authProvider is required in online mode")
|
||||
} else if (::authProvider.isInitialized) authProvider else null
|
||||
} else if (::authProvider.isInitialized) authProvider else null,
|
||||
engine = if (::socketEngine.isInitialized) socketEngine else error("SocketEngine is not configured")
|
||||
)
|
||||
}
|
||||
|
||||
public fun interface RSA1024Encryptor {
|
||||
public suspend fun encrypt(key: ByteArray, data: ByteArray): ByteArray
|
||||
public fun encrypt(key: ByteArray, data: ByteArray): ByteArray
|
||||
}
|
||||
|
||||
public fun interface Sha1Hasher {
|
||||
public suspend fun hash(serverId: String, secretKey: ByteArray, publicKey: ByteArray): String
|
||||
public fun hash(serverId: String, secretKey: ByteArray, publicKey: ByteArray): String
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
public enum class ByteOrder {
|
||||
BIG_ENDIAN, LITTLE_ENDIAN
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
import kotlinx.io.*
|
||||
|
||||
|
||||
public class BytesBuffer {
|
||||
private val _buffer = Buffer()
|
||||
|
||||
public constructor()
|
||||
public constructor(bytes: ByteArray) {
|
||||
this._buffer.write(bytes)
|
||||
}
|
||||
|
||||
public fun writeByte(value: Byte): Unit = _buffer.writeByte(value)
|
||||
public fun writeShort(value: Short): Unit = _buffer.writeShort(value)
|
||||
public fun writeInt(value: Int): Unit = _buffer.writeInt(value)
|
||||
public fun writeLong(value: Long): Unit = _buffer.writeLong(value)
|
||||
public fun writeDouble(value: Double): Unit = _buffer.writeDouble(value)
|
||||
public fun writeFloat(value: Float): Unit = _buffer.writeFloat(value)
|
||||
public fun writeBytes(bytes: ByteArray): Unit = _buffer.write(bytes)
|
||||
public fun writeBoolean(value: Boolean): Unit = _buffer.writeByte(if (value) 0x01 else 0x00)
|
||||
|
||||
public fun readByte(): Byte = _buffer.readByte()
|
||||
public fun readUByte(): UByte = _buffer.readUByte()
|
||||
public fun readShort(): Short = _buffer.readShort()
|
||||
public fun readInt(): Int = _buffer.readInt()
|
||||
public fun readLong(): Long = _buffer.readLong()
|
||||
public fun readDouble(): Double = _buffer.readDouble()
|
||||
public fun readFloat(): Float = _buffer.readFloat()
|
||||
public fun readBytes(length: Int): ByteArray = _buffer.readByteArray(length)
|
||||
public fun readBoolean(): Boolean = _buffer.readByte() != 0x00.toByte()
|
||||
public fun toByteArray(): ByteArray = _buffer.readByteArray()
|
||||
public fun peek(): ByteArray = _buffer.peek().readByteArray()
|
||||
public fun close(): Unit = _buffer.close()
|
||||
|
||||
public val size: Int get() = _buffer.size.toInt()
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
public inline fun ByteArray.wrap(): BytesBuffer = BytesBuffer(this)
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
public interface RawSocket : AutoCloseable {
|
||||
/**
|
||||
* Open socket connection
|
||||
*/
|
||||
public suspend fun connect()
|
||||
|
||||
/**
|
||||
* An abstract function, used to open tcp socket read channel
|
||||
*/
|
||||
public fun openReadChannel(): ReadChannel
|
||||
|
||||
/**
|
||||
* An abstract function, used to open tcp socket write/send channel
|
||||
*/
|
||||
public fun openWriteChannel(): WriteChannel
|
||||
|
||||
/**
|
||||
* Close socket
|
||||
*/
|
||||
override fun close()
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/7
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
public interface ReadChannel {
|
||||
public suspend fun readByte(): Byte
|
||||
public suspend fun readBytes(length: Int): ByteArray
|
||||
public suspend fun readFully(out: ByteArray, start: Int = 0, end: Int = out.size)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/7
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
public abstract class SocketContext {
|
||||
public abstract val engine: SocketEngine
|
||||
|
||||
public fun createSocket(host: String, port: Int): RawSocket = engine.create(host, port)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/7
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
public fun interface SocketEngine {
|
||||
public fun create(host: String, port: Int): RawSocket
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/7
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.network
|
||||
|
||||
public interface WriteChannel {
|
||||
public suspend fun writeFully(value: ByteArray, startIndex: Int = 0, endIndex: Int = value.size)
|
||||
public suspend fun flush()
|
||||
}
|
||||
@@ -7,20 +7,20 @@
|
||||
|
||||
package cn.rtast.libmc.packet
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public interface Encoder<in T> {
|
||||
public suspend fun encode(buffer: BytesBuffer, value: T)
|
||||
public fun encode(buffer: BytesBuffer, value: T)
|
||||
}
|
||||
|
||||
public interface Decoder<out T> {
|
||||
public suspend fun decode(buffer: BytesBuffer): T
|
||||
public fun decode(buffer: BytesBuffer): T
|
||||
}
|
||||
|
||||
public interface PacketCodec<T> : Encoder<T>, Decoder<T>
|
||||
|
||||
public suspend fun BytesBuffer.writeBuffer(source: BytesBuffer, length: Long = source.remaining) {
|
||||
public fun BytesBuffer.writeBuffer(source: BytesBuffer, length: Int = source.size) {
|
||||
if (length <= 0) return
|
||||
val bytes = source.readBytes(length.toInt())
|
||||
val bytes = source.readBytes(length)
|
||||
this.writeBytes(bytes)
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
package cn.rtast.libmc.packet
|
||||
|
||||
import cn.rtast.libmc.primitives.writeVarInt
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
public class PacketRegistry {
|
||||
@@ -26,10 +26,10 @@ public class PacketRegistry {
|
||||
register(id, T::class, codec)
|
||||
}
|
||||
|
||||
public suspend fun decodePacket(packetId: Int, buffer: BytesBuffer): MinecraftPacket =
|
||||
idToCodec[packetId]?.decode(buffer) ?: ClientboundUnknownPacket(packetId, buffer.readRemainingBytes())
|
||||
public fun decodePacket(packetId: Int, buffer: BytesBuffer): MinecraftPacket =
|
||||
idToCodec[packetId]?.decode(buffer) ?: ClientboundUnknownPacket(packetId, buffer.toByteArray())
|
||||
|
||||
public suspend fun <T : MinecraftPacket> encodePacket(buffer: BytesBuffer, packet: T) {
|
||||
public fun <T : MinecraftPacket> encodePacket(buffer: BytesBuffer, packet: T) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val info = requireNotNull(classToInfo[packet::class]) {
|
||||
"Unregistered Packet ${packet::class.simpleName}"
|
||||
|
||||
@@ -7,16 +7,16 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public typealias BitSet = LongArray
|
||||
|
||||
public suspend fun BytesBuffer.readBitSet(): BitSet {
|
||||
public fun BytesBuffer.readBitSet(): BitSet {
|
||||
val count = this.readVarInt()
|
||||
return LongArray(count) { this.readLong() }
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writeBitSet(data: BitSet) {
|
||||
public fun BytesBuffer.writeBitSet(data: BitSet) {
|
||||
this.writeVarInt(data.size)
|
||||
for (i in data.indices) this.writeLong(data[i])
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public sealed class Either<out L, out R> {
|
||||
public data class Left<out L>(val value: L) : Either<L, Nothing>()
|
||||
@@ -17,7 +17,7 @@ public sealed class Either<out L, out R> {
|
||||
public val isRight: Boolean get() = this is Right
|
||||
}
|
||||
|
||||
public suspend inline fun <L, R> BytesBuffer.readEither(
|
||||
public inline fun <L, R> BytesBuffer.readEither(
|
||||
readLeft: BytesBuffer.() -> L,
|
||||
readRight: BytesBuffer.() -> R,
|
||||
): Either<L, R> {
|
||||
@@ -25,7 +25,7 @@ public suspend inline fun <L, R> BytesBuffer.readEither(
|
||||
return if (isLeft) Either.Left(readLeft(this)) else Either.Right(readRight(this))
|
||||
}
|
||||
|
||||
public suspend inline fun <L, R> BytesBuffer.writeEither(
|
||||
public inline fun <L, R> BytesBuffer.writeEither(
|
||||
either: Either<L, R>,
|
||||
writeLeft: BytesBuffer.(L) -> Unit,
|
||||
writeRight: BytesBuffer.(R) -> Unit,
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public sealed interface IdOrX<out T> {
|
||||
public data class Inline<T>(val value: T) : IdOrX<T>
|
||||
public data class Reference(val registryId: Int) : IdOrX<Nothing>
|
||||
}
|
||||
|
||||
public suspend inline fun <T> BytesBuffer.writeIdOrX(value: IdOrX<T>, writeX: BytesBuffer.(T) -> Unit) {
|
||||
public inline fun <T> BytesBuffer.writeIdOrX(value: IdOrX<T>, writeX: BytesBuffer.(T) -> Unit) {
|
||||
when (value) {
|
||||
is IdOrX.Inline -> {
|
||||
this.writeVarInt(0)
|
||||
@@ -25,7 +25,7 @@ public suspend inline fun <T> BytesBuffer.writeIdOrX(value: IdOrX<T>, writeX: By
|
||||
}
|
||||
}
|
||||
|
||||
public suspend inline fun <T> BytesBuffer.readIdOrX(readX: BytesBuffer.() -> T): IdOrX<T> {
|
||||
public inline fun <T> BytesBuffer.readIdOrX(readX: BytesBuffer.() -> T): IdOrX<T> {
|
||||
val id = this.readVarInt()
|
||||
return if (id == 0) IdOrX.Inline(this.readX()) else IdOrX.Reference(id - 1)
|
||||
}
|
||||
@@ -7,14 +7,14 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public sealed interface IdSet {
|
||||
public data class Tag(val tagName: String) : IdSet
|
||||
public data class Entries(val ids: List<Int>) : IdSet
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.readIdSet(): IdSet {
|
||||
public fun BytesBuffer.readIdSet(): IdSet {
|
||||
val type = this.readVarInt()
|
||||
return if (type == 0) {
|
||||
IdSet.Tag(tagName = this.readMcString())
|
||||
@@ -26,7 +26,7 @@ public suspend fun BytesBuffer.readIdSet(): IdSet {
|
||||
}
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writeIdSet(idSet: IdSet) {
|
||||
public fun BytesBuffer.writeIdSet(idSet: IdSet) {
|
||||
when (idSet) {
|
||||
is IdSet.Tag -> {
|
||||
this.writeVarInt(0)
|
||||
|
||||
@@ -8,21 +8,21 @@
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.packet.PacketCodec
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public object McStringCodec : PacketCodec<String> {
|
||||
override suspend fun encode(buffer: BytesBuffer, value: String) {
|
||||
override fun encode(buffer: BytesBuffer, value: String) {
|
||||
val bytes = value.encodeToByteArray()
|
||||
VarIntCodec.encode(buffer, bytes.size)
|
||||
buffer.writeBytes(bytes)
|
||||
}
|
||||
|
||||
override suspend fun decode(buffer: BytesBuffer): String {
|
||||
override fun decode(buffer: BytesBuffer): String {
|
||||
val length = VarIntCodec.decode(buffer)
|
||||
val bytes = buffer.readBytes(length)
|
||||
return bytes.decodeToString()
|
||||
}
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writeMcString(value: String): Unit = McStringCodec.encode(this, value)
|
||||
public suspend fun BytesBuffer.readMcString(): String = McStringCodec.decode(this)
|
||||
public fun BytesBuffer.writeMcString(value: String): Unit = McStringCodec.encode(this, value)
|
||||
public fun BytesBuffer.readMcString(): String = McStringCodec.decode(this)
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public suspend inline fun <T> BytesBuffer.readOptional(block: BytesBuffer.() -> T): T? {
|
||||
public inline fun <T> BytesBuffer.readOptional(block: BytesBuffer.() -> T): T? {
|
||||
val hasData = this.readBoolean()
|
||||
return if (hasData) block.invoke(this) else null
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public suspend inline fun <T> BytesBuffer.readOptional(block: BytesBuffer.() ->
|
||||
/**
|
||||
* buffer.writeOptional(value.someValue) { writeBlockPos(it) }
|
||||
*/
|
||||
public suspend inline fun <T> BytesBuffer.writeOptional(value: T?, block: BytesBuffer.(T) -> Unit) {
|
||||
public inline fun <T> BytesBuffer.writeOptional(value: T?, block: BytesBuffer.(T) -> Unit) {
|
||||
if (value != null) {
|
||||
this.writeBoolean(true)
|
||||
block.invoke(this, value)
|
||||
|
||||
@@ -7,21 +7,21 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
|
||||
public suspend fun BytesBuffer.readPrefixedByteArray(): ByteArray {
|
||||
public fun BytesBuffer.readPrefixedByteArray(): ByteArray {
|
||||
val length = this.readVarInt()
|
||||
val data = this.readBytes(length)
|
||||
return data
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writePrefixedByteArray(data: ByteArray) {
|
||||
public fun BytesBuffer.writePrefixedByteArray(data: ByteArray) {
|
||||
this.writeVarInt(data.size)
|
||||
this.writeBytes(data)
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writeOptionalPrefixedByteArray(data: ByteArray?) {
|
||||
public fun BytesBuffer.writeOptionalPrefixedByteArray(data: ByteArray?) {
|
||||
if (data != null) {
|
||||
this.writeBoolean(true)
|
||||
this.writeVarInt(data.size)
|
||||
@@ -29,21 +29,21 @@ public suspend fun BytesBuffer.writeOptionalPrefixedByteArray(data: ByteArray?)
|
||||
} else this.writeBoolean(false)
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.readPrefixedStringArray(): List<String> {
|
||||
public fun BytesBuffer.readPrefixedStringArray(): List<String> {
|
||||
val length = readVarInt()
|
||||
val list = ArrayList<String>(length)
|
||||
repeat(length) { list.add(readMcString()) }
|
||||
return list
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writePrefixedStringArray(value: List<String>) {
|
||||
public fun BytesBuffer.writePrefixedStringArray(value: List<String>) {
|
||||
writeVarInt(value.size)
|
||||
for (item in value) writeMcString(item)
|
||||
}
|
||||
|
||||
|
||||
|
||||
public suspend inline fun <T> BytesBuffer.readPrefixed(reader: BytesBuffer.() -> T): List<T> {
|
||||
public inline fun <T> BytesBuffer.readPrefixed(reader: BytesBuffer.() -> T): List<T> {
|
||||
val count = this.readVarInt()
|
||||
require(count in 0..4096) {
|
||||
"Prefixed array count $count is invalid (expected 0..4096). " +
|
||||
@@ -54,7 +54,7 @@ public suspend inline fun <T> BytesBuffer.readPrefixed(reader: BytesBuffer.() ->
|
||||
return list
|
||||
}
|
||||
|
||||
public suspend inline fun <T> BytesBuffer.writePrefixed(list: List<T>, writer: BytesBuffer.(T) -> Unit) {
|
||||
public inline fun <T> BytesBuffer.writePrefixed(list: List<T>, writer: BytesBuffer.(T) -> Unit) {
|
||||
this.writeVarInt(list.size)
|
||||
for (item in list) this.writer(item)
|
||||
}
|
||||
@@ -7,15 +7,15 @@
|
||||
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
import kotlin.uuid.Uuid
|
||||
|
||||
public suspend fun BytesBuffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
|
||||
public fun BytesBuffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
|
||||
this.writeLong(mostSignificantBits)
|
||||
this.writeLong(leastSignificantBits)
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.readUuid(): Uuid {
|
||||
public fun BytesBuffer.readUuid(): Uuid {
|
||||
val most = this.readLong()
|
||||
val least = this.readLong()
|
||||
return Uuid.fromLongs(most, least)
|
||||
|
||||
@@ -8,10 +8,11 @@
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.packet.PacketCodec
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
import cn.rtast.libmc.network.ReadChannel
|
||||
|
||||
public object VarIntCodec : PacketCodec<Int> {
|
||||
override suspend fun encode(buffer: BytesBuffer, value: Int) {
|
||||
override fun encode(buffer: BytesBuffer, value: Int) {
|
||||
var v = value
|
||||
while (true) {
|
||||
if ((v and 0x7F.inv()) == 0) {
|
||||
@@ -23,7 +24,7 @@ public object VarIntCodec : PacketCodec<Int> {
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun decode(buffer: BytesBuffer): Int {
|
||||
override fun decode(buffer: BytesBuffer): Int {
|
||||
var numRead = 0
|
||||
var result = 0
|
||||
var read: Byte
|
||||
@@ -38,8 +39,22 @@ public object VarIntCodec : PacketCodec<Int> {
|
||||
}
|
||||
}
|
||||
|
||||
public suspend fun BytesBuffer.writeVarInt(value: Int): Unit = VarIntCodec.encode(this, value)
|
||||
public suspend fun BytesBuffer.readVarInt(): Int = VarIntCodec.decode(this)
|
||||
public fun BytesBuffer.writeVarInt(value: Int): Unit = VarIntCodec.encode(this, value)
|
||||
public fun BytesBuffer.readVarInt(): Int = VarIntCodec.decode(this)
|
||||
|
||||
public suspend fun BytesBuffer.writeVarLong(value: Long): Unit = VarLongCodec.encode(this, value)
|
||||
public suspend fun BytesBuffer.readVarLong(): Long = VarLongCodec.decode(this)
|
||||
public fun BytesBuffer.writeVarLong(value: Long): Unit = VarLongCodec.encode(this, value)
|
||||
public fun BytesBuffer.readVarLong(): Long = VarLongCodec.decode(this)
|
||||
|
||||
public suspend fun ReadChannel.readVarInt(): Int {
|
||||
var numRead = 0
|
||||
var result = 0
|
||||
var read: Byte
|
||||
do {
|
||||
read = 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
|
||||
}
|
||||
@@ -8,10 +8,10 @@
|
||||
package cn.rtast.libmc.primitives
|
||||
|
||||
import cn.rtast.libmc.packet.PacketCodec
|
||||
import cn.rtast.libmc.stream.BytesBuffer
|
||||
import cn.rtast.libmc.network.BytesBuffer
|
||||
|
||||
public object VarLongCodec : PacketCodec<Long> {
|
||||
override suspend fun encode(buffer: BytesBuffer, value: Long) {
|
||||
override fun encode(buffer: BytesBuffer, value: Long) {
|
||||
var v = value
|
||||
while (true) {
|
||||
if ((v and 0x7FL.inv()) == 0L) {
|
||||
@@ -23,7 +23,7 @@ public object VarLongCodec : PacketCodec<Long> {
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun decode(buffer: BytesBuffer): Long {
|
||||
override fun decode(buffer: BytesBuffer): Long {
|
||||
var numRead = 0
|
||||
var result = 0L
|
||||
var read: Byte
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
public expect class BytesBuffer {
|
||||
public constructor()
|
||||
public constructor(bytes: ByteArray)
|
||||
|
||||
public suspend fun writeByte(value: Byte)
|
||||
public suspend fun writeShort(value: Short, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public suspend fun writeInt(value: Int, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public suspend fun writeLong(value: Long, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public suspend fun writeDouble(value: Double, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public suspend fun writeFloat(value: Float, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public suspend fun writeBytes(bytes: ByteArray)
|
||||
public suspend fun writeBoolean(value: Boolean)
|
||||
|
||||
public suspend fun readByte(): Byte
|
||||
public suspend fun readUByte(): UByte
|
||||
public suspend fun readShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short
|
||||
public suspend fun readInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int
|
||||
public suspend fun readLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long
|
||||
public suspend fun readDouble(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Double
|
||||
public suspend fun readFloat(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Float
|
||||
public suspend fun readBytes(length: Int): ByteArray
|
||||
public suspend fun readBoolean(): Boolean
|
||||
public suspend fun readRemainingBytes(): ByteArray
|
||||
|
||||
public suspend fun toByteArray(): ByteArray
|
||||
public suspend fun hasRemaining(): Boolean
|
||||
public suspend fun close()
|
||||
public val size: Int
|
||||
public val remaining: Long
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
public inline fun ByteArray.wrap(): BytesBuffer = BytesBuffer(this)
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
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
|
||||
public open suspend fun readInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int
|
||||
public open suspend fun readLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long
|
||||
public open suspend fun readBytes(length: Int): ByteArray
|
||||
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()
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import cn.rtast.libmc.LibMCContext
|
||||
|
||||
|
||||
public expect class Socket public constructor(host: String, port: Int, context: LibMCContext) {
|
||||
public fun openReadChannel(): ReadChannel
|
||||
public fun openWriteChannel(): WriteChannel
|
||||
public fun close()
|
||||
}
|
||||
|
||||
public expect class UdpSocket public constructor(host: String, port: Int, context: LibMCContext) {
|
||||
public suspend fun sendAndReceive(data: ByteArray): ByteArray
|
||||
public fun close()
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc
|
||||
|
||||
/**
|
||||
* No Context for jvm targets
|
||||
*/
|
||||
public actual class LibMCContext
|
||||
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
public actual class BytesBuffer {
|
||||
private val outStream = ByteArrayOutputStream()
|
||||
private var readBuffer: ByteArray? = null
|
||||
private var readOffset = 0
|
||||
|
||||
public actual constructor()
|
||||
public actual constructor(bytes: ByteArray) {
|
||||
this.readBuffer = bytes
|
||||
outStream.write(bytes)
|
||||
}
|
||||
|
||||
public actual suspend fun writeByte(value: Byte) {
|
||||
readBuffer = null
|
||||
outStream.write(value.toInt())
|
||||
}
|
||||
|
||||
public actual suspend fun writeShort(value: Short, endian: ByteOrder) {
|
||||
readBuffer = null
|
||||
val v = value.toInt()
|
||||
if (endian == ByteOrder.BIG_ENDIAN) {
|
||||
outStream.write(v shr 8)
|
||||
outStream.write(v)
|
||||
} else {
|
||||
outStream.write(v)
|
||||
outStream.write(v shr 8)
|
||||
}
|
||||
}
|
||||
|
||||
public actual suspend fun writeInt(value: Int, endian: ByteOrder) {
|
||||
readBuffer = null
|
||||
if (endian == ByteOrder.BIG_ENDIAN) {
|
||||
outStream.write(value shr 24)
|
||||
outStream.write(value shr 16)
|
||||
outStream.write(value shr 8)
|
||||
outStream.write(value)
|
||||
} else {
|
||||
outStream.write(value)
|
||||
outStream.write(value shr 8)
|
||||
outStream.write(value shr 16)
|
||||
outStream.write(value shr 24)
|
||||
}
|
||||
}
|
||||
|
||||
public actual suspend fun writeLong(value: Long, endian: ByteOrder) {
|
||||
readBuffer = null
|
||||
if (endian == ByteOrder.BIG_ENDIAN) {
|
||||
for (i in 56 downTo 0 step 8) outStream.write((value shr i).toInt())
|
||||
} else {
|
||||
for (i in 0..56 step 8) outStream.write((value shr i).toInt())
|
||||
}
|
||||
}
|
||||
|
||||
public actual suspend fun writeDouble(value: Double, endian: ByteOrder) {
|
||||
writeLong(value.toRawBits(), endian)
|
||||
}
|
||||
|
||||
public actual suspend fun writeFloat(value: Float, endian: ByteOrder) {
|
||||
writeInt(value.toRawBits(), endian)
|
||||
}
|
||||
|
||||
public actual suspend fun writeBytes(bytes: ByteArray) {
|
||||
readBuffer = null
|
||||
withContext(Dispatchers.IO) { outStream.write(bytes) }
|
||||
}
|
||||
|
||||
public actual suspend fun writeBoolean(value: Boolean): Unit = writeByte(if (value) 0x01 else 0x00)
|
||||
|
||||
private suspend fun ensureReadArray(): ByteArray {
|
||||
var buf = readBuffer
|
||||
if (buf == null) {
|
||||
buf = outStream.toByteArray()
|
||||
readBuffer = buf
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
public actual suspend fun readByte(): Byte {
|
||||
val array = ensureReadArray()
|
||||
if (readOffset >= array.size) throw IndexOutOfBoundsException("Buffer underflow")
|
||||
return array[readOffset++]
|
||||
}
|
||||
|
||||
public actual suspend fun readUByte(): UByte = this.readByte().toUByte()
|
||||
public actual suspend fun readShort(endian: ByteOrder): Short = readBytes(2).toShort(endian)
|
||||
public actual suspend fun readInt(endian: ByteOrder): Int = readBytes(4).toInt(endian)
|
||||
public actual suspend fun readLong(endian: ByteOrder): Long = readBytes(8).toLong(endian)
|
||||
|
||||
public actual suspend fun readDouble(endian: ByteOrder): Double {
|
||||
return Double.fromBits(readLong(endian))
|
||||
}
|
||||
|
||||
public actual suspend fun readFloat(endian: ByteOrder): Float {
|
||||
return Float.fromBits(readInt(endian))
|
||||
}
|
||||
|
||||
public actual suspend fun readBytes(length: Int): ByteArray {
|
||||
val array = ensureReadArray()
|
||||
if (readOffset + length > array.size) throw IndexOutOfBoundsException("Buffer underflow")
|
||||
val result = array.copyOfRange(readOffset, readOffset + length)
|
||||
readOffset += length
|
||||
return result
|
||||
}
|
||||
|
||||
public actual suspend fun readBoolean(): Boolean = this.readByte() != 0x00.toByte()
|
||||
|
||||
public actual suspend fun readRemainingBytes(): ByteArray {
|
||||
val array = ensureReadArray()
|
||||
if (readOffset >= array.size) return byteArrayOf()
|
||||
val result = array.copyOfRange(readOffset, array.size)
|
||||
readOffset = array.size
|
||||
return result
|
||||
}
|
||||
|
||||
public actual suspend fun toByteArray(): ByteArray = outStream.toByteArray()
|
||||
public actual suspend fun hasRemaining(): Boolean = readOffset < ensureReadArray().size
|
||||
|
||||
public actual suspend fun close(): Unit = withContext(Dispatchers.IO) { outStream.close() }
|
||||
public actual val size: Int get() = outStream.size()
|
||||
public actual val remaining: Long get() = (outStream.size() - readOffset).coerceAtLeast(0).toLong()
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import java.io.EOFException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
public actual open class ReadChannel public actual constructor() {
|
||||
private lateinit var _inputStream: InputStream
|
||||
|
||||
public constructor(inputStream: InputStream) : this() {
|
||||
this._inputStream = inputStream
|
||||
}
|
||||
|
||||
public actual open suspend fun readFully(out: ByteArray, start: Int, end: Int) {
|
||||
var bytesRead = 0
|
||||
val length = end - start
|
||||
while (bytesRead < length) {
|
||||
val read = _inputStream.read(out, start + bytesRead, length - bytesRead)
|
||||
if (read == -1) throw EOFException("End of stream reached")
|
||||
bytesRead += read
|
||||
}
|
||||
}
|
||||
|
||||
public actual open suspend fun readByte(): Byte {
|
||||
val buf = ByteArray(1)
|
||||
readFully(buf, 0, 1)
|
||||
return buf[0]
|
||||
}
|
||||
|
||||
public actual open suspend fun readBytes(length: Int): ByteArray {
|
||||
val bytes = ByteArray(length)
|
||||
readFully(bytes, 0, length)
|
||||
return bytes
|
||||
}
|
||||
|
||||
public actual open suspend fun readShort(endian: ByteOrder): Short = readBytes(2).toShort(endian)
|
||||
public actual open suspend fun readInt(endian: ByteOrder): Int = readBytes(4).toInt(endian)
|
||||
public actual open suspend fun readLong(endian: ByteOrder): Long = readBytes(8).toLong(endian)
|
||||
}
|
||||
|
||||
public actual open class WriteChannel public actual constructor() {
|
||||
private lateinit var _outputStream: OutputStream
|
||||
|
||||
public constructor(outputStream: OutputStream) : this() {
|
||||
_outputStream = outputStream
|
||||
}
|
||||
|
||||
public actual open suspend fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int): Unit =
|
||||
_outputStream.write(value, startIndex, endIndex - startIndex)
|
||||
|
||||
public actual open suspend fun flush(): Unit = _outputStream.flush()
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/4
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
internal fun ByteArray.toShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short {
|
||||
val b1 = this[0].toInt() and 0xFF
|
||||
val b2 = this[1].toInt() and 0xFF
|
||||
return if (endian == ByteOrder.BIG_ENDIAN) {
|
||||
((b1 shl 8) or b2).toShort()
|
||||
} else {
|
||||
((b2 shl 8) or b1).toShort()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun ByteArray.toInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int {
|
||||
val b1 = this[0].toInt() and 0xFF
|
||||
val b2 = this[1].toInt() and 0xFF
|
||||
val b3 = this[2].toInt() and 0xFF
|
||||
val b4 = this[3].toInt() and 0xFF
|
||||
return if (endian == ByteOrder.BIG_ENDIAN) {
|
||||
(b1 shl 24) or (b2 shl 16) or (b3 shl 8) or b4
|
||||
} else {
|
||||
(b4 shl 24) or (b3 shl 16) or (b2 shl 8) or b1
|
||||
}
|
||||
}
|
||||
|
||||
internal fun ByteArray.toLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long {
|
||||
var result = 0L
|
||||
if (endian == ByteOrder.BIG_ENDIAN) {
|
||||
for (b in this) result = (result shl 8) or (b.toLong() and 0xFF)
|
||||
} else {
|
||||
for (i in 7 downTo 0) result = (result shl 8) or (this[i].toLong() and 0xFF)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import cn.rtast.libmc.LibMCContext
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.net.DatagramPacket
|
||||
import java.net.DatagramSocket
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Socket as JvmSocket
|
||||
|
||||
public actual class Socket public actual constructor(host: String, port: Int, context: LibMCContext) {
|
||||
private val socket = JvmSocket(host, port)
|
||||
|
||||
public actual fun openReadChannel(): ReadChannel = ReadChannel(socket.getInputStream())
|
||||
public actual fun openWriteChannel(): WriteChannel = WriteChannel(socket.getOutputStream())
|
||||
public actual fun close(): Unit = socket.close()
|
||||
}
|
||||
|
||||
public actual class UdpSocket public actual constructor(host: String, port: Int, context: LibMCContext) {
|
||||
private val socket = DatagramSocket().apply {
|
||||
soTimeout = 3000
|
||||
connect(InetSocketAddress(host, port))
|
||||
}
|
||||
|
||||
public actual suspend fun sendAndReceive(data: ByteArray): ByteArray = withContext(Dispatchers.IO) {
|
||||
socket.send(DatagramPacket(data, data.size))
|
||||
val buf = ByteArray(2048)
|
||||
val receivePacket = DatagramPacket(buf, buf.size)
|
||||
socket.receive(receivePacket)
|
||||
return@withContext buf.copyOf(receivePacket.length)
|
||||
}
|
||||
|
||||
public actual fun close(): Unit = socket.close()
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
@file:Suppress("PropertyName")
|
||||
|
||||
package cn.rtast.libmc
|
||||
|
||||
import io.ktor.network.selector.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.IO
|
||||
|
||||
public actual class LibMCContext actual constructor() {
|
||||
internal var _selectorManager: SelectorManager = SelectorManager(Dispatchers.IO)
|
||||
internal var _autoCloseSelectorManager: Boolean = false
|
||||
|
||||
public constructor(selectorManager: SelectorManager, autoClose: Boolean = false) : this() {
|
||||
_selectorManager = selectorManager
|
||||
_autoCloseSelectorManager = autoClose
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import io.ktor.utils.io.core.*
|
||||
import kotlinx.io.*
|
||||
import kotlinx.io.Buffer
|
||||
|
||||
public actual class BytesBuffer {
|
||||
private val _delegateBuf: Buffer
|
||||
|
||||
public actual constructor() {
|
||||
_delegateBuf = Buffer()
|
||||
}
|
||||
|
||||
public actual constructor(bytes: ByteArray) {
|
||||
_delegateBuf = Buffer().apply { write(bytes) }
|
||||
}
|
||||
|
||||
public actual suspend fun writeByte(value: Byte): Unit = _delegateBuf.writeByte(value)
|
||||
public actual suspend fun writeShort(value: Short, endian: ByteOrder) {
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.writeShort(value)
|
||||
else _delegateBuf.writeShortLe(value)
|
||||
}
|
||||
|
||||
public actual suspend fun writeInt(value: Int, endian: ByteOrder) {
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.writeInt(value)
|
||||
else _delegateBuf.writeIntLe(value)
|
||||
}
|
||||
|
||||
public actual suspend fun writeLong(value: Long, endian: ByteOrder) {
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.writeLong(value)
|
||||
else _delegateBuf.writeLongLe(value)
|
||||
}
|
||||
|
||||
public actual suspend fun writeDouble(value: Double, endian: ByteOrder): Unit =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.writeDouble(value) else _delegateBuf.writeDoubleLe(value)
|
||||
|
||||
public actual suspend fun writeFloat(value: Float, endian: ByteOrder): Unit =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.writeFloat(value) else _delegateBuf.writeFloatLe(value)
|
||||
|
||||
public actual suspend fun writeBytes(bytes: ByteArray): Unit = _delegateBuf.write(bytes)
|
||||
public actual suspend fun writeBoolean(value: Boolean): Unit = _delegateBuf.writeByte(if (value) 0x01 else 0x00)
|
||||
|
||||
public actual suspend fun readByte(): Byte = _delegateBuf.readByte()
|
||||
public actual suspend fun readUByte(): UByte = this.readByte().toUByte()
|
||||
public actual suspend fun readShort(endian: ByteOrder): Short =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.readShort() else _delegateBuf.readShortLe()
|
||||
|
||||
public actual suspend fun readInt(endian: ByteOrder): Int =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.readInt() else _delegateBuf.readIntLe()
|
||||
|
||||
public actual suspend fun readLong(endian: ByteOrder): Long =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.readLong() else _delegateBuf.readLongLe()
|
||||
|
||||
public actual suspend fun readDouble(endian: ByteOrder): Double =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.readDouble() else _delegateBuf.readDoubleLe()
|
||||
|
||||
public actual suspend fun readFloat(endian: ByteOrder): Float =
|
||||
if (endian == ByteOrder.BIG_ENDIAN) _delegateBuf.readFloat() else _delegateBuf.readFloatLe()
|
||||
|
||||
public actual suspend fun readBytes(length: Int): ByteArray = _delegateBuf.readByteArray(length)
|
||||
public actual suspend fun readBoolean(): Boolean = _delegateBuf.readByte() != 0x00.toByte()
|
||||
public actual suspend fun readRemainingBytes(): ByteArray = this.toByteArray()
|
||||
|
||||
public actual suspend fun toByteArray(): ByteArray {
|
||||
val copy = _delegateBuf.peek()
|
||||
return try {
|
||||
copy.readByteArray()
|
||||
} finally {
|
||||
copy.close()
|
||||
}
|
||||
}
|
||||
|
||||
public actual suspend fun hasRemaining(): Boolean = !_delegateBuf.exhausted()
|
||||
public actual suspend fun close(): Unit = _delegateBuf.close()
|
||||
|
||||
public actual val size: Int get() = _delegateBuf.size.toInt()
|
||||
public actual val remaining: Long get() = _delegateBuf.remaining
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import io.ktor.utils.io.*
|
||||
import io.ktor.utils.io.bits.*
|
||||
|
||||
public actual open class ReadChannel public actual constructor() {
|
||||
|
||||
private lateinit var _readChannel: ByteReadChannel
|
||||
|
||||
public constructor(readChannel: ByteReadChannel) : this() {
|
||||
this._readChannel = readChannel
|
||||
}
|
||||
|
||||
public actual open suspend fun readByte(): Byte = _readChannel.readByte()
|
||||
public actual open suspend fun readBytes(length: Int): ByteArray = _readChannel.readByteArray(length)
|
||||
public actual open suspend fun readFully(out: ByteArray, start: Int, end: Int): Unit =
|
||||
_readChannel.readFully(out, start, end)
|
||||
|
||||
public actual open suspend fun readShort(endian: ByteOrder): Short {
|
||||
val bytes = readBytes(2)
|
||||
val v = ((bytes[0].toInt() and 0xFF shl 8) or (bytes[1].toInt() and 0xFF)).toShort()
|
||||
return if (endian == ByteOrder.BIG_ENDIAN) v else v.reverseByteOrder()
|
||||
}
|
||||
|
||||
public actual open suspend fun readInt(endian: ByteOrder): Int {
|
||||
val bytes = readBytes(4)
|
||||
val v = (bytes[0].toInt() and 0xFF shl 24) or
|
||||
(bytes[1].toInt() and 0xFF shl 16) or
|
||||
(bytes[2].toInt() and 0xFF shl 8) or
|
||||
(bytes[3].toInt() and 0xFF)
|
||||
return if (endian == ByteOrder.BIG_ENDIAN) v else v.reverseByteOrder()
|
||||
}
|
||||
|
||||
public actual open suspend fun readLong(endian: ByteOrder): Long {
|
||||
val bytes = readBytes(8)
|
||||
var v = 0L
|
||||
for (i in 0 until 8) {
|
||||
v = (v shl 8) or (bytes[i].toLong() and 0xFF)
|
||||
}
|
||||
return if (endian == ByteOrder.BIG_ENDIAN) v else v.reverseByteOrder()
|
||||
}
|
||||
}
|
||||
|
||||
public actual open class WriteChannel public actual constructor() {
|
||||
private lateinit var _writeChannel: ByteWriteChannel
|
||||
|
||||
public constructor(writeChannel: ByteWriteChannel) : this() {
|
||||
_writeChannel = writeChannel
|
||||
}
|
||||
|
||||
public actual open suspend fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int): Unit =
|
||||
_writeChannel.writeFully(value, startIndex, endIndex)
|
||||
|
||||
public actual open suspend fun flush(): Unit = _writeChannel.flush()
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.stream
|
||||
|
||||
import cn.rtast.libmc.LibMCContext
|
||||
import io.ktor.network.sockets.*
|
||||
import io.ktor.utils.io.core.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.io.readByteArray
|
||||
|
||||
public actual class Socket public actual constructor(host: String, port: Int, context: LibMCContext) {
|
||||
private val ctx = context
|
||||
private val socket = runBlocking { aSocket(ctx._selectorManager).tcp().connect(host, port) }
|
||||
|
||||
public actual fun openReadChannel(): ReadChannel = ReadChannel(socket.openReadChannel())
|
||||
public actual fun openWriteChannel(): WriteChannel =
|
||||
WriteChannel(socket.openWriteChannel(autoFlush = true))
|
||||
|
||||
public actual fun close() {
|
||||
socket.close()
|
||||
if (ctx._autoCloseSelectorManager) ctx._selectorManager.close()
|
||||
}
|
||||
}
|
||||
|
||||
public actual class UdpSocket public actual constructor(host: String, port: Int, context: LibMCContext) {
|
||||
private val ctx = context
|
||||
|
||||
// use bind to create an unconnected socket
|
||||
private val socket = runBlocking { aSocket(ctx._selectorManager).udp().bind() }
|
||||
private val remoteAddress = InetSocketAddress(host, port)
|
||||
|
||||
public actual suspend fun sendAndReceive(data: ByteArray): ByteArray {
|
||||
val packet = buildPacket { writeFully(data) }
|
||||
socket.send(Datagram(packet, remoteAddress))
|
||||
return socket.receive().packet.readByteArray()
|
||||
}
|
||||
|
||||
public actual fun close() {
|
||||
socket.close()
|
||||
if (ctx._autoCloseSelectorManager) ctx._selectorManager.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user