bump ktor to v2
This commit is contained in:
parent
a78ffb794a
commit
0b2bd7d10d
4 changed files with 45 additions and 89 deletions
|
|
@ -1,14 +1,6 @@
|
||||||
package dev.adamko.ktrcon
|
package dev.adamko.ktrcon
|
||||||
|
|
||||||
import java.io.*
|
|
||||||
import java.net.Socket
|
import java.net.Socket
|
||||||
import java.net.SocketException
|
|
||||||
import java.nio.BufferUnderflowException
|
|
||||||
import java.nio.ByteBuffer
|
|
||||||
import java.nio.ByteOrder
|
|
||||||
import java.nio.channels.AsynchronousSocketChannel
|
|
||||||
import kotlin.coroutines.coroutineContext
|
|
||||||
import kotlin.coroutines.suspendCoroutine
|
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
import kotlin.random.nextUInt
|
import kotlin.random.nextUInt
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
|
@ -17,15 +9,13 @@ import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.flow.asFlow
|
import kotlinx.coroutines.flow.asFlow
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.supervisorScope
|
import kotlinx.coroutines.supervisorScope
|
||||||
import kotlinx.coroutines.yield
|
|
||||||
|
|
||||||
class KtRconClient(
|
class KtRconClient(
|
||||||
private val host: String,
|
private val host: String,
|
||||||
private val port: Int,
|
private val port: Int,
|
||||||
private val password: Password,
|
private val password: Password,
|
||||||
) : AutoCloseable {
|
) : AutoCloseable {
|
||||||
|
|
||||||
private val rconClientScope = CoroutineScope(SupervisorJob())
|
private val rconClientScope = CoroutineScope(SupervisorJob())
|
||||||
|
|
@ -46,22 +36,22 @@ class KtRconClient(
|
||||||
|
|
||||||
runCatching {
|
runCatching {
|
||||||
val reader = socket.getInputStream()
|
val reader = socket.getInputStream()
|
||||||
.bufferedReader()
|
.bufferedReader()
|
||||||
.lineSequence()
|
.lineSequence()
|
||||||
.asFlow()
|
.asFlow()
|
||||||
.onEach {
|
.onEach {
|
||||||
println("socket response: [$it]")
|
println("socket response: [$it]")
|
||||||
}
|
}
|
||||||
.launchIn(rconClientScope)
|
.launchIn(rconClientScope)
|
||||||
|
|
||||||
// while (socket.isConnected) {
|
// while (socket.isConnected) {
|
||||||
// println("response: ${input.read().toByte().toInt().toChar()}")
|
// println("response: ${input.read().toByte().toInt().toChar()}")
|
||||||
|
|
||||||
println("is listener active? ${reader.isActive}")
|
println("is listener active? ${reader.isActive}")
|
||||||
|
|
||||||
reader.invokeOnCompletion {
|
reader.invokeOnCompletion {
|
||||||
println("cancelled input stream job")
|
println("cancelled input stream job")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,20 @@
|
||||||
package dev.adamko.ktrcon
|
package dev.adamko.ktrcon
|
||||||
|
|
||||||
import java.io.*
|
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.net.Socket
|
|
||||||
import java.net.SocketException
|
|
||||||
import java.nio.BufferUnderflowException
|
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.ByteOrder
|
|
||||||
import java.nio.channels.AsynchronousSocketChannel
|
import java.nio.channels.AsynchronousSocketChannel
|
||||||
import java.nio.channels.CompletionHandler
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import kotlin.coroutines.coroutineContext
|
|
||||||
import kotlin.coroutines.suspendCoroutine
|
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
import kotlin.random.nextUInt
|
import kotlin.random.nextUInt
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import kotlinx.coroutines.supervisorScope
|
import kotlinx.coroutines.supervisorScope
|
||||||
import kotlinx.coroutines.yield
|
|
||||||
|
|
||||||
class KtRconClient2(
|
class KtRconClient2(
|
||||||
private val host: String,
|
private val host: String,
|
||||||
private val port: Int,
|
private val port: Int,
|
||||||
private val password: Password,
|
private val password: Password,
|
||||||
) : AutoCloseable {
|
) : AutoCloseable {
|
||||||
|
|
||||||
private val rconClientScope = CoroutineScope(SupervisorJob())
|
private val rconClientScope = CoroutineScope(SupervisorJob())
|
||||||
|
|
|
||||||
|
|
@ -1,47 +1,28 @@
|
||||||
package dev.adamko.ktrcon
|
package dev.adamko.ktrcon
|
||||||
|
|
||||||
import io.ktor.network.selector.ActorSelectorManager
|
import io.ktor.network.selector.ActorSelectorManager
|
||||||
|
import io.ktor.network.sockets.InetSocketAddress
|
||||||
import io.ktor.network.sockets.aSocket
|
import io.ktor.network.sockets.aSocket
|
||||||
import io.ktor.network.sockets.openReadChannel
|
import io.ktor.network.sockets.openReadChannel
|
||||||
import io.ktor.network.sockets.openWriteChannel
|
import io.ktor.network.sockets.openWriteChannel
|
||||||
import io.ktor.util.decodeString
|
|
||||||
import io.ktor.utils.io.ByteReadChannel
|
import io.ktor.utils.io.ByteReadChannel
|
||||||
import io.ktor.utils.io.ByteWriteChannel
|
import io.ktor.utils.io.ByteWriteChannel
|
||||||
import io.ktor.utils.io.core.isNotEmpty
|
import io.ktor.utils.io.core.isNotEmpty
|
||||||
import io.ktor.utils.io.core.readBytes
|
import io.ktor.utils.io.core.readBytes
|
||||||
import io.ktor.utils.io.readRemaining
|
|
||||||
import io.ktor.utils.io.readUTF8Line
|
|
||||||
import io.ktor.utils.io.writeIntLittleEndian
|
|
||||||
import io.ktor.utils.io.writeStringUtf8
|
|
||||||
import java.io.*
|
|
||||||
import java.net.InetSocketAddress
|
|
||||||
import java.net.Socket
|
|
||||||
import java.net.SocketException
|
|
||||||
import java.nio.BufferUnderflowException
|
|
||||||
import java.nio.ByteBuffer
|
|
||||||
import java.nio.ByteOrder
|
|
||||||
import java.nio.channels.AsynchronousSocketChannel
|
|
||||||
import java.nio.channels.CompletionHandler
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import kotlin.coroutines.coroutineContext
|
|
||||||
import kotlin.coroutines.suspendCoroutine
|
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
import kotlin.random.nextUInt
|
import kotlin.random.nextUInt
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.supervisorScope
|
import kotlinx.coroutines.supervisorScope
|
||||||
import kotlinx.coroutines.yield
|
import kotlinx.coroutines.yield
|
||||||
|
|
||||||
class KtRconClient3(
|
class KtRconClient3(
|
||||||
private val host: String,
|
private val host: String,
|
||||||
private val port: Int,
|
private val port: Int,
|
||||||
private val password: Password,
|
private val password: Password,
|
||||||
) : AutoCloseable {
|
) : AutoCloseable {
|
||||||
|
|
||||||
private val rconClientJob = SupervisorJob()
|
private val rconClientJob = SupervisorJob()
|
||||||
|
|
@ -54,8 +35,8 @@ class KtRconClient3(
|
||||||
val socket = runBlocking {
|
val socket = runBlocking {
|
||||||
val selector = ActorSelectorManager(rconClientJob)
|
val selector = ActorSelectorManager(rconClientJob)
|
||||||
aSocket(selector)
|
aSocket(selector)
|
||||||
.tcp()
|
.tcp()
|
||||||
.connect(InetSocketAddress(host, port))
|
.connect(InetSocketAddress(host, port))
|
||||||
}
|
}
|
||||||
input = socket.openReadChannel()
|
input = socket.openReadChannel()
|
||||||
output = socket.openWriteChannel()
|
output = socket.openWriteChannel()
|
||||||
|
|
@ -74,7 +55,7 @@ class KtRconClient3(
|
||||||
while (packet.isNotEmpty) {
|
while (packet.isNotEmpty) {
|
||||||
val bytes = packet.readBytes()
|
val bytes = packet.readBytes()
|
||||||
println(
|
println(
|
||||||
"received ${bytes.size} bytes: ${bytes.joinToString { "$it" }} - ${bytes.decodeToString()}"
|
"received ${bytes.size} bytes: ${bytes.joinToString { "$it" }} - ${bytes.decodeToString()}"
|
||||||
)
|
)
|
||||||
yield()
|
yield()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package dev.adamko.ktrcon
|
package dev.adamko.ktrcon
|
||||||
|
|
||||||
import io.ktor.utils.io.bits.reverseByteOrder
|
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.ByteOrder
|
import java.nio.ByteOrder
|
||||||
|
|
||||||
|
|
@ -28,9 +27,9 @@ import java.nio.ByteOrder
|
||||||
* to be executed, or the server's response to a request.
|
* to be executed, or the server's response to a request.
|
||||||
*/
|
*/
|
||||||
data class Packet(
|
data class Packet(
|
||||||
val id: UInt,
|
val id: UInt,
|
||||||
val type: PacketType,
|
val type: PacketType,
|
||||||
val bodyRaw: String,
|
val bodyRaw: String,
|
||||||
) {
|
) {
|
||||||
private val bodyTerminator: Byte = NULL_CHAR_BYTE
|
private val bodyTerminator: Byte = NULL_CHAR_BYTE
|
||||||
|
|
||||||
|
|
@ -46,7 +45,7 @@ data class Packet(
|
||||||
val packetTerminatorSize = 1 * Byte.SIZE_BYTES
|
val packetTerminatorSize = 1 * Byte.SIZE_BYTES
|
||||||
val total = idSize + typeSize + bodySize + bodyTerminatorSize + packetTerminatorSize
|
val total = idSize + typeSize + bodySize + bodyTerminatorSize + packetTerminatorSize
|
||||||
println(
|
println(
|
||||||
"packetSize: $total = $idSize + $typeSize + $bodySize + $bodyTerminatorSize + $packetTerminatorSize"
|
"packetSize: $total = $idSize + $typeSize + $bodySize + $bodyTerminatorSize + $packetTerminatorSize"
|
||||||
)
|
)
|
||||||
|
|
||||||
total
|
total
|
||||||
|
|
@ -81,20 +80,20 @@ data class Packet(
|
||||||
fun toByteBuffer2(): ByteBuffer {
|
fun toByteBuffer2(): ByteBuffer {
|
||||||
|
|
||||||
return ByteBuffer
|
return ByteBuffer
|
||||||
.allocate(totalSize)
|
.allocate(totalSize)
|
||||||
.order(ByteOrder.LITTLE_ENDIAN)
|
.order(ByteOrder.LITTLE_ENDIAN)
|
||||||
// size
|
// size
|
||||||
.putInt(size)
|
.putInt(size)
|
||||||
// ID
|
// ID
|
||||||
.putInt(id.toInt())
|
.putInt(id.toInt())
|
||||||
// type
|
// type
|
||||||
.putInt(type.value)
|
.putInt(type.value)
|
||||||
// body
|
// body
|
||||||
.put(body)
|
.put(body)
|
||||||
// body terminator
|
// body terminator
|
||||||
.put(bodyTerminator)
|
.put(bodyTerminator)
|
||||||
// packet terminator
|
// packet terminator
|
||||||
.put(RCON_PACKET_TERMINATOR)
|
.put(RCON_PACKET_TERMINATOR)
|
||||||
// .asReadOnlyBuffer()
|
// .asReadOnlyBuffer()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue