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
|
||||
|
||||
import java.io.*
|
||||
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.nextUInt
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
|
@ -17,15 +9,13 @@ import kotlinx.coroutines.cancel
|
|||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.coroutines.yield
|
||||
|
||||
class KtRconClient(
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val password: Password,
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val password: Password,
|
||||
) : AutoCloseable {
|
||||
|
||||
private val rconClientScope = CoroutineScope(SupervisorJob())
|
||||
|
|
@ -46,22 +36,22 @@ class KtRconClient(
|
|||
|
||||
runCatching {
|
||||
val reader = socket.getInputStream()
|
||||
.bufferedReader()
|
||||
.lineSequence()
|
||||
.asFlow()
|
||||
.onEach {
|
||||
println("socket response: [$it]")
|
||||
}
|
||||
.launchIn(rconClientScope)
|
||||
.bufferedReader()
|
||||
.lineSequence()
|
||||
.asFlow()
|
||||
.onEach {
|
||||
println("socket response: [$it]")
|
||||
}
|
||||
.launchIn(rconClientScope)
|
||||
|
||||
// while (socket.isConnected) {
|
||||
// println("response: ${input.read().toByte().toInt().toChar()}")
|
||||
|
||||
println("is listener active? ${reader.isActive}")
|
||||
println("is listener active? ${reader.isActive}")
|
||||
|
||||
reader.invokeOnCompletion {
|
||||
println("cancelled input stream job")
|
||||
}
|
||||
reader.invokeOnCompletion {
|
||||
println("cancelled input stream job")
|
||||
}
|
||||
|
||||
}
|
||||
// }
|
||||
|
|
@ -121,4 +111,4 @@ class KtRconClient(
|
|||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,34 +1,20 @@
|
|||
package dev.adamko.ktrcon
|
||||
|
||||
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.nextUInt
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
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.runBlocking
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.coroutines.yield
|
||||
|
||||
class KtRconClient2(
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val password: Password,
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val password: Password,
|
||||
) : AutoCloseable {
|
||||
|
||||
private val rconClientScope = CoroutineScope(SupervisorJob())
|
||||
|
|
@ -100,4 +86,4 @@ class KtRconClient2(
|
|||
client.close()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,47 +1,28 @@
|
|||
package dev.adamko.ktrcon
|
||||
|
||||
import io.ktor.network.selector.ActorSelectorManager
|
||||
import io.ktor.network.sockets.InetSocketAddress
|
||||
import io.ktor.network.sockets.aSocket
|
||||
import io.ktor.network.sockets.openReadChannel
|
||||
import io.ktor.network.sockets.openWriteChannel
|
||||
import io.ktor.util.decodeString
|
||||
import io.ktor.utils.io.ByteReadChannel
|
||||
import io.ktor.utils.io.ByteWriteChannel
|
||||
import io.ktor.utils.io.core.isNotEmpty
|
||||
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.nextUInt
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
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.runBlocking
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.coroutines.yield
|
||||
|
||||
class KtRconClient3(
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val password: Password,
|
||||
private val host: String,
|
||||
private val port: Int,
|
||||
private val password: Password,
|
||||
) : AutoCloseable {
|
||||
|
||||
private val rconClientJob = SupervisorJob()
|
||||
|
|
@ -54,8 +35,8 @@ class KtRconClient3(
|
|||
val socket = runBlocking {
|
||||
val selector = ActorSelectorManager(rconClientJob)
|
||||
aSocket(selector)
|
||||
.tcp()
|
||||
.connect(InetSocketAddress(host, port))
|
||||
.tcp()
|
||||
.connect(InetSocketAddress(host, port))
|
||||
}
|
||||
input = socket.openReadChannel()
|
||||
output = socket.openWriteChannel()
|
||||
|
|
@ -74,7 +55,7 @@ class KtRconClient3(
|
|||
while (packet.isNotEmpty) {
|
||||
val bytes = packet.readBytes()
|
||||
println(
|
||||
"received ${bytes.size} bytes: ${bytes.joinToString { "$it" }} - ${bytes.decodeToString()}"
|
||||
"received ${bytes.size} bytes: ${bytes.joinToString { "$it" }} - ${bytes.decodeToString()}"
|
||||
)
|
||||
yield()
|
||||
}
|
||||
|
|
@ -127,4 +108,4 @@ class KtRconClient3(
|
|||
rconClientScope.cancel("Closing ${this::class}")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package dev.adamko.ktrcon
|
||||
|
||||
import io.ktor.utils.io.bits.reverseByteOrder
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
|
|
@ -28,9 +27,9 @@ import java.nio.ByteOrder
|
|||
* to be executed, or the server's response to a request.
|
||||
*/
|
||||
data class Packet(
|
||||
val id: UInt,
|
||||
val type: PacketType,
|
||||
val bodyRaw: String,
|
||||
val id: UInt,
|
||||
val type: PacketType,
|
||||
val bodyRaw: String,
|
||||
) {
|
||||
private val bodyTerminator: Byte = NULL_CHAR_BYTE
|
||||
|
||||
|
|
@ -46,7 +45,7 @@ data class Packet(
|
|||
val packetTerminatorSize = 1 * Byte.SIZE_BYTES
|
||||
val total = idSize + typeSize + bodySize + bodyTerminatorSize + packetTerminatorSize
|
||||
println(
|
||||
"packetSize: $total = $idSize + $typeSize + $bodySize + $bodyTerminatorSize + $packetTerminatorSize"
|
||||
"packetSize: $total = $idSize + $typeSize + $bodySize + $bodyTerminatorSize + $packetTerminatorSize"
|
||||
)
|
||||
|
||||
total
|
||||
|
|
@ -81,20 +80,20 @@ data class Packet(
|
|||
fun toByteBuffer2(): ByteBuffer {
|
||||
|
||||
return ByteBuffer
|
||||
.allocate(totalSize)
|
||||
.order(ByteOrder.LITTLE_ENDIAN)
|
||||
// size
|
||||
.putInt(size)
|
||||
// ID
|
||||
.putInt(id.toInt())
|
||||
// type
|
||||
.putInt(type.value)
|
||||
// body
|
||||
.put(body)
|
||||
// body terminator
|
||||
.put(bodyTerminator)
|
||||
// packet terminator
|
||||
.put(RCON_PACKET_TERMINATOR)
|
||||
.allocate(totalSize)
|
||||
.order(ByteOrder.LITTLE_ENDIAN)
|
||||
// size
|
||||
.putInt(size)
|
||||
// ID
|
||||
.putInt(id.toInt())
|
||||
// type
|
||||
.putInt(type.value)
|
||||
// body
|
||||
.put(body)
|
||||
// body terminator
|
||||
.put(bodyTerminator)
|
||||
// packet terminator
|
||||
.put(RCON_PACKET_TERMINATOR)
|
||||
// .asReadOnlyBuffer()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue