Files
swift-nio/Sources/ChannelHandlerContext.swift
T
2017-05-08 07:16:30 +02:00

166 lines
4.5 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 Future
public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvoker {
public let handler: ChannelHandler
public let pipeline: ChannelPipeline
public let allocator: BufferAllocator
var prev: ChannelHandlerContext?
var next: ChannelHandlerContext?
init(handler: ChannelHandler, pipeline: ChannelPipeline, allocator: BufferAllocator) {
self.handler = handler
self.pipeline = pipeline
self.allocator = allocator;
}
public func fireChannelActive() {
next!.invokeChannelActive()
}
func invokeChannelActive() {
do {
try handler.channelActive(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
public func fireChannelInactive() {
next!.invokeChannelInactive()
}
func invokeChannelInactive() {
do {
try handler.channelInactive(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
public func fireChannelRead(data: Buffer) {
next!.invokeChannelRead(data: data)
}
func invokeChannelRead(data: Buffer) {
do {
try handler.channelRead(ctx: self, data: data)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
public func fireChannelReadComplete() {
next!.invokeChannelReadComplete()
}
func invokeChannelReadComplete() {
do {
try handler.channelReadComplete(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
public func fireChannelWritabilityChanged(writable: Bool) {
next!.invokeChannelWritabilityChanged(writable: writable)
}
public func invokeChannelWritabilityChanged(writable: Bool) {
do {
try handler.channelWritabilityChanged(ctx: self, writable: writable)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
public func fireErrorCaught(error: Error) {
next!.invokeErrorCaught(error: error)
}
func invokeErrorCaught(error: Error) {
do {
try handler.errorCaught(ctx: self, error: error)
} catch {
// TODO: What to do ?
}
}
public func fireUserEventTriggered(event: AnyClass) {
next!.invokeUserEventTriggered(event: event)
}
func invokeUserEventTriggered(event: AnyClass) {
do {
try handler.userEventTriggered(ctx: self, event: event)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
public func write(data: Buffer, promise: Promise<Void>) -> Future<Void> {
prev!.invokeWrite(data: data, promise: promise)
return promise.futureResult
}
func invokeWrite(data: Buffer, promise: Promise<Void>) {
handler.write(ctx: self, data: data, promise: promise)
}
public func writeAndFlush(data: Buffer, promise: Promise<Void>) -> Future<Void> {
prev!.invokeWrite(data: data, promise: promise)
prev!.invokeFlush()
return promise.futureResult
}
public func flush() {
prev!.invokeFlush()
}
func invokeFlush() {
handler.flush(ctx: self)
}
public func close(promise: Promise<Void>) -> Future<Void> {
prev!.invokeClose(promise: promise)
return promise.futureResult
}
func invokeClose(promise: Promise<Void>) {
handler.close(ctx: self, promise: promise)
}
func invokeHandlerAdded() throws {
try handler.handlerAdded(ctx: self)
}
func invokeHandlerRemoved() throws {
try handler.handlerRemoved(ctx: self)
}
private func safeErrorCaught(ctx: ChannelHandlerContext, error: Error) {
do {
try handler.errorCaught(ctx: ctx, error: error)
} catch {
// TOOO: What to do here ?
}
}
}