Skip to content

Commit

Permalink
Minimize deprecation code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Nov 2, 2024
1 parent eed1131 commit b2554eb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 118 deletions.
34 changes: 15 additions & 19 deletions voice/src/main/kotlin/VoiceConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ public class VoiceConnection internal constructor(
connectionDetachDuration: Duration,
internal val strategy: @Suppress("DEPRECATION") NonceStrategy?,
) {
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. A 'VoiceConnection' instance " +
"can be created without a 'nonceStrategy' in which case this property throws an " +
"'UnsupportedOperationException'. $XSalsa20_PROPERTY_DEPRECATION",
level = DeprecationLevel.WARNING,
)
public val nonceStrategy: @Suppress("DEPRECATION") NonceStrategy
get() = strategy
?: throw UnsupportedOperationException("This VoiceConnection instance was created without a nonceStrategy.")

@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. Construct a 'VoiceConnection' " +
"instance without a 'nonceStrategy' instead. $XSalsa20_CONSTRUCTOR_DEPRECATION",
Expand All @@ -83,23 +72,30 @@ public class VoiceConnection internal constructor(
frameInterceptor: FrameInterceptor, frameSender: AudioFrameSender,
nonceStrategy: @Suppress("DEPRECATION") NonceStrategy, connectionDetachDuration: Duration,
) : this(
data = data, gateway = gateway, voiceGateway = voiceGateway, socket = socket,
voiceGatewayConfiguration = voiceGatewayConfiguration, streams = streams, audioProvider = audioProvider,
frameInterceptor = frameInterceptor, frameSender = frameSender,
connectionDetachDuration = connectionDetachDuration, strategy = nonceStrategy,
data, gateway, voiceGateway, socket, voiceGatewayConfiguration, streams, audioProvider, frameInterceptor,
frameSender, connectionDetachDuration, nonceStrategy,
)

public constructor(
data: VoiceConnectionData, gateway: Gateway, voiceGateway: VoiceGateway, socket: VoiceUdpSocket,
voiceGatewayConfiguration: VoiceGatewayConfiguration, streams: Streams, audioProvider: AudioProvider,
frameInterceptor: FrameInterceptor, frameSender: AudioFrameSender, connectionDetachDuration: Duration,
) : this(
data = data, gateway = gateway, voiceGateway = voiceGateway, socket = socket,
voiceGatewayConfiguration = voiceGatewayConfiguration, streams = streams, audioProvider = audioProvider,
frameInterceptor = frameInterceptor, frameSender = frameSender,
connectionDetachDuration = connectionDetachDuration, strategy = null,
data, gateway, voiceGateway, socket, voiceGatewayConfiguration, streams, audioProvider, frameInterceptor,
frameSender, connectionDetachDuration, strategy = null,
)

@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. A 'VoiceConnection' instance " +
"can be created without a 'nonceStrategy' in which case this property throws an " +
"'UnsupportedOperationException'. $XSalsa20_PROPERTY_DEPRECATION",
level = DeprecationLevel.WARNING,
)
public val nonceStrategy: @Suppress("DEPRECATION") NonceStrategy
get() = strategy
?: throw UnsupportedOperationException("This VoiceConnection instance was created without a nonceStrategy.")

public val scope: CoroutineScope =
CoroutineScope(SupervisorJob() + CoroutineName("kord-voice-connection[${data.guildId.value}]"))

Expand Down
58 changes: 25 additions & 33 deletions voice/src/main/kotlin/streams/DefaultStreams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,10 @@ private val defaultStreamsLogger = KotlinLogging.logger { }

