From 7b5fda5629615e77e1cf58a10cf1e4b7a54437ab Mon Sep 17 00:00:00 2001 From: consuelita Date: Wed, 7 Feb 2024 18:38:16 -0600 Subject: [PATCH] Fixed Async Read Length Bug --- .../Async/AsyncDarkstarClientConnection.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/ShadowSwift/Async/AsyncDarkstarClientConnection.swift b/Sources/ShadowSwift/Async/AsyncDarkstarClientConnection.swift index fc61ff3..6eb954b 100644 --- a/Sources/ShadowSwift/Async/AsyncDarkstarClientConnection.swift +++ b/Sources/ShadowSwift/Async/AsyncDarkstarClientConnection.swift @@ -29,6 +29,7 @@ import Logging import Chord import Datable import Net +import Straw import Transmission import TransmissionAsync import Transport @@ -119,6 +120,7 @@ public class DarkstarReadable: Readable { let network: AsyncConnection let cipher: AsyncDarkstarCipher + let straw = UnsafeStraw() public init(_ network: AsyncConnection, _ cipher: AsyncDarkstarCipher) { @@ -134,11 +136,6 @@ public class DarkstarReadable: Readable } public func read() async throws -> Data - { - return try await self.read(1024) - } - - public func read(_ size: Int) async throws -> Data { // Get our encrypted length first let encryptedLengthSize = Cipher.lengthSize + Cipher.tagSize @@ -172,6 +169,18 @@ public class DarkstarReadable: Readable return decrypted } + + public func read(_ size: Int) async throws -> Data + { + while straw.count < size + { + let received = try await self.read() + + straw.write(received) + } + + return try straw.read(size: size) + } } public class DarkstarWritable: Writable