main.handlers.StreamsHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kord-voice Show documentation
Show all versions of kord-voice Show documentation
Idiomatic Kotlin Wrapper for The Discord API
package dev.kord.voice.handlers
import dev.kord.voice.gateway.Close
import dev.kord.voice.gateway.Ready
import dev.kord.voice.gateway.SessionDescription
import dev.kord.voice.gateway.VoiceEvent
import dev.kord.voice.gateway.handler.GatewayEventHandler
import dev.kord.voice.streams.Streams
import io.ktor.network.sockets.*
import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
internal class StreamsHandler(
flow: Flow,
private val streams: Streams,
) : GatewayEventHandler(flow, "HandshakeHandler") {
private val server: AtomicRef = atomic(null)
private var streamsJob: Job? by atomic(null)
@OptIn(ExperimentalUnsignedTypes::class)
override suspend fun start() = coroutineScope {
on {
server.value = InetSocketAddress(it.ip, it.port)
}
on {
streamsJob?.cancel()
streamsJob = launch { streams.listen(it.secretKey.toUByteArray().toByteArray(), server.value!!) }
}
on {
streamsJob?.cancel()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy