Try to fix network when sending lots of packets

This commit is contained in:
Him188 2021-08-27 15:40:53 +08:00 committed by Karlatemp
parent 66c209381d
commit 9590abd702
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8

View File

@ -18,8 +18,6 @@ import io.netty.channel.socket.nio.NioSocketChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder
import io.netty.handler.codec.MessageToByteEncoder
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.trySendBlocking
import net.mamoe.mirai.internal.network.components.*
import net.mamoe.mirai.internal.network.handler.NetworkHandler.State
import net.mamoe.mirai.internal.network.handler.NetworkHandlerContext
@ -165,31 +163,17 @@ internal open class NettyNetworkHandler(
protected inner class PacketDecodePipeline(parentContext: CoroutineContext) :
CoroutineScope by parentContext.childScope() {
private val channel: Channel<RawIncomingPacket> = Channel(Channel.BUFFERED)
private val packetCodec: PacketCodec by lazy { context[PacketCodec] }
init {
coroutineContext.job.invokeOnCompletion {
channel.close() // normally close
fun send(raw: RawIncomingPacket) {
launch {
packetLogger.debug { "Packet Handling Processor: receive packet ${raw.commandName}" }
val result = packetCodec.processBody(context.bot, raw)
if (result == null) {
collectUnknownPacket(raw)
} else collectReceived(result)
}
}
init {
repeat(4) { processorId ->
launch(CoroutineName("PacketDecodePipeline processor #$processorId")) {
while (isActive) {
val raw = channel.receiveCatching().getOrNull() ?: return@launch
packetLogger.debug { "Packet Handling Processor #$processorId: receive packet ${raw.commandName}" }
val result = packetCodec.processBody(context.bot, raw)
if (result == null) {
collectUnknownPacket(raw)
} else collectReceived(result)
}
}
}
}
fun send(raw: RawIncomingPacket) = channel.trySendBlocking(raw)
}