From c6685c3e7cd3339e4d347ffa8fbea4229b91c578 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 9 Jun 2017 15:47:10 +0200 Subject: [PATCH] Use types to allow the compile to specialize the methods --- Sources/NIO/Bootstrap.swift | 2 +- Sources/NIO/Channel.swift | 45 +++++++++++++++--------- Sources/NIO/ChannelHandler.swift | 8 ++--- Sources/NIO/ChannelInboundInvoker.swift | 4 ++- Sources/NIO/ChannelOutboundInvoker.swift | 14 ++++---- Sources/NIO/ChannelPipeline.swift | 24 ++++++------- Sources/NIO/Embedded.swift | 4 +-- Sources/NIOEchoServer/main.swift | 9 ++--- 8 files changed, 60 insertions(+), 50 deletions(-) diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index 38e7860b5..4fbc173c3 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -123,7 +123,7 @@ public final class ServerBootstrap { self.childOptions = childOptions } - func channelRead(ctx: ChannelHandlerContext, data: Any) { + func channelRead(ctx: ChannelHandlerContext, data: T) { if let accepted = data as? SocketChannel { do { try self.childOptions.applyAll(channel: accepted) diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index 466a42ab0..3691adeb3 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -321,7 +321,7 @@ final fileprivate class PendingWrites { /* All operations on SocketChannel are thread-safe */ -final class SocketChannel : BaseSocketChannel { +final class SocketChannel : BaseSocketChannel { init(eventLoop: SelectableEventLoop) throws { let socket = try Socket() @@ -340,7 +340,7 @@ final class SocketChannel : BaseSocketChannel { super.init(socket: socket, eventLoop: eventLoop) } - override fileprivate func readFromSocket() throws -> Any? { + override fileprivate func readFromSocket() throws -> ByteBuffer? { var buffer = try recvAllocator.buffer(allocator: allocator) if let bytesRead = try buffer.withMutableWritePointer { try (self.socket as! Socket).read(pointer: $0, size: $1) } { if bytesRead > 0 { @@ -385,10 +385,21 @@ final class SocketChannel : BaseSocketChannel { } } + +/* + ByteBuffer will be read and written to the Channel so declare it as InboundData and OutboundData + */ +extension ByteBuffer : InboundData, OutboundData { } + +/* + SocketChannel is read when using the ServerSocketChannel so delcare it was InboundData + */ +extension SocketChannel : InboundData { } + /* All operations on ServerSocketChannel are thread-safe */ -final class ServerSocketChannel : BaseSocketChannel { +final class ServerSocketChannel : BaseSocketChannel { private var backlog: Int32 = 128 private let group: EventLoopGroup @@ -442,7 +453,7 @@ final class ServerSocketChannel : BaseSocketChannel { throw ChannelError.operationUnsupported } - override fileprivate func readFromSocket() throws -> Any? { + override fileprivate func readFromSocket() throws -> SocketChannel? { if let accepted = try (self.socket as! ServerSocket).accept() { do { return try SocketChannel(socket: accepted, eventLoop: group.next() as! SelectableEventLoop) @@ -459,7 +470,7 @@ final class ServerSocketChannel : BaseSocketChannel { return true } - override public func channelRead0(data: Any) { + override public func channelRead0(data: T) { assert(eventLoop.inEventLoop) if let ch = data as? Channel { @@ -482,13 +493,13 @@ public protocol ChannelCore : class{ func register0(promise: Promise) func bind0(local: SocketAddress, promise: Promise) func connect0(remote: SocketAddress, promise: Promise) - func write0(data: Any, promise: Promise) + func write0(data: T, promise: Promise) func flush0() func readIfNeeded0() func startReading0() func stopReading0() func close0(promise: Promise, error: Error) - func channelRead0(data: Any) + func channelRead0(data: T) var closed: Bool { get } var eventLoop: EventLoop { get } } @@ -517,9 +528,9 @@ public protocol Channel : class, ChannelOutboundInvoker { @discardableResult func bind(local: SocketAddress, promise: Promise) -> Future @discardableResult func connect(remote: SocketAddress, promise: Promise) -> Future func read() - @discardableResult func write(data: Any, promise: Promise) -> Future + @discardableResult func write(data: T, promise: Promise) -> Future func flush() - @discardableResult func writeAndFlush(data: Any, promise: Promise) -> Future + @discardableResult func writeAndFlush(data: T, promise: Promise) -> Future @discardableResult func close(promise: Promise) -> Future var _unsafe: ChannelCore { get } @@ -553,7 +564,7 @@ extension Channel { return pipeline.connect(remote: remote, promise: promise) } - @discardableResult public func write(data: Any, promise: Promise) -> Future { + @discardableResult public func write(data: T, promise: Promise) -> Future { return pipeline.write(data: data, promise: promise) } @@ -565,7 +576,7 @@ extension Channel { pipeline.read() } - @discardableResult public func writeAndFlush(data: Any, promise: Promise) -> Future { + @discardableResult public func writeAndFlush(data: T, promise: Promise) -> Future { return pipeline.writeAndFlush(data: data, promise: promise) } @@ -579,7 +590,7 @@ extension Channel { } } -class BaseSocketChannel : SelectableChannel, ChannelCore { +class BaseSocketChannel : SelectableChannel, ChannelCore { public final var selectable: Selectable { return socket } @@ -729,7 +740,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { return pipeline.connect(remote: remote, promise: promise) } - @discardableResult public final func write(data: Any, promise: Promise) -> Future { + @discardableResult public final func write(data: T, promise: Promise) -> Future { return pipeline.write(data: data, promise: promise) } @@ -741,7 +752,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { pipeline.read() } - @discardableResult public final func writeAndFlush(data: Any, promise: Promise) -> Future { + @discardableResult public final func writeAndFlush(data: T, promise: Promise) -> Future { return pipeline.writeAndFlush(data: data, promise: promise) } @@ -761,7 +772,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { } } - public func write0(data: Any, promise: Promise) { + public func write0(data: T, promise: Promise) { assert(eventLoop.inEventLoop) guard !closed else { @@ -1015,7 +1026,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { } } - fileprivate func readFromSocket() throws -> Any? { + fileprivate func readFromSocket() throws -> I? { fatalError("this must be overridden by sub class") } @@ -1024,7 +1035,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { fatalError("this must be overridden by sub class") } - public func channelRead0(data: Any) { + public func channelRead0(data: T) { // Do nothing by default } diff --git a/Sources/NIO/ChannelHandler.swift b/Sources/NIO/ChannelHandler.swift index ee93eb728..27031ce46 100644 --- a/Sources/NIO/ChannelHandler.swift +++ b/Sources/NIO/ChannelHandler.swift @@ -21,7 +21,7 @@ public protocol ChannelHandler : class { func channelUnregistered(ctx: ChannelHandlerContext) throws func channelActive(ctx: ChannelHandlerContext) throws func channelInactive(ctx: ChannelHandlerContext) throws - func channelRead(ctx: ChannelHandlerContext, data: Any) throws + func channelRead(ctx: ChannelHandlerContext, data: T) throws func channelReadComplete(ctx: ChannelHandlerContext) throws func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) throws func userEventTriggered(ctx: ChannelHandlerContext, event: Any) throws @@ -29,7 +29,7 @@ public protocol ChannelHandler : class { func register(ctx: ChannelHandlerContext, promise: Promise) func bind(ctx: ChannelHandlerContext, local: SocketAddress, promise: Promise) func connect(ctx: ChannelHandlerContext, remote: SocketAddress, promise: Promise) - func write(ctx: ChannelHandlerContext, data: Any, promise: Promise) + func write(ctx: ChannelHandlerContext, data: T, promise: Promise) func flush(ctx: ChannelHandlerContext) // TODO: Think about make this more flexible in terms of influence the allocation that is used to read the next amount of data func read(ctx: ChannelHandlerContext) @@ -57,7 +57,7 @@ public extension ChannelHandler { ctx.fireChannelInactive() } - public func channelRead(ctx: ChannelHandlerContext, data: Any) { + public func channelRead(ctx: ChannelHandlerContext, data: T) { ctx.fireChannelRead(data: data) } @@ -89,7 +89,7 @@ public extension ChannelHandler { ctx.connect(remote: remote, promise: promise) } - public func write(ctx: ChannelHandlerContext, data: Any, promise: Promise) { + public func write(ctx: ChannelHandlerContext, data: T, promise: Promise) { ctx.write(data: data, promise: promise) } diff --git a/Sources/NIO/ChannelInboundInvoker.swift b/Sources/NIO/ChannelInboundInvoker.swift index 06da9a185..7c413842e 100644 --- a/Sources/NIO/ChannelInboundInvoker.swift +++ b/Sources/NIO/ChannelInboundInvoker.swift @@ -14,6 +14,8 @@ import Foundation +public protocol InboundData { } + public protocol ChannelInboundInvoker { func fireChannelRegistered() @@ -24,7 +26,7 @@ public protocol ChannelInboundInvoker { func fireChannelInactive() - func fireChannelRead(data: Any) + func fireChannelRead(data: T) func fireChannelReadComplete() diff --git a/Sources/NIO/ChannelOutboundInvoker.swift b/Sources/NIO/ChannelOutboundInvoker.swift index b49fd4545..c8b60ea33 100644 --- a/Sources/NIO/ChannelOutboundInvoker.swift +++ b/Sources/NIO/ChannelOutboundInvoker.swift @@ -16,6 +16,8 @@ import Foundation import Future import Sockets +public protocol OutboundData { } + public protocol ChannelOutboundInvoker { func register() -> Future @discardableResult func register(promise: Promise) -> Future @@ -25,14 +27,14 @@ public protocol ChannelOutboundInvoker { func connect(remote: SocketAddress) -> Future @discardableResult func connect(remote: SocketAddress, promise: Promise) -> Future - @discardableResult func write(data: Any) -> Future - @discardableResult func write(data: Any, promise: Promise) -> Future + @discardableResult func write(data: T) -> Future + @discardableResult func write(data: T, promise: Promise) -> Future func flush() func read() - func writeAndFlush(data: Any) -> Future - @discardableResult func writeAndFlush(data: Any, promise: Promise) -> Future + func writeAndFlush(data: T) -> Future + @discardableResult func writeAndFlush(data: T, promise: Promise) -> Future func close() -> Future @discardableResult func close(promise: Promise) -> Future @@ -54,12 +56,12 @@ public extension ChannelOutboundInvoker { } @discardableResult - public func write(data: Any) -> Future { + public func write(data: T) -> Future { return write(data: data, promise: newVoidPromise()) } @discardableResult - public func writeAndFlush(data: Any) -> Future { + public func writeAndFlush(data: T) -> Future { return writeAndFlush(data: data, promise: newVoidPromise()) } diff --git a/Sources/NIO/ChannelPipeline.swift b/Sources/NIO/ChannelPipeline.swift index 163158c34..7cff13aaa 100644 --- a/Sources/NIO/ChannelPipeline.swift +++ b/Sources/NIO/ChannelPipeline.swift @@ -234,7 +234,7 @@ public final class ChannelPipeline : ChannelInboundInvoker { } } - public func fireChannelRead(data: Any) { + public func fireChannelRead(data: T) { if eventLoop.inEventLoop { fireChannelRead0(data: data) } else { @@ -316,7 +316,7 @@ public final class ChannelPipeline : ChannelInboundInvoker { } @discardableResult - func write(data: Any, promise: Promise) -> Future { + func write(data: T, promise: Promise) -> Future { if eventLoop.inEventLoop { tail!.invokeWrite(data: data, promise: promise) } else { @@ -328,7 +328,7 @@ public final class ChannelPipeline : ChannelInboundInvoker { } @discardableResult - func writeAndFlush(data: Any, promise: Promise) -> Future { + func writeAndFlush(data: T, promise: Promise) -> Future { if eventLoop.inEventLoop { tail!.invokeWriteAndFlush(data: data, promise: promise) } else { @@ -393,7 +393,7 @@ public final class ChannelPipeline : ChannelInboundInvoker { head!.invokeChannelActive() } - func fireChannelRead0(data: Any) { + func fireChannelRead0(data: T) { head!.invokeChannelRead(data: data) } @@ -447,7 +447,7 @@ private final class HeadChannelHandler : ChannelHandler { ctx.channel!._unsafe.connect0(remote: remote, promise: promise) } - func write(ctx: ChannelHandlerContext, data: Any, promise: Promise) { + func write(ctx: ChannelHandlerContext, data: T, promise: Promise) { ctx.channel!._unsafe.write0(data: data, promise: promise) } @@ -524,7 +524,7 @@ private final class TailChannelHandler : ChannelHandler { // TODO: Log this and tell the user that its most likely a fault not handling it. } - func channelRead(ctx: ChannelHandlerContext, data: Any) { + func channelRead(ctx: ChannelHandlerContext, data: T) { ctx.channel!._unsafe.channelRead0(data: data) } } @@ -577,7 +577,7 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun next!.invokeChannelInactive() } - public func fireChannelRead(data: Any) { + public func fireChannelRead(data: T) { next!.invokeChannelRead(data: data) } @@ -612,12 +612,12 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun return promise.futureResult } - @discardableResult public func write(data: Any, promise: Promise) -> Future { + @discardableResult public func write(data: T, promise: Promise) -> Future { prev!.invokeWrite(data: data, promise: promise) return promise.futureResult } - @discardableResult public func writeAndFlush(data: Any, promise: Promise) -> Future { + @discardableResult public func writeAndFlush(data: T, promise: Promise) -> Future { prev!.invokeWriteAndFlush(data: data, promise: promise) return promise.futureResult } @@ -675,7 +675,7 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun } } - func invokeChannelRead(data: Any) { + func invokeChannelRead(data: T) { assert(inEventLoop) do { @@ -744,7 +744,7 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun handler.connect(ctx: self, remote: remote, promise: promise) } - func invokeWrite(data: Any, promise: Promise) { + func invokeWrite(data: T, promise: Promise) { assert(inEventLoop) handler.write(ctx: self, data: data, promise: promise) @@ -756,7 +756,7 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun handler.flush(ctx: self) } - func invokeWriteAndFlush(data: Any, promise: Promise) { + func invokeWriteAndFlush(data: T, promise: Promise) { assert(inEventLoop) handler.write(ctx: self, data: data, promise: promise) diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 9adcc19bf..421007d24 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -84,7 +84,7 @@ class EmbeddedChannelCore : ChannelCore { promise.succeed(result: ()) } - func write0(data: Any, promise: Promise) { + func write0(data: T, promise: Promise) { outboundBuffer.append(data as! ByteBuffer) promise.succeed(result: ()) } @@ -102,7 +102,7 @@ class EmbeddedChannelCore : ChannelCore { func readIfNeeded0() { } - func channelRead0(data: Any) { + func channelRead0(data: T) { } } diff --git a/Sources/NIOEchoServer/main.swift b/Sources/NIOEchoServer/main.swift index f22e258e3..411f26197 100644 --- a/Sources/NIOEchoServer/main.swift +++ b/Sources/NIOEchoServer/main.swift @@ -18,13 +18,8 @@ import Future public class EchoHandler: ChannelHandler { - public func channelRead(ctx: ChannelHandlerContext, data: Any) { - let f = ctx.write(data: data) - - // If the write fails close the channel - /*f.whenFailure(callback: { error in - let _ = ctx.close() - })*/ + public func channelRead(ctx: ChannelHandlerContext, data: T) { + ctx.write(data: data as! ByteBuffer) } // Flush it out. This can make use of gathering writes if multiple buffers are pending