Initial commit

This commit is contained in:
2026-09-03 19:32:46 +08:00
commit b3583ba48d
25 files changed
+1411

No files matched your search

@@ -0,0 +1,24 @@
/*
* Copyright © 2026 RTAkland
* Author: RTAkland
* Date: 2026/9/3
*/
package cn.rtast.mcping.platform
import kotlinx.io.Buffer
import kotlinx.io.readByteArray
internal actual class PlatformBuffer actual constructor() {
private val _delegateBuf = Buffer()
actual fun writeByte(value: Byte) = _delegateBuf.writeByte(value)
actual fun writeBytes(bytes: ByteArray) = _delegateBuf.write(bytes)
actual fun readByte(): Byte = _delegateBuf.readByte()
actual fun readBytes(length: Int): ByteArray = _delegateBuf.readByteArray(length)
actual fun toByteArray(): ByteArray = _delegateBuf.peek().readByteArray()
actual val size: Int
get() = _delegateBuf.size.toInt()
}
@@ -0,0 +1,35 @@
/*
* Copyright © 2026 RTAkland
* Author: RTAkland
* Date: 2026/9/3
*/
package cn.rtast.mcping.platform
import io.ktor.utils.io.*
import kotlinx.coroutines.runBlocking
internal actual class PlatformReadChannel {
private val _readChannel: ByteReadChannel
constructor(readChannel: ByteReadChannel) {
_readChannel = readChannel
}
actual fun readByte(): Byte = runBlocking { _readChannel.readByte() }
actual fun readBytes(length: Int): ByteArray = runBlocking { _readChannel.readByteArray(length) }
actual fun readFully(out: ByteArray, start: Int, end: Int) = runBlocking { _readChannel.readFully(out, start, end) }
}
internal actual class PlatformWriteChannel {
private val _writeChannel: ByteWriteChannel
constructor(writeChannel: ByteWriteChannel) {
_writeChannel = writeChannel
}
actual fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int) =
runBlocking { _writeChannel.writeFully(value, startIndex, endIndex) }
actual fun flush() = runBlocking { _writeChannel.flush() }
}
@@ -0,0 +1,28 @@
/*
* Copyright © 2026 RTAkland
* Author: RTAkland
* Date: 2026/9/3
*/
package cn.rtast.mcping.platform
import io.ktor.network.selector.*
import io.ktor.network.sockets.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.runBlocking
internal actual class PlatformSocket internal actual constructor(host: String, port: Int) {
private val sm = SelectorManager(Dispatchers.IO)
private val socket = runBlocking { aSocket(sm).tcp().connect(host, port) }
actual fun openReadChannel(): PlatformReadChannel = PlatformReadChannel(socket.openReadChannel())
actual fun openWriteChannel(): PlatformWriteChannel =
PlatformWriteChannel(socket.openWriteChannel(autoFlush = true))
actual fun close() {
socket.close()
sm.close()
}
}