From 01ff84692948ee914b68d7b42e014952ed2df9c3 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 28 Feb 2020 22:02:34 +0800 Subject: [PATCH] Fix `copyTo` --- .../net/mamoe/mirai/qqandroid/ContactImpl.kt | 21 +++++----- .../qqandroid/network/QQAndroidClient.kt | 2 +- .../network/highway/HighwayHelper.kt | 6 ++- .../qqandroid/network/highway/highway.kt | 2 +- .../kotlin/net.mamoe.mirai/utils/channels.kt | 40 +++++++++---------- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt index 1792cdf7f..e082ddedb 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt @@ -118,16 +118,17 @@ internal class QQImpl( } is LongConn.OffPicUp.Response.RequireUpload -> { Http.postImage("0x6ff0070", bot.uin, null, imageInput = image.input, inputSize = image.inputSize, uKeyHex = response.uKey.toUHexString("")) -// HighwayHelper.uploadImage( -// client = bot.client, -// serverIp = response.serverIp[0].toIpV4AddressString(), -// serverPort = response.serverPort[0], -// imageInput = image.input, -// inputSize = image.inputSize.toInt(), -// md5 = image.md5, -// uKey = response.uKey, -// commandId = 1 -// ) + //HighwayHelper.uploadImage( + // client = bot.client, + // serverIp = response.serverIp[0].toIpV4AddressString(), + // serverPort = response.serverPort[0], + // imageInput = image.input, + // inputSize = image.inputSize.toInt(), + // fileMd5 = image.md5, + // uKey = response.uKey, + // commandId = 1 + //) + // 为什么不能 ?? return NotOnlineImageFromFile( filepath = response.resourceId, diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt index e3f6c3f5a..91239ab7b 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt @@ -112,7 +112,7 @@ internal open class QQAndroidClient( private val highwayDataTransSequenceIdForGroup: AtomicInt = atomic(87017) internal fun nextHighwayDataTransSequenceIdForGroup(): Int = highwayDataTransSequenceIdForGroup.getAndAdd(2) - private val highwayDataTransSequenceIdForFriend: AtomicInt = atomic(40717) + private val highwayDataTransSequenceIdForFriend: AtomicInt = atomic(43973) internal fun nextHighwayDataTransSequenceIdForFriend(): Int = highwayDataTransSequenceIdForFriend.getAndAdd(2) val appClientVersion: Int = 0 diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt index a3316f7b1..2c5561eda 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt @@ -72,15 +72,17 @@ internal suspend fun HttpClient.postImage( when (imageInput) { is Input -> { var size: Int - while (imageInput.readAvailable(buffer).also { size = it } != 0) { + while (imageInput.readAvailable(buffer).also { size = it } > 0) { channel.writeFully(buffer, 0, size) + channel.flush() } } is ByteReadChannel -> imageInput.copyAndClose(channel) is InputStream -> { var size: Int - while (imageInput.read(buffer).also { size = it } != 0) { + while (imageInput.read(buffer).also { size = it } > 0) { channel.writeFully(buffer, 0, size) + channel.flush() } } else -> error("unsupported imageInput: ${imageInput::class.simpleName}") diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/highway.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/highway.kt index 9941bda27..2401bcb24 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/highway.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/highway.kt @@ -69,7 +69,7 @@ internal fun createImageDataPacketSequence( // RequestDataTrans localeId = localId ), msgSeghead = CSDataHighwayHead.SegHead( - cacheAddr = 812157193, + // cacheAddr = 812157193, datalength = chunkedInput.bufferSize, dataoffset = offset, filesize = dataSize.toLong(), diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt index 25a3ebfbd..75373a528 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt @@ -30,11 +30,11 @@ import kotlin.jvm.JvmName */ suspend fun ByteReadChannel.copyTo(dst: OutputStream) { @UseExperimental(MiraiInternalAPI::class) - ByteArrayPool.useInstance { - do { - val size = this.readAvailable(it) - dst.write(it, 0, size) - } while (size != 0) + ByteArrayPool.useInstance { buffer -> + var size: Int + while (this.readAvailable(buffer).also { size = it } > 0) { + dst.write(buffer, 0, size) + } } } @@ -43,11 +43,11 @@ suspend fun ByteReadChannel.copyTo(dst: OutputStream) { */ suspend fun ByteReadChannel.copyTo(dst: Output) { @UseExperimental(MiraiInternalAPI::class) - ByteArrayPool.useInstance { - do { - val size = this.readAvailable(it) - dst.writeFully(it, 0, size) - } while (size != 0) + ByteArrayPool.useInstance { buffer -> + var size: Int + while (this.readAvailable(buffer).also { size = it } > 0) { + dst.writeFully(buffer, 0, size) + } } } @@ -75,11 +75,11 @@ suspend fun ByteReadChannel.copyTo(dst: kotlinx.coroutines.io.ByteWriteChannel) suspend fun ByteReadChannel.copyAndClose(dst: OutputStream) { try { @UseExperimental(MiraiInternalAPI::class) - ByteArrayPool.useInstance { - do { - val size = this.readAvailable(it) - dst.write(it, 0, size) - } while (size != 0) + ByteArrayPool.useInstance { buffer -> + var size: Int + while (this.readAvailable(buffer).also { size = it } > 0) { + dst.write(buffer, 0, size) + } } } finally { dst.close() @@ -92,11 +92,11 @@ suspend fun ByteReadChannel.copyAndClose(dst: OutputStream) { suspend fun ByteReadChannel.copyAndClose(dst: Output) { try { @UseExperimental(MiraiInternalAPI::class) - ByteArrayPool.useInstance { - do { - val size = this.readAvailable(it) - dst.writeFully(it, 0, size) - } while (size != 0) + ByteArrayPool.useInstance { buffer -> + var size: Int + while (this.readAvailable(buffer).also { size = it } > 0) { + dst.writeFully(buffer, 0, size) + } } } finally { dst.close()