From 3964d9976a6ff544cabef0e8712e14090f6b197e Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 6 Feb 2021 17:29:28 +0800 Subject: [PATCH] Fix PC OtherClientOnlineEvent sync missing clientInfo, fix #955 --- .../receive/MessageSvc.RequestPushStatus.kt | 36 +++++++++++++++---- .../network/protocol/packet/login/StatSvc.kt | 2 +- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.RequestPushStatus.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.RequestPushStatus.kt index 812f0c4bf..45ceb89d5 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.RequestPushStatus.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.RequestPushStatus.kt @@ -2,10 +2,13 @@ package net.mamoe.mirai.internal.network.protocol.packet.chat.receive import kotlinx.coroutines.CancellationException import kotlinx.coroutines.cancel +import kotlinx.coroutines.delay import kotlinx.coroutines.sync.withLock import kotlinx.io.core.ByteReadPacket import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.ClientKind +import net.mamoe.mirai.contact.OtherClientInfo +import net.mamoe.mirai.contact.Platform import net.mamoe.mirai.event.events.OtherClientOfflineEvent import net.mamoe.mirai.event.events.OtherClientOnlineEvent import net.mamoe.mirai.internal.QQAndroidBot @@ -24,16 +27,35 @@ internal object MessageSvcRequestPushStatus : IncomingPacketFactory( override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): Packet? { val packet = readUniPacket(RequestPushStatus.serializer()) bot.otherClientsLock.withLock { - val appId = packet.vecInstanceList?.firstOrNull()?.iAppId ?: 1 + val instanceInfo = packet.vecInstanceList?.firstOrNull() + val appId = instanceInfo?.iAppId ?: 1 + val platformId = instanceInfo?.iPlatform?.toInt() return when (packet.status.toInt()) { 1 -> { // online if (bot.otherClients.any { appId == it.appId }) return null - val info = Mirai.getOnlineOtherClientsList(bot).firstOrNull { appId == it.appId } - ?: throw contextualBugReportException( - "SvcRequestPushStatus (OtherClient online)", - packet._miraiContentToString(), - additional = "Failed to find corresponding instanceInfo." - ) + + suspend fun tryFindInQuery(): OtherClientInfo? { + return Mirai.getOnlineOtherClientsList(bot).find { it.appId == appId } + ?: kotlin.run { + delay(2000) // sometimes server sync slow + Mirai.getOnlineOtherClientsList(bot).find { it.appId == appId } + } + } + + val info = + tryFindInQuery() ?: kotlin.run { + bot.network.logger.warning( + contextualBugReportException( + "SvcRequestPushStatus (OtherClient online)", + "packet: \n" + packet._miraiContentToString() + + "\n\nquery: \n" + + Mirai.getOnlineOtherClientsList(bot)._miraiContentToString(), + additional = "Failed to find corresponding instanceInfo." + ) + ) + OtherClientInfo(appId, Platform.WINDOWS, "", "电脑") + } + val client = bot.createOtherClient(info) bot.otherClients.delegate.add(client) OtherClientOnlineEvent( diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt index 3acf0090d..da02eb756 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt @@ -268,7 +268,7 @@ internal class StatSvc { val info = Mirai.getOnlineOtherClientsList(bot).find { it.appId == appId } ?: kotlin.run { - delay(1000) // sometimes server sync slow + delay(2000) // sometimes server sync slow Mirai.getOnlineOtherClientsList(bot).find { it.appId == appId } } ?: throw contextualBugReportException( "SvcReqMSFLoginNotify (OtherClient online)",