Implement join online-mode server process

This commit is contained in:
2026-09-07 03:15:53 +08:00
parent a88888ba4d
commit 5dc4e8bc0d
44 files changed
+696 -699

No files matched your search

-2
View File
@@ -8,8 +8,6 @@ kotlin {
linuxArm64()
macosArm64()
mingwX64()
iosArm64()
iosSimulatorArm64()
jvm { compilerOptions.jvmTarget = JvmTarget.JVM_1_8 }
sourceSets {
@@ -7,18 +7,18 @@
package cn.rtast.libmc.common
public expect class ReadChannel {
public fun readByte(): Byte
public fun readShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short
public fun readInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int
public fun readLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long
public fun readBytes(length: Int): ByteArray
public fun readFully(out: ByteArray, start: Int = 0, end: Int = out.size)
public expect open class ReadChannel() {
public open fun readByte(): Byte
public open fun readShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short
public open fun readInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int
public open fun readLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long
public open fun readBytes(length: Int): ByteArray
public open fun readFully(out: ByteArray, start: Int = 0, end: Int = out.size)
}
public expect class WriteChannel {
public fun writeFully(value: ByteArray, startIndex: Int = 0, endIndex: Int = value.size)
public fun flush()
public expect open class WriteChannel() {
public open fun writeFully(value: ByteArray, startIndex: Int = 0, endIndex: Int = value.size)
public open fun flush()
}
public fun ReadChannel.readVarInt(): Int {
@@ -4,53 +4,55 @@
* Date: 2026/9/3
*/
package cn.rtast.libmc.common
import java.io.EOFException
import java.io.InputStream
import java.io.OutputStream
public actual class ReadChannel(private val _inputStream: InputStream) {
public actual open class ReadChannel public actual constructor() {
private lateinit var _inputStream: InputStream
public actual fun readByte(): Byte = _inputStream.read().toByte()
public actual fun readBytes(length: Int): ByteArray = _inputStream.readNBytes(length)
public constructor(inputStream: InputStream) : this() {
this._inputStream = inputStream
}
public actual fun readFully(out: ByteArray, start: Int, end: Int) {
public actual open 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 IllegalStateException("End of stream")
if (read == -1) throw EOFException("End of stream reached")
bytesRead += read
}
}
public actual fun readShort(endian: ByteOrder): Short = readBytes(2).toShort(endian)
public actual fun readInt(endian: ByteOrder): Int = readBytes(4).toInt(endian)
public actual fun readLong(endian: ByteOrder): Long = readBytes(8).toLong(endian)
public actual open fun readByte(): Byte {
val buf = ByteArray(1)
readFully(buf, 0, 1)
return buf[0]
}
public actual open fun readBytes(length: Int): ByteArray {
val bytes = ByteArray(length)
readFully(bytes, 0, length)
return bytes
}
public actual open fun readShort(endian: ByteOrder): Short = readBytes(2).toShort(endian)
public actual open fun readInt(endian: ByteOrder): Int = readBytes(4).toInt(endian)
public actual open fun readLong(endian: ByteOrder): Long = readBytes(8).toLong(endian)
}
public actual class WriteChannel {
private val _outputStream: OutputStream
public actual open class WriteChannel public actual constructor() {
private lateinit var _outputStream: OutputStream
public constructor(outputStream: OutputStream) {
public constructor(outputStream: OutputStream) : this() {
_outputStream = outputStream
}
public actual fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int): Unit =
public actual open fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int): Unit =
_outputStream.write(value, startIndex, endIndex - startIndex)
public actual fun flush(): Unit = _outputStream.flush()
}
private fun InputStream.readNBytes(length: Int): ByteArray {
val buffer = ByteArray(length)
var totalRead = 0
while (totalRead < length) {
val read = this.read(buffer, totalRead, length - totalRead)
if (read == -1) throw EOFException()
totalRead += read
}
return buffer
public actual open fun flush(): Unit = _outputStream.flush()
}
@@ -10,38 +10,53 @@ import io.ktor.utils.io.*
import io.ktor.utils.io.bits.*
import kotlinx.coroutines.runBlocking
public actual class ReadChannel(private val _readChannel: ByteReadChannel) {
public actual open class ReadChannel public actual constructor() {
public actual fun readByte(): Byte = runBlocking { _readChannel.readByte() }
public actual fun readBytes(length: Int): ByteArray = runBlocking { _readChannel.readByteArray(length) }
public actual fun readFully(out: ByteArray, start: Int, end: Int): Unit =
private lateinit var _readChannel: ByteReadChannel
public constructor(readChannel: ByteReadChannel) : this() {
this._readChannel = readChannel
}
public actual open fun readByte(): Byte = runBlocking { _readChannel.readByte() }
public actual open fun readBytes(length: Int): ByteArray = runBlocking { _readChannel.readByteArray(length) }
public actual open fun readFully(out: ByteArray, start: Int, end: Int): Unit =
runBlocking { _readChannel.readFully(out, start, end) }
public actual fun readShort(endian: ByteOrder): Short = runBlocking {
val v = _readChannel.readShort()
if (endian == ByteOrder.BIG_ENDIAN) v else v.reverseByteOrder()
public actual open 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 fun readInt(endian: ByteOrder): Int = runBlocking {
val v = _readChannel.readInt()
if (endian == ByteOrder.BIG_ENDIAN) v else v.reverseByteOrder()
public actual open 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 fun readLong(endian: ByteOrder): Long = runBlocking {
val v = _readChannel.readLong()
if (endian == ByteOrder.BIG_ENDIAN) v else v.reverseByteOrder()
public actual open 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 class WriteChannel {
private val _writeChannel: ByteWriteChannel
public actual open class WriteChannel public actual constructor() {
private lateinit var _writeChannel: ByteWriteChannel
public constructor(writeChannel: ByteWriteChannel) {
public constructor(writeChannel: ByteWriteChannel) : this() {
_writeChannel = writeChannel
}
public actual fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int): Unit =
public actual open fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int): Unit =
runBlocking { _writeChannel.writeFully(value, startIndex, endIndex) }
public actual fun flush(): Unit = runBlocking { _writeChannel.flush() }
public actual open fun flush(): Unit = runBlocking { _writeChannel.flush() }
}