diff --git a/Examples/IORingTCPEcho/IORingTCPEcho.swift b/Examples/IORingTCPEcho/IORingTCPEcho.swift index 3416a32..89a139e 100644 --- a/Examples/IORingTCPEcho/IORingTCPEcho.swift +++ b/Examples/IORingTCPEcho/IORingTCPEcho.swift @@ -59,7 +59,7 @@ public struct IORingTCPEcho { var buffer = [UInt8](repeating: 0, count: 1) more = try await client.read(into: &buffer, count: 1) == 1 if more { - guard try await client.write(buffer, count: 1) == 1 else { + guard try await client.write(buffer, count: 1, awaitingAllWritten: false) == 1 else { break } } diff --git a/Sources/IORingUtils/Socket.swift b/Sources/IORingUtils/Socket.swift index 041c37d..43f2d01 100644 --- a/Sources/IORingUtils/Socket.swift +++ b/Sources/IORingUtils/Socket.swift @@ -191,30 +191,36 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { public func read(into buffer: inout [UInt8], count: Int) async throws -> Int { guard let fileHandle else { throw Errno.badFileDescriptor } - return try await ring.read( - into: &buffer, - count: count, - offset: -1, - from: fileHandle - ) + + return try await ring.read(into: &buffer, count: count, from: fileHandle) } - public func read(count: Int) async throws -> [UInt8] { + public func read(count: Int, awaitingAllRead: Bool) async throws -> [UInt8] { guard let fileHandle else { throw Errno.badFileDescriptor } - return try await ring.read( - count: count, - from: fileHandle - ) + + var buffer = [UInt8]() + + repeat { + buffer += try await ring.read(count: count, from: fileHandle) + } while awaitingAllRead && buffer.count < count + + return buffer } - public func write(_ buffer: [UInt8], count: Int) async throws -> Int { + public func write(_ buffer: [UInt8], count: Int, awaitingAllWritten: Bool) async throws -> Int { guard let fileHandle else { throw Errno.badFileDescriptor } - return try await ring.write( - buffer, - count: count, - offset: -1, - to: fileHandle - ) + + var nwritten = 0 + + repeat { + nwritten += try await ring.write( + Array(buffer[nwritten.. [UInt8] {