From 12c38b94fe26cd1cca72330712e36a651f265977 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 21 May 2020 10:38:46 +0800 Subject: [PATCH] Fix IllegalStateException when losing packet --- .../network/QQAndroidBotNetworkHandler.kt | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt index 81b3cc36a..af9040595 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt @@ -531,34 +531,39 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo val cache = cachedPacket.value if (cache == null) { - // 没有缓存 - var length: Int = rawInput.readInt() - 4 - if (rawInput.remaining == length.toLong()) { - // 捷径: 当包长度正好, 直接传递剩余数据. - cachedPacketTimeoutJob?.cancel() - parsePacketAsync(rawInput) - return - } - // 循环所有完整的包 - while (rawInput.remaining >= length) { - parsePacketAsync(rawInput.readPacketExact(length)) + kotlin.runCatching { + // 没有缓存 + var length: Int = rawInput.readInt() - 4 + if (rawInput.remaining == length.toLong()) { + // 捷径: 当包长度正好, 直接传递剩余数据. + cachedPacketTimeoutJob?.cancel() + parsePacketAsync(rawInput) + return + } + // 循环所有完整的包 + while (rawInput.remaining >= length) { + parsePacketAsync(rawInput.readPacketExact(length)) - if (rawInput.remaining == 0L) { + if (rawInput.remaining == 0L) { + cachedPacket.value = null // 表示包长度正好 + cachedPacketTimeoutJob?.cancel() + return + } + length = rawInput.readInt() - 4 + } + + if (rawInput.remaining != 0L) { + // 剩余的包长度不够, 缓存后接收下一个包 + expectingRemainingLength = length - rawInput.remaining + cachedPacket.value = rawInput + } else { cachedPacket.value = null // 表示包长度正好 cachedPacketTimeoutJob?.cancel() return } - length = rawInput.readInt() - 4 - } - - if (rawInput.remaining != 0L) { - // 剩余的包长度不够, 缓存后接收下一个包 - expectingRemainingLength = length - rawInput.remaining - cachedPacket.value = rawInput - } else { - cachedPacket.value = null // 表示包长度正好 + }.getOrElse { + cachedPacket.value = null cachedPacketTimeoutJob?.cancel() - return } } else { // 有缓存