mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
Use types to allow the compile to specialize the methods
This commit is contained in:
1 parent
fce361e337
commit
c6685c3e7c
8 files changed
+60
-50
No files matched your search
@@ -123,7 +123,7 @@ public final class ServerBootstrap {
|
||||
self.childOptions = childOptions
|
||||
}
|
||||
|
||||
func channelRead(ctx: ChannelHandlerContext, data: Any) {
|
||||
func channelRead<T: InboundData>(ctx: ChannelHandlerContext, data: T) {
|
||||
if let accepted = data as? SocketChannel {
|
||||
do {
|
||||
try self.childOptions.applyAll(channel: accepted)
|
||||
|
||||
+28
-17
@@ -321,7 +321,7 @@ final fileprivate class PendingWrites {
|
||||
/*
|
||||
All operations on SocketChannel are thread-safe
|
||||
*/
|
||||
final class SocketChannel : BaseSocketChannel {
|
||||
final class SocketChannel : BaseSocketChannel<ByteBuffer> {
|
||||
|
||||
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<SocketChannel> {
|
||||
|
||||
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<T: InboundData>(data: T) {
|
||||
assert(eventLoop.inEventLoop)
|
||||
|
||||
if let ch = data as? Channel {
|
||||
@@ -482,13 +493,13 @@ public protocol ChannelCore : class{
|
||||
func register0(promise: Promise<Void>)
|
||||
func bind0(local: SocketAddress, promise: Promise<Void>)
|
||||
func connect0(remote: SocketAddress, promise: Promise<Void>)
|
||||
func write0(data: Any, promise: Promise<Void>)
|
||||
func write0<T: OutboundData>(data: T, promise: Promise<Void>)
|
||||
func flush0()
|
||||
func readIfNeeded0()
|
||||
func startReading0()
|
||||
func stopReading0()
|
||||
func close0(promise: Promise<Void>, error: Error)
|
||||
func channelRead0(data: Any)
|
||||
func channelRead0<T: InboundData>(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<Void>) -> Future<Void>
|
||||
@discardableResult func connect(remote: SocketAddress, promise: Promise<Void>) -> Future<Void>
|
||||
func read()
|
||||
@discardableResult func write(data: Any, promise: Promise<Void>) -> Future<Void>
|
||||
@discardableResult func write<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void>
|
||||
func flush()
|
||||
@discardableResult func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void>
|
||||
@discardableResult func writeAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void>
|
||||
@discardableResult func close(promise: Promise<Void>) -> Future<Void>
|
||||
|
||||
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<Void>) -> Future<Void> {
|
||||
@discardableResult public func write<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
return pipeline.write(data: data, promise: promise)
|
||||
}
|
||||
|
||||
@@ -565,7 +576,7 @@ extension Channel {
|
||||
pipeline.read()
|
||||
}
|
||||
|
||||
@discardableResult public func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void> {
|
||||
@discardableResult public func writeAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
return pipeline.writeAndFlush(data: data, promise: promise)
|
||||
}
|
||||
|
||||
@@ -579,7 +590,7 @@ extension Channel {
|
||||
}
|
||||
}
|
||||
|
||||
class BaseSocketChannel : SelectableChannel, ChannelCore {
|
||||
class BaseSocketChannel<I: InboundData> : 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<Void>) -> Future<Void> {
|
||||
@discardableResult public final func write<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
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<Void>) -> Future<Void> {
|
||||
@discardableResult public final func writeAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
return pipeline.writeAndFlush(data: data, promise: promise)
|
||||
}
|
||||
|
||||
@@ -761,7 +772,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
|
||||
}
|
||||
}
|
||||
|
||||
public func write0(data: Any, promise: Promise<Void>) {
|
||||
public func write0<T: OutboundData>(data: T, promise: Promise<Void>) {
|
||||
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<T: InboundData>(data: T) {
|
||||
// Do nothing by default
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T: InboundData>(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<Void>)
|
||||
func bind(ctx: ChannelHandlerContext, local: SocketAddress, promise: Promise<Void>)
|
||||
func connect(ctx: ChannelHandlerContext, remote: SocketAddress, promise: Promise<Void>)
|
||||
func write(ctx: ChannelHandlerContext, data: Any, promise: Promise<Void>)
|
||||
func write<T: OutboundData>(ctx: ChannelHandlerContext, data: T, promise: Promise<Void>)
|
||||
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<T: InboundData>(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<Void>) {
|
||||
public func write<T: OutboundData>(ctx: ChannelHandlerContext, data: T, promise: Promise<Void>) {
|
||||
ctx.write(data: data, promise: promise)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T: InboundData>(data: T)
|
||||
|
||||
func fireChannelReadComplete()
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ import Foundation
|
||||
import Future
|
||||
import Sockets
|
||||
|
||||
public protocol OutboundData { }
|
||||
|
||||
public protocol ChannelOutboundInvoker {
|
||||
func register() -> Future<Void>
|
||||
@discardableResult func register(promise: Promise<Void>) -> Future<Void>
|
||||
@@ -25,14 +27,14 @@ public protocol ChannelOutboundInvoker {
|
||||
func connect(remote: SocketAddress) -> Future<Void>
|
||||
@discardableResult func connect(remote: SocketAddress, promise: Promise<Void>) -> Future<Void>
|
||||
|
||||
@discardableResult func write(data: Any) -> Future<Void>
|
||||
@discardableResult func write(data: Any, promise: Promise<Void>) -> Future<Void>
|
||||
@discardableResult func write<T: OutboundData>(data: T) -> Future<Void>
|
||||
@discardableResult func write<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void>
|
||||
|
||||
func flush()
|
||||
func read()
|
||||
|
||||
func writeAndFlush(data: Any) -> Future<Void>
|
||||
@discardableResult func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void>
|
||||
func writeAndFlush<T: OutboundData>(data: T) -> Future<Void>
|
||||
@discardableResult func writeAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void>
|
||||
|
||||
func close() -> Future<Void>
|
||||
@discardableResult func close(promise: Promise<Void>) -> Future<Void>
|
||||
@@ -54,12 +56,12 @@ public extension ChannelOutboundInvoker {
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func write(data: Any) -> Future<Void> {
|
||||
public func write<T: OutboundData>(data: T) -> Future<Void> {
|
||||
return write(data: data, promise: newVoidPromise())
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func writeAndFlush(data: Any) -> Future<Void> {
|
||||
public func writeAndFlush<T: OutboundData>(data: T) -> Future<Void> {
|
||||
return writeAndFlush(data: data, promise: newVoidPromise())
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ public final class ChannelPipeline : ChannelInboundInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
public func fireChannelRead(data: Any) {
|
||||
public func fireChannelRead<T: InboundData>(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<Void>) -> Future<Void> {
|
||||
func write<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
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<Void>) -> Future<Void> {
|
||||
func writeAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
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<T: InboundData>(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<Void>) {
|
||||
func write<T: OutboundData>(ctx: ChannelHandlerContext, data: T, promise: Promise<Void>) {
|
||||
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<T: InboundData>(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<T: InboundData>(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<Void>) -> Future<Void> {
|
||||
@discardableResult public func write<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
prev!.invokeWrite(data: data, promise: promise)
|
||||
return promise.futureResult
|
||||
}
|
||||
|
||||
@discardableResult public func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void> {
|
||||
@discardableResult public func writeAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) -> Future<Void> {
|
||||
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<T: InboundData>(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<Void>) {
|
||||
func invokeWrite<T: OutboundData>(data: T, promise: Promise<Void>) {
|
||||
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<Void>) {
|
||||
func invokeWriteAndFlush<T: OutboundData>(data: T, promise: Promise<Void>) {
|
||||
assert(inEventLoop)
|
||||
|
||||
handler.write(ctx: self, data: data, promise: promise)
|
||||
|
||||
@@ -84,7 +84,7 @@ class EmbeddedChannelCore : ChannelCore {
|
||||
promise.succeed(result: ())
|
||||
}
|
||||
|
||||
func write0(data: Any, promise: Promise<Void>) {
|
||||
func write0<T: OutboundData>(data: T, promise: Promise<Void>) {
|
||||
outboundBuffer.append(data as! ByteBuffer)
|
||||
promise.succeed(result: ())
|
||||
}
|
||||
@@ -102,7 +102,7 @@ class EmbeddedChannelCore : ChannelCore {
|
||||
func readIfNeeded0() {
|
||||
}
|
||||
|
||||
func channelRead0(data: Any) {
|
||||
func channelRead0<T: InboundData>(data: T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T: InboundData>(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
|
||||
|
||||
Reference in new issue
Block a user