Skip to content

Commit

Permalink
Update to Ktor 3.0.0-beta-2
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Jul 18, 2024
1 parent fc4007a commit 4ea3199
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ tasks {

afterEvaluate {
val compilationTasks = kotlin.targets.flatMap {
listOf("compileKotlin${it.name.capitalized()}", "${it.name}SourcesJar")
listOf("compileKotlin${it.name.replaceFirstChar(Char::titlecase)}", "${it.name}SourcesJar")
}
for (task in compilationTasks) {
named(task) {
Expand Down
14 changes: 7 additions & 7 deletions common/src/nonJvmMain/kotlin/DiscordBitSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package dev.kord.common

import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.integer.Sign
import io.ktor.utils.io.core.*
import kotlinx.io.Buffer
import kotlinx.io.readByteArray

internal actual fun formatIntegerFromLittleEndianLongArray(data: LongArray) =
withBuffer(data.size * Long.SIZE_BYTES) {
// need to convert from little-endian data to big-endian expected by BigInteger
writeFully(data.reversedArray())
BigInteger.fromByteArray(readBytes(), Sign.POSITIVE).toString()
}
internal actual fun formatIntegerFromLittleEndianLongArray(data: LongArray): String {
val buffer = Buffer()
data.reversedArray().forEach(buffer::writeLong)
return BigInteger.fromByteArray(buffer.readByteArray(), Sign.POSITIVE).toString()
}

internal actual fun parseNonNegativeIntegerToBigEndianByteArray(value: String): ByteArray = BigInteger
.parseString(value)
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ kotlinx-datetime = "0.6.0" # https://github.com/Kotlin/kotlinx-datetime
kord-cache = "0.5.4" # https://github.com/kordlib/cache

# implementation dependencies
kotlinx-io = "0.5.1" # https://github.com/Kotlin/kotlinx-io
kotlin-logging = "7.0.0" # https://github.com/oshai/kotlin-logging
slf4j = "2.0.13" # https://www.slf4j.org
kotlin-node = "20.14.10-pre.773" # https://github.com/JetBrains/kotlin-wrappers
Expand All @@ -32,7 +33,8 @@ dokka = "1.9.20" # https://github.com/Kotlin/dokka
kotlinx-atomicfu = "0.25.0" # https://github.com/Kotlin/kotlinx-atomicfu
binary-compatibility-validator = "0.15.1" # https://github.com/Kotlin/binary-compatibility-validator
buildconfig = "5.4.0" # https://github.com/gmazzo/gradle-buildconfig-plugin

kord-gradle-tools = "1.6.2" # https://github.com/kordlib/gradle-tools/blob/main/build.gradle.kts#L10
maven-publish-plugin = "0.29.0" # https://github.com/vanniktech/gradle-maven-publish-plugin

[libraries]

Expand Down
2 changes: 1 addition & 1 deletion rest/src/commonTest/kotlin/json/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import dev.kord.test.file as platformFile
import dev.kord.test.readFile as platformReadFile

internal suspend fun file(name: String): String = platformFile("rest", "json/$name.json")
internal suspend fun readFile(name: String): ByteReadChannel = platformReadFile("rest", name)
internal suspend fun readFile(name: String): CountedByteReadChannel = platformReadFile("rest", name)
3 changes: 1 addition & 2 deletions rest/src/commonTest/kotlin/request/MessageRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import dev.kord.test.Platform
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.request.forms.*
import io.ktor.utils.io.*
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Clock
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -67,7 +68,6 @@ class MessageRequests {
val fileChannel = readFile("images/kord.png")

with(fileChannel) {
if (Platform.IS_JVM) assertFalse(isClosedForWrite) // only read lazily on jvm
assertFalse(isClosedForRead)
assertEquals(0L, totalBytesRead)

Expand All @@ -76,7 +76,6 @@ class MessageRequests {
}
assertEquals(mockMessage, createdMessage)

assertTrue(isClosedForWrite)
assertTrue(isClosedForRead)
assertTrue(totalBytesRead > 0L)
}
Expand Down
3 changes: 2 additions & 1 deletion test-kit/src/commonMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ expect object Platform {

expect fun getEnv(name: String): String?
expect suspend fun file(project: String, path: String): String
expect suspend fun readFile(project: String, path: String): ByteReadChannel
suspend fun readFile(project: String, path: String) = readFile0(project, path).counted()
expect suspend fun readFile0(project: String, path: String): ByteReadChannel
2 changes: 1 addition & 1 deletion test-kit/src/jsMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ actual fun getEnv(name: String) = process.env[name]
actual suspend fun file(project: String, path: String): String =
if (Platform.IS_NODE) nodeFile(project, path) else TODO("Browser JS is not supported yet")

actual suspend fun readFile(project: String, path: String): ByteReadChannel =
actual suspend fun readFile0(project: String, path: String): ByteReadChannel =
if (Platform.IS_NODE) nodeReadFile(project, path) else TODO("Browser JS is not supported yet")
3 changes: 2 additions & 1 deletion test-kit/src/jvmMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:JvmName("PlatformJvm")
package dev.kord.test

import io.ktor.utils.io.*
Expand All @@ -14,5 +15,5 @@ actual object Platform {

actual fun getEnv(name: String): String? = System.getenv(name)
actual suspend fun file(project: String, path: String): String = ClassLoader.getSystemResource(path).readText()
actual suspend fun readFile(project: String, path: String): ByteReadChannel =
actual suspend fun readFile0(project: String, path: String): ByteReadChannel =
ClassLoader.getSystemResourceAsStream(path)!!.toByteReadChannel()
2 changes: 1 addition & 1 deletion test-kit/src/nativeMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ actual fun getEnv(name: String) = getenv(name)?.toKString()

actual suspend fun file(project: String, path: String): String = read(project, path, Source::readString)

actual suspend fun readFile(project: String, path: String): ByteReadChannel =
actual suspend fun readFile0(project: String, path: String): ByteReadChannel =
read(project, path) { ByteReadChannel(readByteArray()) }

private inline fun <T> read(project: String, path: String, readerAction: Source.() -> T): T {
Expand Down
2 changes: 1 addition & 1 deletion voice/api/voice.api
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ public final class dev/kord/voice/udp/RTPPacket$Builder {
}

public final class dev/kord/voice/udp/RTPPacket$Companion {
public final fun fromPacket (Lio/ktor/utils/io/core/ByteReadPacket;)Ldev/kord/voice/udp/RTPPacket;
public final fun fromPacket (Lkotlinx/io/Source;)Ldev/kord/voice/udp/RTPPacket;
}

public final class dev/kord/voice/udp/RTPPacketKt {
Expand Down
20 changes: 13 additions & 7 deletions voice/api/voice.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ abstract interface dev.kord.voice.udp/VoiceUdpSocket { // dev.kord.voice.udp/Voi
final fun none(): dev.kord.voice.udp/VoiceUdpSocket // dev.kord.voice.udp/VoiceUdpSocket.Companion.none|none(){}[0]
}

// Targets: [apple, linux]
abstract fun all(io.ktor.network.sockets/InetSocketAddress): kotlinx.coroutines.flow/Flow<io.ktor.utils.io.core/ByteReadPacket> // dev.kord.voice.udp/VoiceUdpSocket.all|all(io.ktor.network.sockets.InetSocketAddress){}[0]

// Targets: [apple, linux]
abstract suspend fun send(io.ktor.network.sockets/InetSocketAddress, dev.kord.voice.io/ByteArrayView) // dev.kord.voice.udp/VoiceUdpSocket.send|send(io.ktor.network.sockets.InetSocketAddress;dev.kord.voice.io.ByteArrayView){}[0]

// Targets: [apple]
abstract fun all(io.ktor.network.sockets/InetSocketAddress): kotlinx.coroutines.flow/Flow<io.ktor.utils.io.core/ByteReadPacket> // dev.kord.voice.udp/VoiceUdpSocket.all|all(io.ktor.network.sockets.InetSocketAddress){}[0]

// Targets: [js, mingwX64]
abstract fun all(dev.kord.voice.udp/SocketAddress): kotlinx.coroutines.flow/Flow<io.ktor.utils.io.core/ByteReadPacket> // dev.kord.voice.udp/VoiceUdpSocket.all|all(dev.kord.voice.udp.SocketAddress){}[0]
abstract fun all(dev.kord.voice.udp/SocketAddress): kotlinx.coroutines.flow/Flow<kotlinx.io/Source> // dev.kord.voice.udp/VoiceUdpSocket.all|all(dev.kord.voice.udp.SocketAddress){}[0]

// Targets: [js, mingwX64]
abstract suspend fun send(dev.kord.voice.udp/SocketAddress, dev.kord.voice.io/ByteArrayView) // dev.kord.voice.udp/VoiceUdpSocket.send|send(dev.kord.voice.udp.SocketAddress;dev.kord.voice.io.ByteArrayView){}[0]

// Targets: [linux]
abstract fun all(io.ktor.network.sockets/InetSocketAddress): kotlinx.coroutines.flow/Flow<kotlinx.io/Source> // dev.kord.voice.udp/VoiceUdpSocket.all|all(io.ktor.network.sockets.InetSocketAddress){}[0]
}

sealed interface dev.kord.voice.encryption.strategies/NonceStrategy { // dev.kord.voice.encryption.strategies/NonceStrategy|null[0]
Expand Down Expand Up @@ -821,7 +824,7 @@ final class dev.kord.voice.udp/RTPPacket { // dev.kord.voice.udp/RTPPacket|null[
}

final object Companion { // dev.kord.voice.udp/RTPPacket.Companion|null[0]
final fun fromPacket(io.ktor.utils.io.core/ByteReadPacket): dev.kord.voice.udp/RTPPacket? // dev.kord.voice.udp/RTPPacket.Companion.fromPacket|fromPacket(io.ktor.utils.io.core.ByteReadPacket){}[0]
final fun fromPacket(kotlinx.io/Source): dev.kord.voice.udp/RTPPacket? // dev.kord.voice.udp/RTPPacket.Companion.fromPacket|fromPacket(kotlinx.io.Source){}[0]
}
}

Expand Down Expand Up @@ -1238,7 +1241,7 @@ final suspend inline fun dev.kord.voice/VoiceConnection(dev.kord.gateway/Gateway
// Targets: [apple, linux]
final suspend fun (dev.kord.voice.udp/VoiceUdpSocket).dev.kord.voice.udp/discoverIP(io.ktor.network.sockets/InetSocketAddress, kotlin/Int): io.ktor.network.sockets/InetSocketAddress // dev.kord.voice.udp/discoverIP|[email protected](io.ktor.network.sockets.InetSocketAddress;kotlin.Int){}[0]

// Targets: [apple, linux]
// Targets: [apple]
final suspend fun (dev.kord.voice.udp/VoiceUdpSocket).dev.kord.voice.udp/recv(io.ktor.network.sockets/InetSocketAddress): io.ktor.utils.io.core/ByteReadPacket // dev.kord.voice.udp/recv|[email protected](io.ktor.network.sockets.InetSocketAddress){}[0]

// Targets: [js, mingwX64]
Expand All @@ -1262,4 +1265,7 @@ final class dev.kord.voice.udp/SocketAddress { // dev.kord.voice.udp/SocketAddre
final suspend fun (dev.kord.voice.udp/VoiceUdpSocket).dev.kord.voice.udp/discoverIP(dev.kord.voice.udp/SocketAddress, kotlin/Int): dev.kord.voice.udp/SocketAddress // dev.kord.voice.udp/discoverIP|[email protected](dev.kord.voice.udp.SocketAddress;kotlin.Int){}[0]

// Targets: [js, mingwX64]
final suspend fun (dev.kord.voice.udp/VoiceUdpSocket).dev.kord.voice.udp/recv(dev.kord.voice.udp/SocketAddress): io.ktor.utils.io.core/ByteReadPacket // dev.kord.voice.udp/recv|[email protected](dev.kord.voice.udp.SocketAddress){}[0]
final suspend fun (dev.kord.voice.udp/VoiceUdpSocket).dev.kord.voice.udp/recv(dev.kord.voice.udp/SocketAddress): kotlinx.io/Source // dev.kord.voice.udp/recv|[email protected](dev.kord.voice.udp.SocketAddress){}[0]

// Targets: [linux]
final suspend fun (dev.kord.voice.udp/VoiceUdpSocket).dev.kord.voice.udp/recv(io.ktor.network.sockets/InetSocketAddress): kotlinx.io/Source // dev.kord.voice.udp/recv|[email protected](io.ktor.network.sockets.InetSocketAddress){}[0]
7 changes: 5 additions & 2 deletions voice/src/commonMain/kotlin/udp/RTPPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import dev.kord.voice.io.MutableByteArrayCursor
import dev.kord.voice.io.mutableCursor
import dev.kord.voice.io.view
import io.ktor.utils.io.core.*
import kotlinx.io.Source
import kotlinx.io.readByteArray
import kotlinx.io.readUInt
import kotlin.experimental.and

internal const val RTP_HEADER_LENGTH = 12
Expand Down Expand Up @@ -38,7 +41,7 @@ public data class RTPPacket(
public companion object {
internal const val VERSION = 2

public fun fromPacket(packet: ByteReadPacket): RTPPacket? = with(packet) base@{
public fun fromPacket(packet: Source): RTPPacket? = with(packet) base@{
if (remaining <= 13) return@base null

/*
Expand Down Expand Up @@ -80,7 +83,7 @@ public data class RTPPacket(
if (remaining <= csrcCount * 4 + 1) return@base null
val csrcIdentifiers = UIntArray(csrcCount.toInt()) { readUInt() }

val payload = readBytes().view()
val payload = readByteArray().view()

val paddingBytes = if (hasPadding) { payload[payload.viewSize - 1] } else 0

Expand Down
7 changes: 4 additions & 3 deletions voice/src/commonMain/kotlin/udp/VoiceUdpSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.kord.common.annotation.KordVoice
import dev.kord.voice.io.ByteArrayView
import io.ktor.utils.io.core.*
import kotlinx.coroutines.flow.*
import kotlinx.io.Source

@KordVoice
public expect class SocketAddress(hostname: String, port: Int) {
Expand All @@ -21,15 +22,15 @@ public expect val GlobalVoiceUdpSocket: VoiceUdpSocket

@KordVoice
public interface VoiceUdpSocket {
public fun all(address: SocketAddress): Flow<ByteReadPacket>
public fun all(address: SocketAddress): Flow<Source>

public suspend fun send(address: SocketAddress, packet: ByteArrayView): Unit

public suspend fun stop()

public companion object {
private object None : VoiceUdpSocket {
override fun all(address: SocketAddress): Flow<ByteReadPacket> = emptyFlow()
override fun all(address: SocketAddress): Flow<Source> = emptyFlow()

override suspend fun send(address: SocketAddress, packet: ByteArrayView) {}

Expand All @@ -40,4 +41,4 @@ public interface VoiceUdpSocket {
}
}

public suspend fun VoiceUdpSocket.recv(address: SocketAddress): ByteReadPacket = all(address).first()
public suspend fun VoiceUdpSocket.recv(address: SocketAddress): Source = all(address).first()
7 changes: 4 additions & 3 deletions voice/src/commonMain/kotlin/udp/ipDiscovery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dev.kord.voice.io.mutableCursor
import dev.kord.voice.io.view
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.utils.io.core.*
import kotlinx.io.readByteArray
import kotlinx.io.readUShort

private val ipDiscoveryLogger = KotlinLogging.logger { }

Expand All @@ -15,7 +17,6 @@ private const val DISCOVERY_MESSAGE_SIZE = DISCOVERY_HEADER_SIZE + DISCOVERY_DAT
private const val REQUEST: Short = 0x01
private const val RESPONSE: Short = 0x02

@OptIn(ExperimentalUnsignedTypes::class)
public suspend fun VoiceUdpSocket.discoverIP(address: SocketAddress, ssrc: Int): SocketAddress {
ipDiscoveryLogger.trace { "discovering ip" }

Expand All @@ -31,9 +32,9 @@ public suspend fun VoiceUdpSocket.discoverIP(address: SocketAddress, ssrc: Int):
return with(recv(address)) {
require(readShort() == RESPONSE) { "did not receive a response." }
require(readShort() == MESSAGE_LENGTH) { "expected $MESSAGE_LENGTH bytes of data."}
discardExact(4) // ssrc
skip(4) // ssrc

val ip = String(readBytes(64)).trimEnd(0.toChar())
val ip = readByteArray(64).decodeToString().trimEnd(0.toChar())
val port = readUShort().toInt()

SocketAddress(ip, port)
Expand Down
3 changes: 2 additions & 1 deletion voice/src/jsMain/kotlin/udp/VoiceUdpSocket.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.io.Source
import node.dgram.SocketEvent
import node.dgram.SocketType
import node.dgram.createSocket
Expand All @@ -33,7 +34,7 @@ public actual val GlobalVoiceUdpSocket: VoiceUdpSocket = object : VoiceUdpSocket
}
}

override fun all(address: SocketAddress): Flow<ByteReadPacket> = incoming
override fun all(address: SocketAddress): Flow<Source> = incoming
.filter { it.first == address }
.map { ByteReadPacket(it.second) }

Expand Down
3 changes: 2 additions & 1 deletion voice/src/ktorMain/kotlin/udp/VoiceUdpSocket.ktor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.ktor.network.sockets.Datagram
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.io.Source
import io.ktor.network.sockets.Datagram as KtorDatagram

@KordVoice
Expand All @@ -29,7 +30,7 @@ public actual val GlobalVoiceUdpSocket: VoiceUdpSocket = object : VoiceUdpSocket
.launchIn(socketScope)
}

override fun all(address: SocketAddress): Flow<ByteReadPacket> {
override fun all(address: SocketAddress): Flow<Source> {
return incoming
.filter { it.address == address }
.map { it.packet }
Expand Down
4 changes: 2 additions & 2 deletions voice/src/mingwMain/kotlin/udp/VoiceUdpSocket.mingw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package dev.kord.voice.udp

import dev.kord.common.annotation.KordVoice
import dev.kord.voice.io.ByteArrayView
import io.ktor.utils.io.core.*
import kotlinx.coroutines.flow.Flow
import kotlinx.io.Source

@KordVoice
public actual val GlobalVoiceUdpSocket: VoiceUdpSocket = object : VoiceUdpSocket {
override fun all(address: SocketAddress): Flow<ByteReadPacket> = unsupported()
override fun all(address: SocketAddress): Flow<Source> = unsupported()

override suspend fun send(address: SocketAddress, packet: ByteArrayView) = unsupported()

Expand Down

0 comments on commit 4ea3199

Please sign in to comment.