Correctly implement TailHandler and ensure we also forward close operations

This commit is contained in:
Norman Maurer committed 2017-04-30 18:26:44 +02:00
1 parent 0981cfd9b6
commit 5bee1c7bcd
3 files changed
+62 -33

No files matched your search

+18 -16
View File
@@ -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<Void>())
close0()
}
}
}
@@ -94,7 +96,7 @@ public class Channel : ChannelOutboundInvoker {
try selector.reregister(selectable: socket, interested: InterestedEvent.Read)
} catch {
// TODO: Log ?
close0(promise: Promise<Void>())
close0()
}
}
}
@@ -120,7 +122,9 @@ public class Channel : ChannelOutboundInvoker {
pendingWrites.removeFirst()
}
close0(promise: Promise<Void>())
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<Void>())
close0()
}
}
}
func close0(promise: Promise<Void>) {
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<Void> = Promise<Void>()) {
defer {
// Ensure this is always called
pipeline.fireChannelInactive()
}
do {
try socket.close()
promise.succeed(result: ())
} catch let err {
promise.fail(error: err)
}
}
+12 -13
View File
@@ -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<Void>) -> Future<Void> {
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<Void>) -> Future<Void> {
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<Void>) -> Future<Void> {
prev?.invokeClose(promise: promise)
prev!.invokeClose(promise: promise)
return promise.futureResult
}
+32 -4
View File
@@ -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<Void>) {
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
}
}