1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 21:23:55 +08:00

Use Either for IncomingPacket.result

This commit is contained in:
Him188 2021-06-25 20:05:33 +08:00
parent 11ffb324c9
commit ce4373a322
4 changed files with 24 additions and 26 deletions
mirai-core/src
commonMain/kotlin/network
commonTest/kotlin/network/impl/netty

View File

@ -18,6 +18,8 @@ import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.component.ComponentKey
import net.mamoe.mirai.internal.network.component.ComponentStorage
import net.mamoe.mirai.internal.network.protocol.packet.*
import net.mamoe.mirai.utils.Either.Companion.ifRight
import net.mamoe.mirai.utils.Either.Companion.onRight
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.cast
import kotlin.coroutines.cancellation.CancellationException
@ -68,11 +70,11 @@ internal class EventBroadcasterPacketHandler(
) : PacketHandler {
override suspend fun handlePacket(incomingPacket: IncomingPacket) {
val data = incomingPacket.data ?: return
impl(data)
incomingPacket.result.ifRight(::impl)
}
private fun impl(packet: Packet) {
private fun impl(packet: Packet?) {
if (packet == null) return
if (packet is MultiPacket<*>) {
for (p in packet) {
impl(p)
@ -95,17 +97,18 @@ internal class CallPacketFactoryPacketHandler(
) : PacketHandler {
override suspend fun handlePacket(incomingPacket: IncomingPacket) {
if (incomingPacket.exception != null) return // failure
val factory = KnownPacketFactories.findPacketFactory(incomingPacket.commandName) ?: return
factory.cast<PacketFactory<Packet?>>().run {
when (this) {
is IncomingPacketFactory -> {
val r = bot.handle(incomingPacket.data, incomingPacket.sequenceId)
if (r != null) {
bot.network.sendWithoutExpect(r)
incomingPacket.result.onRight { data ->
val factory = KnownPacketFactories.findPacketFactory(incomingPacket.commandName) ?: return
factory.cast<PacketFactory<Packet?>>().run {
when (this) {
is IncomingPacketFactory -> {
val r = bot.handle(data, incomingPacket.sequenceId)
if (r != null) {
bot.network.sendWithoutExpect(r)
}
}
is OutgoingPacketFactory -> bot.handle(data)
}
is OutgoingPacketFactory -> bot.handle(incomingPacket.data)
}
}
}

View File

@ -23,6 +23,7 @@ import net.mamoe.mirai.internal.network.protocol.packet.IncomingPacket
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.internal.utils.SingleEntrantLock
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.Either.Companion.fold
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
@ -69,12 +70,10 @@ internal abstract class NetworkHandlerSupport(
for (listener in packetListeners) {
if (!listener.isExpected(packet)) continue
if (packetListeners.remove(listener)) {
val e = packet.exception
if (e != null) {
listener.result.completeExceptionally(e)
} else {
listener.result.complete(packet.data)
}
packet.result.fold(
onLeft = { listener.result.completeExceptionally(it) },
onRight = { listener.result.complete(it) }
)
}
}
launch {

View File

@ -60,12 +60,8 @@ internal class IncomingPacket private constructor(
override fun toString(): String {
return result.fold(
onLeft = {
"IncomingPacket(cmd=$commandName, seq=$sequenceId, FAILURE, e=$it)"
},
onRight = {
"IncomingPacket(cmd=$commandName, seq=$sequenceId, SUCCESS, r=$it)"
}
onLeft = { "IncomingPacket(cmd=$commandName, seq=$sequenceId, FAILURE, e=$it)" },
onRight = { "IncomingPacket(cmd=$commandName, seq=$sequenceId, SUCCESS, r=$it)" }
)
}
}

View File

@ -57,7 +57,7 @@ internal class NettySendPacketTest : AbstractNettyNHTest() {
yield() // yields the thread to run `sendAndExpect`
// when we got thread here again, `sendAndExpect` is suspending for response [Packet].
network.collectReceived(IncomingPacket("cmd", 1, object : Packet {}, null))
network.collectReceived(IncomingPacket("cmd", 1, object : Packet {}))
// now `sendAndExpect` should finish (asynchronously).
expectStop.value = true
}
@ -72,7 +72,7 @@ internal class NettySendPacketTest : AbstractNettyNHTest() {
assertTrue { expectStop.value } // ensures `sendAndExpect` does not finish immediately. (We expect one suspension.)
}
expectStop.value = true
network.collectReceived(IncomingPacket("cmd", 1, object : Packet {}, null))
network.collectReceived(IncomingPacket("cmd", 1, object : Packet {}))
yield()
assertTrue(job.isCompleted)
}