mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
The mention change was done for performance reasons as often the user only either implement inbound or outbound operations per handler. The problem before was that we had to traverse the full list all the time even if the user was not really interested in either inbound or outbound events. With this change we also split the doubly-linked-list into two linked-lists, one for inbound handlers and one for outbound handlers. This way we only need to traverse the handlers that are really interested in th event itself. To give an idea about the impact of the change in terms of performance, here are some numbers. Two ChannelInboundHandlers in the ChannelPipeline that do nothing and so just forward the events to the next handler in the pipeline: Before: `Bandwidth per channel: 18369.796⇅ Mbps (2296224.5 kBps)` After: `Bandwidth per channel: 20043.638⇅ Mbps (2505454.7 kBps)`
129 lines
4.3 KiB
Swift
129 lines
4.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
import Sockets
|
|
|
|
public protocol ChannelHandler : class {
|
|
func handlerAdded(ctx: ChannelHandlerContext) throws
|
|
func handlerRemoved(ctx: ChannelHandlerContext) throws
|
|
}
|
|
|
|
public protocol ChannelOutboundHandler : ChannelHandler {
|
|
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: IOData, 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)
|
|
func close(ctx: ChannelHandlerContext, promise: Promise<Void>)
|
|
}
|
|
|
|
public protocol ChannelInboundHandler : ChannelHandler {
|
|
func channelRegistered(ctx: ChannelHandlerContext) throws
|
|
func channelUnregistered(ctx: ChannelHandlerContext) throws
|
|
func channelActive(ctx: ChannelHandlerContext) throws
|
|
func channelInactive(ctx: ChannelHandlerContext) throws
|
|
func channelRead(ctx: ChannelHandlerContext, data: IOData) throws
|
|
func channelReadComplete(ctx: ChannelHandlerContext) throws
|
|
func channelWritabilityChanged(ctx: ChannelHandlerContext) throws
|
|
func userEventTriggered(ctx: ChannelHandlerContext, event: Any) throws
|
|
func errorCaught(ctx: ChannelHandlerContext, error: Error) throws
|
|
}
|
|
|
|
// Default implementation for the ChannelHandler protocol
|
|
public extension ChannelHandler {
|
|
|
|
public func handlerAdded(ctx: ChannelHandlerContext) {
|
|
// Do nothing by default
|
|
}
|
|
|
|
public func handlerRemoved(ctx: ChannelHandlerContext) {
|
|
// Do nothing by default
|
|
}
|
|
}
|
|
|
|
public extension ChannelOutboundHandler {
|
|
|
|
public func register(ctx: ChannelHandlerContext, promise: Promise<Void>) {
|
|
ctx.register(promise: promise)
|
|
}
|
|
|
|
public func bind(ctx: ChannelHandlerContext, local: SocketAddress, promise: Promise<Void>) {
|
|
ctx.bind(local: local, promise: promise)
|
|
}
|
|
|
|
public func connect(ctx: ChannelHandlerContext, remote: SocketAddress, promise: Promise<Void>) {
|
|
ctx.connect(remote: remote, promise: promise)
|
|
}
|
|
|
|
public func write(ctx: ChannelHandlerContext, data: IOData, promise: Promise<Void>) {
|
|
ctx.write(data: data, promise: promise)
|
|
}
|
|
|
|
public func flush(ctx: ChannelHandlerContext) {
|
|
ctx.flush()
|
|
}
|
|
|
|
public func read(ctx: ChannelHandlerContext) {
|
|
ctx.read()
|
|
}
|
|
|
|
public func close(ctx: ChannelHandlerContext, promise: Promise<Void>) {
|
|
ctx.close(promise: promise)
|
|
}
|
|
}
|
|
|
|
|
|
public extension ChannelInboundHandler {
|
|
|
|
public func channelRegistered(ctx: ChannelHandlerContext) {
|
|
ctx.fireChannelRegistered()
|
|
}
|
|
|
|
public func channelUnregistered(ctx: ChannelHandlerContext) {
|
|
ctx.fireChannelUnregistered()
|
|
}
|
|
|
|
public func channelActive(ctx: ChannelHandlerContext) {
|
|
ctx.fireChannelActive()
|
|
}
|
|
|
|
public func channelInactive(ctx: ChannelHandlerContext) {
|
|
ctx.fireChannelInactive()
|
|
}
|
|
|
|
public func channelRead(ctx: ChannelHandlerContext, data: IOData) {
|
|
ctx.fireChannelRead(data: data)
|
|
}
|
|
|
|
public func channelReadComplete(ctx: ChannelHandlerContext) {
|
|
ctx.fireChannelReadComplete()
|
|
}
|
|
|
|
public func channelWritabilityChanged(ctx: ChannelHandlerContext) {
|
|
ctx.fireChannelWritabilityChanged()
|
|
}
|
|
|
|
public func userEventTriggered(ctx: ChannelHandlerContext, event: Any) {
|
|
ctx.fireUserEventTriggered(event: event)
|
|
}
|
|
|
|
public func errorCaught(ctx: ChannelHandlerContext, error: Error) {
|
|
ctx.fireErrorCaught(error: error)
|
|
}
|
|
}
|
|
|