From 5bee1c7bcd596bbd6a997aa50baa6d60c740b704 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 30 Apr 2017 18:26:44 +0200 Subject: [PATCH] Correctly implement TailHandler and ensure we also forward close operations --- Sources/Channel.swift | 34 ++++++++++++++------------- Sources/ChannelHandlerContext.swift | 25 ++++++++++---------- Sources/ChannelPipeline.swift | 36 +++++++++++++++++++++++++---- 3 files changed, 62 insertions(+), 33 deletions(-) diff --git a/Sources/Channel.swift b/Sources/Channel.swift index 6579f9750..14a706bb7 100644 --- a/Sources/Channel.swift +++ b/Sources/Channel.swift @@ -27,6 +27,7 @@ public class Channel : ChannelOutboundInvoker { self.socket = socket self.selector = selector pipeline = ChannelPipeline() + // TODO: This is most likely not the best datastructure for us. Doubly-Linked-List would be better. pendingWrites = Array() outstanding = 0 flushPending = false @@ -63,6 +64,7 @@ public class Channel : ChannelOutboundInvoker { pendingWrites.append((data, promise)) outstanding += UInt64((data.limit - data.offset)) + // TODO: Configurable or remove completely ? if outstanding >= 64 * 1024 { // Too many outstanding bytes, try flush these now. flush0() @@ -79,7 +81,7 @@ public class Channel : ChannelOutboundInvoker { pipeline.fireChannelWritabilityChanged(writable: false) } catch { // TODO: Log ? - close0(promise: Promise()) + close0() } } } @@ -94,7 +96,7 @@ public class Channel : ChannelOutboundInvoker { try selector.reregister(selectable: socket, interested: InterestedEvent.Read) } catch { // TODO: Log ? - close0(promise: Promise()) + close0() } } } @@ -120,7 +122,9 @@ public class Channel : ChannelOutboundInvoker { pendingWrites.removeFirst() } - close0(promise: Promise()) + if err is IOError { + close0() + } } return true } @@ -143,23 +147,21 @@ public class Channel : ChannelOutboundInvoker { } catch let err { pipeline.fireErrorCaught(error: err) if err is IOError { - close0(promise: Promise()) + close0() } } } - func close0(promise: Promise) { - if socket.open { - defer { - // Ensure this is always called - pipeline.fireChannelInactive() - } - do { - try socket.close() - promise.succeed(result: ()) - } catch let err { - promise.fail(error: err) - } + func close0(promise: Promise = Promise()) { + defer { + // Ensure this is always called + pipeline.fireChannelInactive() + } + do { + try socket.close() + promise.succeed(result: ()) + } catch let err { + promise.fail(error: err) } } diff --git a/Sources/ChannelHandlerContext.swift b/Sources/ChannelHandlerContext.swift index 71eae1fea..b3caea6f5 100644 --- a/Sources/ChannelHandlerContext.swift +++ b/Sources/ChannelHandlerContext.swift @@ -29,7 +29,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func fireChannelActive() { - next?.invokeChannelActive() + next!.invokeChannelActive() } func invokeChannelActive() { @@ -37,7 +37,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func fireChannelInactive() { - next?.invokeChannelInactive() + next!.invokeChannelInactive() } func invokeChannelInactive() { @@ -45,7 +45,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func fireChannelRead(data: Buffer) { - next?.invokeChannelRead(data: data) + next!.invokeChannelRead(data: data) } func invokeChannelRead(data: Buffer) { @@ -53,7 +53,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func fireChannelReadComplete() { - next?.invokeChannelReadComplete() + next!.invokeChannelReadComplete() } func invokeChannelReadComplete() { @@ -61,7 +61,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func fireChannelWritabilityChanged(writable: Bool) { - next?.invokeChannelWritabilityChanged(writable: writable) + next!.invokeChannelWritabilityChanged(writable: writable) } public func invokeChannelWritabilityChanged(writable: Bool) { @@ -69,7 +69,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func fireErrorCaught(error: Error) { - next?.invokeErrorCaught(error: error) + next!.invokeErrorCaught(error: error) } func invokeErrorCaught(error: Error) { @@ -77,7 +77,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func write(data: Buffer, promise: Promise) -> Future { - prev?.invokeWrite(data: data, promise: promise) + prev!.invokeWrite(data: data, promise: promise) return promise.futureResult } @@ -86,15 +86,14 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func writeAndFlush(data: Buffer, promise: Promise) -> Future { - if let p = prev { - p.invokeWrite(data: data, promise: promise) - p.invokeFlush() - } + prev!.invokeWrite(data: data, promise: promise) + prev!.invokeFlush() + return promise.futureResult } public func flush() { - prev?.invokeFlush() + prev!.invokeFlush() } func invokeFlush() { @@ -102,7 +101,7 @@ public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvok } public func close(promise: Promise) -> Future { - prev?.invokeClose(promise: promise) + prev!.invokeClose(promise: promise) return promise.futureResult } diff --git a/Sources/ChannelPipeline.swift b/Sources/ChannelPipeline.swift index c5a7afb61..0bd799090 100644 --- a/Sources/ChannelPipeline.swift +++ b/Sources/ChannelPipeline.swift @@ -34,8 +34,8 @@ public class ChannelPipeline : ChannelInboundInvoker, ChannelOutboundInvoker { ctx.prev = tail!.prev ctx.next = tail - prev?.next = ctx - tail?.prev = ctx + prev!.next = ctx + tail!.prev = ctx ctx.invokeHandlerAdded() } @@ -47,7 +47,7 @@ public class ChannelPipeline : ChannelInboundInvoker, ChannelOutboundInvoker { ctx.prev = head ctx.next = next - next?.prev = ctx + next!.prev = ctx ctx.invokeHandlerAdded() } @@ -108,6 +108,34 @@ class HeadChannelHandler : ChannelHandler { func flush(ctx: ChannelHandlerContext) { channel.flush0() } + + func close(ctx: ChannelHandlerContext, promise: Promise) { + channel.close0(promise: promise) + } } -class TailChannelHandler : ChannelHandler { } +class TailChannelHandler : ChannelHandler { + public func errorCaught(ctx: ChannelHandlerContext, error: Error) { + // TODO: Log this and tell the user that its most likely a fault not handling it. + } + + public func channelRead(ctx: ChannelHandlerContext, data: Buffer) { + // TODO: Log this and tell the user that its most likely a fault not handling it. + } + + public func channelActive(ctx: ChannelHandlerContext) { + // Discard + } + + public func channelInactive(ctx: ChannelHandlerContext) { + // Discard + } + + public func channelReadComplete(ctx: ChannelHandlerContext) { + // Discard + } + + public func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) { + // Discard + } +}