Commit bdcff30c authored by Him188's avatar Him188

Fix packet resend

parent c980bf1c
...@@ -342,25 +342,16 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -342,25 +342,16 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
require(timeoutMillis > 0) { "timeoutMillis must > 0" } require(timeoutMillis > 0) { "timeoutMillis must > 0" }
require(retry >= 0) { "retry must >= 0" } require(retry >= 0) { "retry must >= 0" }
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler)
var lastException: Exception? = null var lastException: Exception? = null
repeat(retry + 1) { repeat(retry + 1) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler)
try { try {
return doSendAndReceive(timeoutMillis, handler)
} catch (e: Exception) {
lastException = e
}
}
packetListeners.remove(handler)
throw lastException!!
}
private suspend inline fun <E : Packet> OutgoingPacket.doSendAndReceive(timeoutMillis: Long = 3000, handler: PacketListener): E {
withContext(this@QQAndroidBotNetworkHandler.coroutineContext + CoroutineName("Packet sender")) { withContext(this@QQAndroidBotNetworkHandler.coroutineContext + CoroutineName("Packet sender")) {
channel.send(delegate) channel.send(delegate)
} }
bot.logger.info("Send: ${this.commandName}") bot.logger.info("Send: ${this.commandName}")
try {
return withTimeoutOrNull(timeoutMillis) { return withTimeoutOrNull(timeoutMillis) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
handler.await() as E handler.await() as E
...@@ -368,6 +359,14 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -368,6 +359,14 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} ?: net.mamoe.mirai.qqandroid.utils.inline { } ?: net.mamoe.mirai.qqandroid.utils.inline {
error("timeout when receiving response of $commandName") error("timeout when receiving response of $commandName")
} }
} finally {
packetListeners.remove(handler)
}
} catch (e: Exception) {
lastException = e
}
}
throw lastException!!
} }
@PublishedApi @PublishedApi
......
...@@ -192,8 +192,8 @@ internal class MessageSvc { ...@@ -192,8 +192,8 @@ internal class MessageSvc {
) )
), ),
msgSeq = client.atomicNextMessageSequenceId(), msgSeq = client.atomicNextMessageSequenceId(),
msgRand = Random.nextInt().absoluteValue msgRand = 123,
// syncCookie = client.c2cMessageSync.syncCookie.takeIf { it.isNotEmpty() } ?: "08 92 C2 C4 F1 05 10 92 C2 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 89 84 F9 A2 06 48 DE 8C EA E5 0E 58 D9 BD BB A0 09 60 1D 68 92 C2 C4 F1 05 70 00".hexToBytes(), syncCookie = client.c2cMessageSync.syncCookie?.takeIf { it.isNotEmpty() } ?: byteArrayOf()
// msgVia = 1 // msgVia = 1
) )
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment