diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index 8615b5b9b..ef8ceb7ee 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -33,6 +33,9 @@ private extension IOData { } private typealias PendingWrite = (data: IOData, promise: Promise?) +// We need to know the maximum value of a 32-bit int, but in Swift's regular int size. Store it here. +private let maxInt = Int(INT32_MAX) + private func doPendingWriteVectorOperation(pending: MarkedCircularBuffer, count: Int, iovecs: UnsafeMutableBufferPointer, @@ -46,10 +49,19 @@ private func doPendingWriteVectorOperation(pending: MarkedCircularBuffer= buffer.readableBytes else { + break loop + } + toWrite += buffer.readableBytes + buffer.withUnsafeReadableBytesWithStorageManagement { ptr, storageRef in storageRefs[i] = storageRef.retain() iovecs[i] = iovec(iov_base: UnsafeMutableRawPointer(mutating: ptr.baseAddress!), iov_len: ptr.count) diff --git a/Tests/NIOTests/ChannelTests+XCTest.swift b/Tests/NIOTests/ChannelTests+XCTest.swift index 48e4a9906..1947ec8ac 100644 --- a/Tests/NIOTests/ChannelTests+XCTest.swift +++ b/Tests/NIOTests/ChannelTests+XCTest.swift @@ -28,6 +28,7 @@ extension ChannelTests { return [ ("testBasicLifecycle", testBasicLifecycle), ("testManyManyWrites", testManyManyWrites), + ("testWritevLotsOfData", testWritevLotsOfData), ] } } diff --git a/Tests/NIOTests/ChannelTests.swift b/Tests/NIOTests/ChannelTests.swift index 62005bdf8..b73de5d9c 100644 --- a/Tests/NIOTests/ChannelTests.swift +++ b/Tests/NIOTests/ChannelTests.swift @@ -128,4 +128,39 @@ public class ChannelTests: XCTestCase { // Start shutting stuff down. try clientChannel.close().wait() } + + func testWritevLotsOfData() throws { + let group = try MultiThreadedEventLoopGroup(numThreads: 1) + defer { + try! group.syncShutdownGracefully() + } + + let serverChannel = try ServerBootstrap(group: group) + .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .bind(to: "127.0.0.1", on: 0).wait() + + let clientChannel = try ClientBootstrap(group: group) + .connect(to: serverChannel.localAddress!).wait() + + let bufferSize = 1024 * 1024 * 2 + var buffer = clientChannel.allocator.buffer(capacity: bufferSize) + for _ in 0..