Skip to content

Commit

Permalink
Correctly close zlib stream when gateway disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Jul 25, 2023
1 parent e643db9 commit b02c222
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gateway/src/commonMain/kotlin/DefaultGateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public class DefaultGateway(private val data: DefaultGatewayData) : Gateway {
}

private suspend fun handleClose() {
inflater.close()

val reason = withTimeoutOrNull(1500) {
socket.closeReason.await()
} ?: return
Expand Down
3 changes: 2 additions & 1 deletion gateway/src/commonMain/kotlin/Inflater.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.kord.gateway

import io.ktor.utils.io.core.*
import io.ktor.websocket.*

internal expect class Inflater() {
internal expect class Inflater() : Closeable {
fun Frame.inflateData(): String
}
5 changes: 4 additions & 1 deletion gateway/src/jsMain/kotlin/Inflater.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package dev.kord.gateway

import dev.kord.gateway.internal.Inflate
import io.ktor.utils.io.core.*
import io.ktor.websocket.*
import node.buffer.Buffer
import node.buffer.BufferEncoding

internal actual class Inflater {
internal actual class Inflater : Closeable {
private val inflate = Inflate()

actual fun Frame.inflateData(): String {
val buffer = Buffer.from(data)

return inflate.process(buffer).toString(BufferEncoding.utf8)
}

override fun close() = inflate.close()
}
2 changes: 2 additions & 0 deletions gateway/src/jsMain/kotlin/internal/JsInflater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ import node.buffer.Buffer

internal external class Inflate {
fun process(data: Uint8Array): Buffer

fun close()
}
6 changes: 5 additions & 1 deletion gateway/src/jvmMain/kotlin/Inflater.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.kord.gateway

import io.ktor.utils.io.core.*
import io.ktor.websocket.*
import java.io.ByteArrayOutputStream
import java.util.zip.InflaterOutputStream
import kotlin.io.use

internal actual class Inflater {
internal actual class Inflater : Closeable {
private val delegate = java.util.zip.Inflater()

actual fun Frame.inflateData(): String {
Expand All @@ -15,4 +17,6 @@ internal actual class Inflater {

return outputStream.use { it.toByteArray().decodeToString() }
}

override fun close() = delegate.end()
}

0 comments on commit b02c222

Please sign in to comment.