diff --git a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewayCloseReason.kt b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewayCloseReason.kt index 7232126..2e1a826 100644 --- a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewayCloseReason.kt +++ b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewayCloseReason.kt @@ -12,6 +12,7 @@ sealed interface GatewayCloseReason { data class InvalidSession(val resumable: Boolean) : ShouldReconnect(resume = resumable), ClientInitiated // TODO: handle non-resumable cases properly data class ServerClosed(val closeCode: CloseReason) : ShouldReconnect(resume = true) + data object ServerReconnect : ShouldReconnect(resume = true), ClientInitiated data object ClientPaused : KeepDisconnected(), ClientInitiated diff --git a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewaySession.kt b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewaySession.kt index 1955ec2..1ac638b 100644 --- a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewaySession.kt +++ b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/GatewaySession.kt @@ -101,15 +101,17 @@ class GatewaySession private constructor( private suspend fun handlePayload(payload: Payload.Incoming<*>) { logger.d { payload.toString() } when (val event = payload.d) { + is Event.Heartbeat -> handleHeartbeat() + is Event.Reconnect -> close(GatewayCloseReason.ServerReconnect) + is Event.InvalidSession -> close(GatewayCloseReason.InvalidSession(event.resumable)) is Event.Hello -> handleHello(event) + is Event.HeartbeatAck -> { missedHeartbeats -= 1 } + is Event.Ready -> handlers.ready.handle(event) { resumeProps = it onSuccess() } is Event.Resumed -> onSuccess() - is Event.Heartbeat -> handleHeartbeat() - is Event.InvalidSession -> close(GatewayCloseReason.InvalidSession(event.resumable)) - is Event.HeartbeatAck -> { missedHeartbeats -= 1 } } } @@ -160,8 +162,11 @@ class GatewaySession private constructor( "invalid session (resumable: $reason)" is GatewayCloseReason.ClientPaused -> "client requested pause" + is GatewayCloseReason.ServerReconnect -> + "server requested reconnect" null -> "no reason" + } closeReason = reason diff --git a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/Payloads.kt b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/Payloads.kt index f7d83dc..446980b 100644 --- a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/Payloads.kt +++ b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/Payloads.kt @@ -72,6 +72,10 @@ sealed interface Event { val seq: Int, ) : Outgoing + // 7 + @Serializable + data object Reconnect : Incoming + // 9 @JvmInline @Serializable @@ -82,9 +86,8 @@ sealed interface Event { data class Hello(val heartbeatInterval: Int) : Incoming // 11 - @JvmInline @Serializable - value class HeartbeatAck(private val nothing: Nothing?) : Incoming + data object HeartbeatAck : Incoming // 40 @Serializable diff --git a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/SerializingExtensions.kt b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/SerializingExtensions.kt index 12b98cb..f95aaa3 100644 --- a/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/SerializingExtensions.kt +++ b/core/src/commonMain/kotlin/moe/lava/neon/core/api/gateway/SerializingExtensions.kt @@ -1,6 +1,5 @@ package moe.lava.neon.core.api.gateway -import kotlinx.serialization.KSerializer import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.decodeFromJsonElement import moe.lava.neon.core.api.ApiConstants @@ -25,9 +24,10 @@ fun Payload.Unknown.asIncoming() : Payload.WithSequence { else -> this } 1 -> decode() + 7 -> decodeObject(Event.Reconnect) 9 -> decode() 10 -> decode() - 11 -> decode() + 11 -> decodeObject(Event.HeartbeatAck) else -> this } } @@ -40,5 +40,10 @@ private inline fun Payload.Unknown.decode(): Payloa t = t, ) -private inline fun KSerializer.wrap(): KSerializer> = - Payload.Incoming.serializer(this) +private inline fun Payload.Unknown.decodeObject(obj: T): Payload.Incoming = + Payload.Incoming( + op = op, + d = obj, + s = s, + t = t, + )