Ensure Buffer.withMutable*Pointer returns an optional so its possible to signal back that a Socket can not read / write anymore

This commit is contained in:
Norman Maurer committed 2017-05-16 11:38:36 +02:00
1 parent 76a88539f7
commit 51764e1c6c
2 files changed
+20 -18

No files matched your search

+8 -8
View File
@@ -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<UInt8>, Int) -> Int) -> Int {
public mutating func withMutableWritePointer(body: (UnsafeMutablePointer<UInt8>, 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<UInt8>, Int) throws -> Int) throws -> Int {
public mutating func withMutableWritePointer(body: (UnsafeMutablePointer<UInt8>, 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<UInt8>, Int) -> Int) -> Int {
public mutating func withMutableReadPointer(body: (UnsafeMutablePointer<UInt8>, 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<UInt8>, Int) throws -> Int) throws -> Int {
public mutating func withMutableReadPointer(body: (UnsafeMutablePointer<UInt8>, Int) throws -> Int?) throws -> Int? {
let bytesWritten = try data.withUnsafeMutableBytes({ return try body($0 + readerIndex, readableBytes) })
advanceReaderIndex(bytesWritten)
advanceReaderIndex(bytesWritten ?? 0)
return bytesWritten
}
+12 -10
View File
@@ -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: ())