Commit ce7c9f8c authored by Him188's avatar Him188

Remove getQQOrAdd

parent a113f0ed
...@@ -37,10 +37,6 @@ internal abstract class QQAndroidBotBase constructor( ...@@ -37,10 +37,6 @@ internal abstract class QQAndroidBotBase constructor(
return qqs.delegate.filteringGetOrAdd({ it.id == id }, { QQImpl(this as QQAndroidBot, coroutineContext, id) }) return qqs.delegate.filteringGetOrAdd({ it.id == id }, { QQImpl(this as QQAndroidBot, coroutineContext, id) })
} }
fun getQQOrAdd(id: Long): QQ {
return qqs.delegate.filteringGetOrAdd({ it.id == id }, { QQImpl(this as QQAndroidBot, coroutineContext, id) })
}
override fun QQ(id: Long): QQ { override fun QQ(id: Long): QQ {
return QQImpl(this as QQAndroidBot, coroutineContext, id) return QQImpl(this as QQAndroidBot, coroutineContext, id)
} }
......
...@@ -115,7 +115,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -115,7 +115,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
//val msg = MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect<MessageSvc.PbGetMsg.Response>() //val msg = MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect<MessageSvc.PbGetMsg.Response>()
//println(msg.contentToString()) //println(msg.contentToString())
bot.qqs.delegate.clear()
bot.groups.delegate.clear()
val startTime = currentTimeMillis val startTime = currentTimeMillis
try { try {
...@@ -472,12 +473,12 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -472,12 +473,12 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
/** /**
* 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms) * 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms)
*/ */
suspend fun <E : Packet> OutgoingPacket.sendAndExpect(timeoutMillis: Long = 3000, retry: Int = 1): E { suspend fun <E : Packet> OutgoingPacket.sendAndExpect(timeoutMillis: Long = 3000, retry: Int = 0): E {
require(timeoutMillis > 0) { "timeoutMillis must > 0" } require(timeoutMillis > 0) { "timeoutMillis must > 0" }
require(retry >= 0) { "retry must >= 0" } require(retry >= 0) { "retry must >= 0" }
var lastException: Exception? = null var lastException: Exception? = null
repeat(retry + 1) { if (retry == 0) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId) val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler) packetListeners.addLast(handler)
try { try {
...@@ -492,6 +493,25 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -492,6 +493,25 @@ 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)
}
} else this.delegate.useBytes { data, length ->
repeat(retry + 1) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler)
try {
withContext(this@QQAndroidBotNetworkHandler.coroutineContext + CoroutineName("Packet sender")) {
channel.send(data, 0, length)
}
bot.logger.info("Send: ${this.commandName}")
return withTimeoutOrNull(timeoutMillis) {
@Suppress("UNCHECKED_CAST")
handler.await() as E
// 不要 `withTimeout`. timeout 的异常会不知道去哪了.
} ?: net.mamoe.mirai.qqandroid.utils.inline {
error("timeout when receiving response of $commandName")
}
} catch (e: Exception) { } catch (e: Exception) {
lastException = e lastException = e
} finally { } finally {
...@@ -500,6 +520,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -500,6 +520,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} }
throw lastException!! throw lastException!!
} }
}
@PublishedApi @PublishedApi
internal val packetListeners = LockFreeLinkedList<PacketListener>() internal val packetListeners = LockFreeLinkedList<PacketListener>()
......
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