@KordVoice
public class DefaultStreams internal constructor(
gateway: VoiceGateway,
strategy: @Suppress("DEPRECATION") NonceStrategy?,
udpSocket: VoiceUdpSocket,
private val voiceGateway: VoiceGateway,
private val nonceStrategy: @Suppress("DEPRECATION") NonceStrategy?,
private val udp: VoiceUdpSocket,
) : Streams {
private val voiceGateway = gateway
private val udp = udpSocket
private val nonceStrategy = strategy

public constructor(voiceGateway: VoiceGateway, udp: VoiceUdpSocket) :
this(gateway = voiceGateway, strategy = null, udpSocket = udp)

@Deprecated(
"The 'nonceStrategy' parameter is only used for XSalsa20 Poly1305 encryption. Construct a 'DefaultStreams' " +
"instance without a 'NonceStrategy' instead. $XSalsa20_CONSTRUCTOR_DEPRECATION",
Expand All @@ -50,15 +43,14 @@ public class DefaultStreams internal constructor(
)
public constructor(
voiceGateway: VoiceGateway, udp: VoiceUdpSocket, nonceStrategy: @Suppress("DEPRECATION") NonceStrategy,
) : this(gateway = voiceGateway, strategy = nonceStrategy, udpSocket = udp)
) : this(voiceGateway, nonceStrategy, udp)

public constructor(voiceGateway: VoiceGateway, udp: VoiceUdpSocket) : this(voiceGateway, nonceStrategy = null, udp)

internal fun CoroutineScope.listenForIncoming(
udp: VoiceUdpSocket,
key: ByteArray,
server: SocketAddress,
key: ByteArray, server: SocketAddress,
@Suppress("LocalVariableName") _incomingAudioPackets: MutableSharedFlow<RTPPacket>,
nonceStrategy: @Suppress("DEPRECATION") NonceStrategy,
emitVoicePacket: suspend (RTPPacket) -> Unit,
nonceStrategy: @Suppress("DEPRECATION") NonceStrategy, emitVoicePacket: suspend (RTPPacket) -> Unit,
) {
udp.incoming
.filter { it.address == server }
Expand Down Expand Up @@ -92,22 +84,6 @@ public class DefaultStreams internal constructor(
}.launchIn(this)
}

override suspend fun listen(key: ByteArray, server: SocketAddress, encryptionMode: EncryptionMode) {
val decryptionDelegate = @Suppress("DEPRECATION") when (encryptionMode) {
EncryptionMode.AeadAes256GcmRtpSize -> NewDecryptionDelegate(AeadAes256GcmRtpSizeVoicePacketDecryptor(key))
EncryptionMode.AeadXChaCha20Poly1305RtpSize ->
NewDecryptionDelegate(AeadXChaCha20Poly1305RtpSizeVoicePacketDecryptor(key))
EncryptionMode.XSalsa20Poly1305 ->
LegacyDecryptionDelegate(key, this, nonceStrategy as? NormalNonceStrategy ?: NormalNonceStrategy())
EncryptionMode.XSalsa20Poly1305Lite ->
LegacyDecryptionDelegate(key, this, nonceStrategy as? LiteNonceStrategy ?: LiteNonceStrategy())
EncryptionMode.XSalsa20Poly1305Suffix ->
LegacyDecryptionDelegate(key, this, nonceStrategy as? SuffixNonceStrategy ?: SuffixNonceStrategy())
is EncryptionMode.Unknown -> throw UnsupportedOperationException("Unknown encryption mode $encryptionMode")
}
listen(decryptionDelegate, server)
}

@Deprecated(
"This functions always uses XSalsa20 Poly1305 encryption. Pass an explicit 'EncryptionMode' instead. A " +
"'DefaultStreams' instance can be created without a 'NonceStrategy' in which case this function throws " +
Expand All @@ -124,6 +100,22 @@ public class DefaultStreams internal constructor(
listen(LegacyDecryptionDelegate(key, this, strategy), server)
}

override suspend fun listen(key: ByteArray, server: SocketAddress, encryptionMode: EncryptionMode) {
val decryptionDelegate = @Suppress("DEPRECATION") when (encryptionMode) {
EncryptionMode.AeadAes256GcmRtpSize -> NewDecryptionDelegate(AeadAes256GcmRtpSizeVoicePacketDecryptor(key))
EncryptionMode.AeadXChaCha20Poly1305RtpSize ->
NewDecryptionDelegate(AeadXChaCha20Poly1305RtpSizeVoicePacketDecryptor(key))
EncryptionMode.XSalsa20Poly1305 ->
LegacyDecryptionDelegate(key, this, nonceStrategy as? NormalNonceStrategy ?: NormalNonceStrategy())
EncryptionMode.XSalsa20Poly1305Lite ->
LegacyDecryptionDelegate(key, this, nonceStrategy as? LiteNonceStrategy ?: LiteNonceStrategy())
EncryptionMode.XSalsa20Poly1305Suffix ->
LegacyDecryptionDelegate(key, this, nonceStrategy as? SuffixNonceStrategy ?: SuffixNonceStrategy())
is EncryptionMode.Unknown -> throw UnsupportedOperationException("Unknown encryption mode $encryptionMode")
}
listen(decryptionDelegate, server)
}

private suspend fun listen(delegate: DecryptionDelegate, server: SocketAddress): Unit = coroutineScope {
delegate.listenForIncoming(scope = this, udp, server, _incomingAudioPackets, _incomingVoicePackets)
listenForUserFrames()
Expand Down Expand Up @@ -223,7 +215,7 @@ private class LegacyDecryptionDelegate(
audioPackets: MutableSharedFlow<RTPPacket>,
voicePackets: MutableSharedFlow<DecryptedVoicePacket>,
) = with(streams) {
scope.listenForIncoming(udp, key, server, audioPackets, nonceStrategy) { rtpPacket ->
scope.listenForIncoming(key, server, audioPackets, nonceStrategy) { rtpPacket ->
if (voicePackets.subscriptionCount.value > 0) {
voicePackets.emit(
@OptIn(ExperimentalUnsignedTypes::class)
Expand Down
10 changes: 4 additions & 6 deletions voice/src/main/kotlin/udp/AudioFrameSender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public data class AudioFrameSenderConfiguration(
public constructor(
server: SocketAddress, ssrc: UInt, key: ByteArray, interceptorConfiguration: FrameInterceptorConfiguration,
) : this(
server = server, ssrc = ssrc, key = key, interceptorConfiguration = interceptorConfiguration,
encryptionMode = EncryptionMode.from("AudioFrameSenderConfiguration.encryptionMode placeholder"),
server, ssrc, key, interceptorConfiguration,
EncryptionMode.from("AudioFrameSenderConfiguration.encryptionMode placeholder"),
)

@Deprecated(
Expand All @@ -35,10 +35,8 @@ public data class AudioFrameSenderConfiguration(
public fun copy(
server: SocketAddress = this.server, ssrc: UInt = this.ssrc, key: ByteArray = this.key,
interceptorConfiguration: FrameInterceptorConfiguration = this.interceptorConfiguration,
): AudioFrameSenderConfiguration = AudioFrameSenderConfiguration(
server = server, ssrc = ssrc, key = key, interceptorConfiguration = interceptorConfiguration,
encryptionMode = this.encryptionMode,
)
): AudioFrameSenderConfiguration =
AudioFrameSenderConfiguration(server, ssrc, key, interceptorConfiguration, encryptionMode)
}

@KordVoice
Expand Down
27 changes: 12 additions & 15 deletions voice/src/main/kotlin/udp/AudioPacketProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public abstract class AudioPacketProvider internal constructor(
private val strategy: @Suppress("DEPRECATION") NonceStrategy?,
public val key: ByteArray,
) {
@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. Construct an " +
"'AudioPacketProvider' instance without a 'nonceStrategy' instead. $XSalsa20_CONSTRUCTOR_DEPRECATION",
ReplaceWith("AudioPacketProvider(key)", imports = ["dev.kord.voice.udp.AudioPacketProvider"]),
DeprecationLevel.WARNING,
)
public constructor(key: ByteArray, nonceStrategy: @Suppress("DEPRECATION") NonceStrategy) : this(nonceStrategy, key)
public constructor(key: ByteArray) : this(strategy = null, key)

@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. An 'AudioPacketProvider' " +
"instance can be created without a 'nonceStrategy' in which case this property throws an " +
Expand All @@ -29,25 +38,14 @@ public abstract class AudioPacketProvider internal constructor(
"This AudioPacketProvider instance was created without a nonceStrategy."
)

@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. Construct an " +
"'AudioPacketProvider' instance without a 'nonceStrategy' instead. $XSalsa20_CONSTRUCTOR_DEPRECATION",
ReplaceWith("AudioPacketProvider(key)", imports = ["dev.kord.voice.udp.AudioPacketProvider"]),
DeprecationLevel.WARNING,
)
public constructor(key: ByteArray, nonceStrategy: @Suppress("DEPRECATION") NonceStrategy) :
this(strategy = nonceStrategy, key = key)

public constructor(key: ByteArray) : this(strategy = null, key = key)

public abstract fun provide(sequence: UShort, timestamp: UInt, ssrc: UInt, data: ByteArray): ByteArrayView
}

private class CouldNotEncryptDataException(data: ByteArray) :
RuntimeException("Couldn't encrypt the following data: [${data.joinToString(", ")}]")

public class DefaultAudioPacketProvider internal constructor(
key: ByteArray, encryptionMode: EncryptionMode?, nonceStrategy: @Suppress("DEPRECATION") NonceStrategy?,
key: ByteArray, nonceStrategy: @Suppress("DEPRECATION") NonceStrategy?, encryptionMode: EncryptionMode?,
) : AudioPacketProvider(nonceStrategy, key) {
@Deprecated(
"The 'nonceStrategy' property is only used for XSalsa20 Poly1305 encryption. Construct a " +
Expand All @@ -60,10 +58,9 @@ public class DefaultAudioPacketProvider internal constructor(
DeprecationLevel.WARNING,
)
public constructor(key: ByteArray, nonceStrategy: @Suppress("DEPRECATION") NonceStrategy) :
this(key = key, encryptionMode = null, nonceStrategy = nonceStrategy)
this(key, nonceStrategy, encryptionMode = null)

public constructor(key: ByteArray, encryptionMode: EncryptionMode) :
this(key = key, encryptionMode = encryptionMode, nonceStrategy = null)
public constructor(key: ByteArray, encryptionMode: EncryptionMode) : this(key, nonceStrategy = null, encryptionMode)

private val delegate = if (nonceStrategy != null) {
LegacyProviderDelegate(key, nonceStrategy)
Expand Down
Loading

0 comments on commit b2554eb

Please sign in to comment.