Skip to content

Commit

Permalink
Add stubs for Windows
Browse files Browse the repository at this point in the history
Relates-to: #929
  • Loading branch information
DRSchlaubi committed Apr 12, 2024
1 parent 35227fc commit dc42c76
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
10 changes: 10 additions & 0 deletions voice/src/commonMain/kotlin/VoiceConnectionBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ import kotlinx.coroutines.flow.first
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

internal expect val isSupported: Boolean

@KordVoice
public class VoiceConnectionBuilder(
public var gateway: Gateway,
public var selfId: Snowflake,
public var channelId: Snowflake,
public var guildId: Snowflake
) {
init {
if(!isSupported) {
throw UnsupportedOperationException("""
Voice is currently not supported on Windows, if you're developing on Windows we recommend using
WSL: https://aka.ms/wsl
""".trimIndent())
}
}
/**
* The amount in milliseconds to wait for the events required to create a [VoiceConnection]. Default is 5000, or 5 seconds.
*/
Expand Down
3 changes: 3 additions & 0 deletions voice/src/jsMain/kotlin/VoiceConnectionBuilder.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.kord.voice

internal actual val isSupported: Boolean = true
3 changes: 3 additions & 0 deletions voice/src/ktorMain/kotlin/VoiceConnectionBuilder.ktor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.kord.voice

internal actual val isSupported: Boolean = true
46 changes: 46 additions & 0 deletions voice/src/ktorMain/kotlin/udp/VoiceUdpSocket.ktor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.kord.voice.udp

import dev.kord.common.annotation.KordVoice
import dev.kord.voice.io.ByteArrayView
import io.ktor.network.sockets.*
import io.ktor.network.selector.*
import io.ktor.network.sockets.Datagram
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import io.ktor.network.sockets.Datagram as KtorDatagram

@KordVoice
public actual typealias SocketAddress = InetSocketAddress

@KordVoice
public actual val GlobalVoiceUdpSocket: VoiceUdpSocket = object : VoiceUdpSocket {
private val socketScope =
CoroutineScope(Dispatchers.Default + SupervisorJob() + CoroutineName("kord-voice-global-socket"))

private val socket = aSocket(SelectorManager(socketScope.coroutineContext)).udp().bind()

private val incoming: MutableSharedFlow<Datagram> = MutableSharedFlow()

init {
socket.incoming
.consumeAsFlow()
.onEach { incoming.emit(it) }
.launchIn(socketScope)
}

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

override suspend fun send(address: SocketAddress, packet: ByteArrayView) {
val brp = ByteReadPacket(packet.data, packet.dataStart, packet.viewSize)
socket.send(KtorDatagram(brp, address))
}

override suspend fun stop() {
}
}

3 changes: 3 additions & 0 deletions voice/src/mingwMain/kotlin/VoiceConnectionBuilder.mingw.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.kord.voice

internal actual val isSupported: Boolean = false
18 changes: 18 additions & 0 deletions voice/src/mingwMain/kotlin/udp/VoiceUdpSocket.nonJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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

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

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

override suspend fun stop() = unsupported()
}

private fun unsupported(): Nothing = TODO("Voice is not supported on windows")

9 changes: 9 additions & 0 deletions voice/src/nonKtorMain/kotlin/VoiceUdpSocket.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.kord.voice.udp

import dev.kord.common.annotation.KordVoice

@KordVoice
public actual data class SocketAddress actual constructor(
public actual val hostname: String,
public actual val port: Int,
)

0 comments on commit dc42c76

Please sign in to comment.