Add writeAndFlushOrCloseAsync to handle exception on send properly

This commit is contained in:
Him188 2021-04-29 16:29:13 +08:00
parent d755ebf7ee
commit 060004644e
2 changed files with 11 additions and 3 deletions

View File

@ -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
}

View File

@ -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()