Commit 7de04824 authored by Him188's avatar Him188

Fix null packet

parent c32843fb
...@@ -373,7 +373,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -373,7 +373,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} }
// with generic type, less mistakes // with generic type, less mistakes
private suspend inline fun <P : Packet> generifiedParsePacket(input: Input) { private suspend fun <P : Packet?> generifiedParsePacket(input: Input) {
KnownPacketFactories.parseIncomingPacket(bot, input) { packetFactory: PacketFactory<P>, packet: P, commandName: String, sequenceId: Int -> KnownPacketFactories.parseIncomingPacket(bot, input) { packetFactory: PacketFactory<P>, packet: P, commandName: String, sequenceId: Int ->
handlePacket(packetFactory, packet, commandName, sequenceId) handlePacket(packetFactory, packet, commandName, sequenceId)
if (packet is MultiPacket<*>) { if (packet is MultiPacket<*>) {
...@@ -387,7 +387,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -387,7 +387,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
/** /**
* 处理解析完成的包. * 处理解析完成的包.
*/ */
suspend fun <P : Packet> handlePacket(packetFactory: PacketFactory<P>?, packet: P, commandName: String, sequenceId: Int) { suspend fun <P : Packet?> handlePacket(packetFactory: PacketFactory<P>?, packet: P, commandName: String, sequenceId: Int) {
// highest priority: pass to listeners (attached by sendAndExpect). // highest priority: pass to listeners (attached by sendAndExpect).
packetListeners.forEach { listener -> packetListeners.forEach { listener ->
if (listener.filter(commandName, sequenceId) && packetListeners.remove(listener)) { if (listener.filter(commandName, sequenceId) && packetListeners.remove(listener)) {
...@@ -396,7 +396,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -396,7 +396,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} }
// check top-level cancelling // check top-level cancelling
if (PacketReceivedEvent(packet).broadcast().isCancelled) { if (packet != null && PacketReceivedEvent(packet).broadcast().isCancelled) {
return return
} }
...@@ -412,7 +412,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -412,7 +412,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
if (packet is CancellableEvent && packet.isCancelled) return if (packet is CancellableEvent && packet.isCancelled) return
} }
if (bot.logger.isEnabled || logger.isEnabled) { if (packet != null && bot.logger.isEnabled || logger.isEnabled) {
val logMessage = "Received: ${packet.toString().replace("\n", """\n""").replace("\r", "")}" val logMessage = "Received: ${packet.toString().replace("\n", """\n""").replace("\r", "")}"
if (packet is Event) { if (packet is Event) {
...@@ -585,7 +585,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -585,7 +585,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
internal inner class PacketListener( // callback internal inner class PacketListener( // callback
val commandName: String, val commandName: String,
val sequenceId: Int val sequenceId: Int
) : CompletableDeferred<Packet> by CompletableDeferred(supervisor) { ) : CompletableDeferred<Packet?> by CompletableDeferred(supervisor) {
fun filter(commandName: String, sequenceId: Int) = this.commandName == commandName && this.sequenceId == sequenceId fun filter(commandName: String, sequenceId: Int) = this.commandName == commandName && this.sequenceId == sequenceId
} }
......
...@@ -97,10 +97,10 @@ internal abstract class IncomingPacketFactory<TPacket : Packet?>( ...@@ -97,10 +97,10 @@ internal abstract class IncomingPacketFactory<TPacket : Packet?>(
} }
@JvmName("decode0") @JvmName("decode0")
private suspend inline fun <P : Packet> OutgoingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket): P = packet.decode(bot) private suspend inline fun <P : Packet?> OutgoingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket): P = packet.decode(bot)
@JvmName("decode1") @JvmName("decode1")
private suspend inline fun <P : Packet> IncomingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket, sequenceId: Int): P = private suspend inline fun <P : Packet?> IncomingPacketFactory<P>.decode(bot: QQAndroidBot, packet: ByteReadPacket, sequenceId: Int): P =
packet.decode(bot, sequenceId) packet.decode(bot, sequenceId)
internal val DECRYPTER_16_ZERO = ByteArray(16) internal val DECRYPTER_16_ZERO = ByteArray(16)
...@@ -169,7 +169,7 @@ internal object KnownPacketFactories { ...@@ -169,7 +169,7 @@ internal object KnownPacketFactories {
// do not inline. Exceptions thrown will not be reported correctly // do not inline. Exceptions thrown will not be reported correctly
@UseExperimental(MiraiInternalAPI::class) @UseExperimental(MiraiInternalAPI::class)
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
suspend fun <T : Packet> parseIncomingPacket(bot: QQAndroidBot, rawInput: Input, consumer: PacketConsumer<T>) = with(rawInput) { suspend fun <T : Packet?> parseIncomingPacket(bot: QQAndroidBot, rawInput: Input, consumer: PacketConsumer<T>) = with(rawInput) {
// login // login
val flag1 = readInt() val flag1 = readInt()
...@@ -229,7 +229,7 @@ internal object KnownPacketFactories { ...@@ -229,7 +229,7 @@ internal object KnownPacketFactories {
} }
@UseExperimental(MiraiInternalAPI::class) @UseExperimental(MiraiInternalAPI::class)
internal suspend fun <T : Packet> handleIncomingPacket(it: IncomingPacket<T>, bot: QQAndroidBot, flag2: Int, consumer: PacketConsumer<T>) { internal suspend fun <T : Packet?> handleIncomingPacket(it: IncomingPacket<T>, bot: QQAndroidBot, flag2: Int, consumer: PacketConsumer<T>) {
if (it.packetFactory == null) { if (it.packetFactory == null) {
bot.network.logger.debug("Received commandName: ${it.commandName}") bot.network.logger.debug("Received commandName: ${it.commandName}")
PacketLogger.warning { "找不到 PacketFactory" } PacketLogger.warning { "找不到 PacketFactory" }
...@@ -337,7 +337,7 @@ internal object KnownPacketFactories { ...@@ -337,7 +337,7 @@ internal object KnownPacketFactories {
return IncomingPacket(packetFactory, ssoSequenceId, packet, commandName) return IncomingPacket(packetFactory, ssoSequenceId, packet, commandName)
} }
private suspend fun <T : Packet> ByteReadPacket.parseOicqResponse( private suspend fun <T : Packet?> ByteReadPacket.parseOicqResponse(
bot: QQAndroidBot, bot: QQAndroidBot,
packetFactory: OutgoingPacketFactory<T>, packetFactory: OutgoingPacketFactory<T>,
ssoSequenceId: Int, ssoSequenceId: Int,
......
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