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 649535c4e..05f315e66 100644 --- a/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/impl/netty/NettyNetworkHandler.kt @@ -244,7 +244,7 @@ internal open class NettyNetworkHandler( override suspend fun sendPacketImpl(packet: OutgoingPacket): Boolean { connection.await() // split line number - .writeAndFlush(packet) + .writeAndFlushOrCloseAsync(packet) return true } @@ -276,7 +276,7 @@ internal open class NettyNetworkHandler( } override suspend fun sendPacketImpl(packet: OutgoingPacket): Boolean { - connection.writeAndFlush(packet) + connection.writeAndFlushOrCloseAsync(packet) return true } @@ -357,7 +357,7 @@ internal open class NettyNetworkHandler( } override suspend fun sendPacketImpl(packet: OutgoingPacket): Boolean { - connection.writeAndFlush(packet) + connection.writeAndFlushOrCloseAsync(packet) return true } diff --git a/mirai-core/src/commonMain/kotlin/network/impl/netty/nettyUtils.kt b/mirai-core/src/commonMain/kotlin/network/impl/netty/nettyUtils.kt index 9d3168292..64833a387 100644 --- a/mirai-core/src/commonMain/kotlin/network/impl/netty/nettyUtils.kt +++ b/mirai-core/src/commonMain/kotlin/network/impl/netty/nettyUtils.kt @@ -12,6 +12,8 @@ package net.mamoe.mirai.internal.network.impl.netty import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufInputStream import io.netty.channel.ChannelFuture +import io.netty.channel.ChannelFutureListener +import io.netty.channel.ChannelOutboundInvoker import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.Job @@ -57,6 +59,12 @@ internal fun MiraiLogger.asCoroutineExceptionHandler( } } +internal fun ChannelOutboundInvoker.writeAndFlushOrCloseAsync(msg: Any?): ChannelFuture? { + return writeAndFlush(msg) + .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE) + .addListener(ChannelFutureListener.CLOSE_ON_FAILURE) +} + internal suspend inline fun joinCompleted(job: Job) { if (job.isCompleted) job.join()