Skip to content

Commit

Permalink
Fixed Async Read Length Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
consuelita committed Feb 8, 2024
1 parent 92c30ff commit 7b5fda5
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions Sources/ShadowSwift/Async/AsyncDarkstarClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Logging
import Chord
import Datable
import Net
import Straw
import Transmission
import TransmissionAsync
import Transport
Expand Down Expand Up @@ -119,6 +120,7 @@ public class DarkstarReadable: Readable
{
let network: AsyncConnection
let cipher: AsyncDarkstarCipher
let straw = UnsafeStraw()

public init(_ network: AsyncConnection, _ cipher: AsyncDarkstarCipher)
{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7b5fda5

Please sign in to comment.