diff --git a/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt index 0be48b9d748..0fd22c82e7a 100644 --- a/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt @@ -113,12 +113,18 @@ internal open class NettyNetworkHandler( .addLast(RawIncomingPacketCollector(decodePipeline)) } + protected open fun createDummyDecodePipeline() = PacketDecodePipeline(this@NettyNetworkHandler.coroutineContext) + // can be overridden for tests - protected open suspend fun createConnection(decodePipeline: PacketDecodePipeline): NettyChannel { + protected open suspend fun createConnection(): NettyChannel { packetLogger.debug { "Connecting to $address" } val contextResult = CompletableDeferred() val eventLoopGroup = NioEventLoopGroup() + val decodePipeline = PacketDecodePipeline( + this@NettyNetworkHandler.coroutineContext + .plus(eventLoopGroup.asCoroutineDispatcher()) + ) val future = Bootstrap().group(eventLoopGroup) .channel(NioSocketChannel::class.java) @@ -159,8 +165,6 @@ internal open class NettyNetworkHandler( return contextResult.await() } - protected val decodePipeline = PacketDecodePipeline(this@NettyNetworkHandler.coroutineContext) - protected inner class PacketDecodePipeline(parentContext: CoroutineContext) : CoroutineScope by parentContext.childScope() { private val packetCodec: PacketCodec by lazy { context[PacketCodec] } @@ -241,7 +245,7 @@ internal open class NettyNetworkHandler( private val collectiveExceptions: ExceptionCollector, ) : NettyState(State.CONNECTING) { private val connection = async { - createConnection(decodePipeline) + createConnection() } @Suppress("JoinDeclarationAndAssignment") diff --git a/mirai-core/src/commonTest/kotlin/network/framework/AbstractNettyNHTest.kt b/mirai-core/src/commonTest/kotlin/network/framework/AbstractNettyNHTest.kt index ff96b66f664..21ad528d5c6 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/AbstractNettyNHTest.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/AbstractNettyNHTest.kt @@ -33,7 +33,10 @@ internal abstract class TestNettyNH( address: SocketAddress, ) : NettyNetworkHandler(context, address), ITestNetworkHandler { - abstract override suspend fun createConnection(decodePipeline: PacketDecodePipeline): Channel + protected abstract suspend fun createConnection(decodePipeline: PacketDecodePipeline): Channel + final override suspend fun createConnection(): Channel { + return createConnection(createDummyDecodePipeline()) + } override fun setStateClosed(exception: Throwable?): NetworkHandlerSupport.BaseStateImpl? { return setState { StateClosed(exception) }