Files
swift-nio/Sources/NIO/ChannelHandler.swift
T
Johannes Weiss c7e47e75f3 remove flush promises
Motivation:

We have multiple reasons why flush promises weren't great, for example:

- writeAndFlush sounds like it's a write + flush optimisation but in
  reality it was more expensive (2 extra promises)
- the semantics were never quite clear
- lots of implementation complexity, especially for the datagram channel

Modifications:

- removed the flush promises in the API
- this deliberately doesn't do the PendingWritesManager simplifications
  that this unlocks

Result:

flush doesn't have a promise anymore.
2018-02-20 16:13:41 +00:00

131 lines
4.6 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
//
//===----------------------------------------------------------------------===//
public protocol ChannelHandler: class {
func handlerAdded(ctx: ChannelHandlerContext)
func handlerRemoved(ctx: ChannelHandlerContext)
}
public protocol _ChannelOutboundHandler : ChannelHandler {
func register(ctx: ChannelHandlerContext, promise: EventLoopPromise<Void>?)
func bind(ctx: ChannelHandlerContext, to: SocketAddress, promise: EventLoopPromise<Void>?)
func connect(ctx: ChannelHandlerContext, to: SocketAddress, promise: EventLoopPromise<Void>?)
func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<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, mode: CloseMode, promise: EventLoopPromise<Void>?)
func triggerUserOutboundEvent(ctx: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?)
}
public protocol _ChannelInboundHandler : ChannelHandler {
func channelRegistered(ctx: ChannelHandlerContext)
func channelUnregistered(ctx: ChannelHandlerContext)
func channelActive(ctx: ChannelHandlerContext)
func channelInactive(ctx: ChannelHandlerContext)
func channelRead(ctx: ChannelHandlerContext, data: NIOAny)
func channelReadComplete(ctx: ChannelHandlerContext)
func channelWritabilityChanged(ctx: ChannelHandlerContext)
func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any)
func errorCaught(ctx: ChannelHandlerContext, error: Error)
}
// Default implementation for the ChannelHandler protocol
extension ChannelHandler {
public func handlerAdded(ctx: ChannelHandlerContext) {
// Do nothing by default
}
public func handlerRemoved(ctx: ChannelHandlerContext) {
// Do nothing by default
}
}
extension _ChannelOutboundHandler {
public func register(ctx: ChannelHandlerContext, promise: EventLoopPromise<Void>?) {
ctx.register(promise: promise)
}
public func bind(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
ctx.bind(to: address, promise: promise)
}
public func connect(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
ctx.connect(to: address, promise: promise)
}
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
ctx.write(data, promise: promise)
}
public func flush(ctx: ChannelHandlerContext) {
ctx.flush()
}
public func read(ctx: ChannelHandlerContext) {
ctx.read()
}
public func close(ctx: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) {
ctx.close(mode: mode, promise: promise)
}
public func triggerUserOutboundEvent(ctx: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
ctx.triggerUserOutboundEvent(event, promise: promise)
}
}
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: NIOAny) {
ctx.fireChannelRead(data)
}
public func channelReadComplete(ctx: ChannelHandlerContext) {
ctx.fireChannelReadComplete()
}
public func channelWritabilityChanged(ctx: ChannelHandlerContext) {
ctx.fireChannelWritabilityChanged()
}
public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
ctx.fireUserInboundEventTriggered(event)
}
public func errorCaught(ctx: ChannelHandlerContext, error: Error) {
ctx.fireErrorCaught(error)
}
}