diff --git a/Sources/NIO/ByteBuffer.swift b/Sources/NIO/ByteBuffer.swift index bd8dfa197..3373ac8de 100644 --- a/Sources/NIO/ByteBuffer.swift +++ b/Sources/NIO/ByteBuffer.swift @@ -134,34 +134,34 @@ public struct ByteBuffer { // TODO: Equatable, Comparable // Mutable versions for writing to the buffer. body function returns the number of bytes written and writerIndex // will be automatically moved. - public mutating func withMutableWritePointer(body: (UnsafeMutablePointer, Int) -> Int) -> Int { + public mutating func withMutableWritePointer(body: (UnsafeMutablePointer, Int) -> Int?) -> Int? { let bytesWritten = data.withUnsafeMutableBytes({ return body($0 + writerIndex, writableBytes) }) - advanceWriterIndex(bytesWritten) + advanceWriterIndex(bytesWritten ?? 0) return bytesWritten } - public mutating func withMutableWritePointer(body: (UnsafeMutablePointer, Int) throws -> Int) throws -> Int { + public mutating func withMutableWritePointer(body: (UnsafeMutablePointer, Int) throws -> Int?) throws -> Int? { let bytesWritten = try data.withUnsafeMutableBytes({ return try body($0 + writerIndex, writableBytes) }) - advanceWriterIndex(bytesWritten) + advanceWriterIndex(bytesWritten ?? 0) return bytesWritten } - public mutating func withMutableReadPointer(body: (UnsafeMutablePointer, Int) -> Int) -> Int { + public mutating func withMutableReadPointer(body: (UnsafeMutablePointer, Int) -> Int?) -> Int? { let bytesWritten = data.withUnsafeMutableBytes({ return body($0 + readerIndex, readableBytes) }) - advanceReaderIndex(bytesWritten) + advanceReaderIndex(bytesWritten ?? 0) return bytesWritten } - public mutating func withMutableReadPointer(body: (UnsafeMutablePointer, Int) throws -> Int) throws -> Int { + public mutating func withMutableReadPointer(body: (UnsafeMutablePointer, Int) throws -> Int?) throws -> Int? { let bytesWritten = try data.withUnsafeMutableBytes({ return try body($0 + readerIndex, readableBytes) }) - advanceReaderIndex(bytesWritten) + advanceReaderIndex(bytesWritten ?? 0) return bytesWritten } diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index e5e9ab41d..dbd680cb1 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -213,14 +213,16 @@ public class Channel : ChannelOutboundInvoker { var buffer = try config.recvAllocator.buffer(allocator: allocator) - let bytesRead = try buffer.withMutableWritePointer { try self.socket.read(pointer: $0, size: $1) ?? 0 } - - if bytesRead > 0 { - pipeline.fireChannelRead(data: buffer) - } else { - // end-of-file - close0() - return + let res = try buffer.withMutableWritePointer { try self.socket.read(pointer: $0, size: $1) } + + if let bytesRead = res { + if bytesRead > 0 { + pipeline.fireChannelRead(data: buffer) + } else { + // end-of-file + close0() + return + } } } catch let err { pipeline.fireErrorCaught(error: err) @@ -276,9 +278,9 @@ public class Channel : ChannelOutboundInvoker { // If the buffer is still applicable we'll need to add it back into the queue. var (buffer, promise) = pendingWrites.removeFirst() - let written = try buffer.withMutableReadPointer { try self.socket.write(pointer: $0, size: $1) ?? 0 } + let res = try buffer.withMutableReadPointer { try self.socket.write(pointer: $0, size: $1) } - if written > 0 { + if let written = res { outstanding -= UInt64(written) if buffer.readerIndex == buffer.writerIndex { promise.succeed(result: ())