mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-20 16:39:22 +08:00
Use single thread for reading packet, fix #557
This commit is contained in:
parent
db588f949f
commit
0294df4700
@ -10,6 +10,7 @@
|
||||
package net.mamoe.mirai.qqandroid.utils
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.io.core.ByteReadPacket
|
||||
import kotlinx.io.core.Closeable
|
||||
@ -20,6 +21,7 @@ import java.io.BufferedInputStream
|
||||
import java.io.BufferedOutputStream
|
||||
import java.net.Socket
|
||||
import java.net.SocketException
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
@ -38,6 +40,7 @@ internal actual class PlatformSocket : Closeable {
|
||||
if (::socket.isInitialized) {
|
||||
socket.close()
|
||||
}
|
||||
thread.shutdownNow()
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
@ -67,15 +70,17 @@ internal actual class PlatformSocket : Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
private val thread = Executors.newSingleThreadExecutor()
|
||||
|
||||
/**
|
||||
* @throws ReadPacketInternalException
|
||||
*/
|
||||
actual suspend fun read(): ByteReadPacket {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
actual suspend fun read(): ByteReadPacket = suspendCancellableCoroutine { cont ->
|
||||
thread.submit {
|
||||
kotlin.runCatching {
|
||||
readChannel.readPacketAtMost(Long.MAX_VALUE)
|
||||
} catch (e: IOException) {
|
||||
throw ReadPacketInternalException(e)
|
||||
}.let {
|
||||
cont.resumeWith(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user