Move ChannelHandlerContext class to ChannelPipeline.swift to be able to use fileprivate.

This commit is contained in:
Norman Maurer committed 2017-05-15 11:46:29 +02:00
1 parent f6762500bb
commit ec16d01c7c
3 files changed
+290 -308

No files matched your search

+2 -3
View File
@@ -37,8 +37,8 @@ public class Channel : ChannelOutboundInvoker {
public let eventLoop: EventLoop
// Visible to access from EventLoop directly
let socket: Socket
var interestedEvent: InterestedEvent? = nil
internal let socket: Socket
internal var interestedEvent: InterestedEvent? = nil
// TODO: This is most likely not the best datastructure for us. Linked-List would be better.
private var pendingWrites: [(Buffer, Promise<Void>)] = Array()
@@ -287,7 +287,6 @@ public class Channel : ChannelOutboundInvoker {
pendingWrites.removeAll()
outstanding = 0
}
init(socket: Socket, eventLoop: EventLoop) {
self.socket = socket
-298
View File
@@ -1,298 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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 {
// visible for ChannelPipeline to modify and also marked as weak to ensure we not create a
// reference-cycle for the doubly-linked-list
weak var prev: ChannelHandlerContext?
var next: ChannelHandlerContext?
// marked as weak to not create a reference cycle between this instance and the pipeline
public private(set) weak var pipeline: ChannelPipeline?
public let handler: ChannelHandler
public let name: String
public let eventLoop: EventLoop
// Only created from within ChannelPipeline
init(name: String, handler: ChannelHandler, pipeline: ChannelPipeline) {
self.name = name
self.handler = handler
self.pipeline = pipeline
self.eventLoop = pipeline.eventLoop
}
public func fireChannelRegistered() {
if let ctx = next {
ctx.invokeChannelRegistered()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelUnregistered() {
if let ctx = next {
ctx.invokeChannelUnregistered()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelActive() {
if let ctx = next {
ctx.invokeChannelActive()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelInactive() {
if let ctx = next {
ctx.invokeChannelInactive()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelRead(data: Any) {
if let ctx = next {
ctx.invokeChannelRead(data: data)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelReadComplete() {
if let ctx = next {
ctx.invokeChannelReadComplete()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelWritabilityChanged(writable: Bool) {
if let ctx = next {
ctx.invokeChannelWritabilityChanged(writable: writable)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireErrorCaught(error: Error) {
if let ctx = next {
ctx.invokeErrorCaught(error: error)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireUserEventTriggered(event: Any) {
if let ctx = next {
ctx.invokeUserEventTriggered(event: event)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func write(data: Any, promise: Promise<Void>) -> Future<Void> {
if let ctx = prev {
ctx.invokeWrite(data: data, promise: promise)
} else {
promise.fail(error: ChannelPipelineException.alreadyRemoved)
}
return promise.futureResult
}
public func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void> {
if let ctx = prev {
ctx.invokeWriteAndFlush(data: data, promise: promise)
} else {
promise.fail(error: ChannelPipelineException.alreadyRemoved)
}
return promise.futureResult
}
public func flush() {
prev?.invokeFlush()
}
public func read() {
prev?.invokeRead()
}
public func close(promise: Promise<Void>) -> Future<Void> {
if let ctx = prev {
ctx.invokeClose(promise: promise)
} else {
promise.fail(error: ChannelPipelineException.alreadyRemoved)
}
return promise.futureResult
}
func invokeChannelRegistered() {
assert(inEventLoop)
do {
try handler.channelRegistered(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelUnregistered() {
assert(inEventLoop)
do {
try handler.channelUnregistered(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelActive() {
assert(inEventLoop)
do {
try handler.channelActive(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelInactive() {
assert(inEventLoop)
do {
try handler.channelInactive(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelRead(data: Any) {
assert(inEventLoop)
do {
try handler.channelRead(ctx: self, data: data)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelReadComplete() {
assert(inEventLoop)
do {
try handler.channelReadComplete(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelWritabilityChanged(writable: Bool) {
assert(inEventLoop)
do {
try handler.channelWritabilityChanged(ctx: self, writable: writable)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeErrorCaught(error: Error) {
assert(inEventLoop)
do {
try handler.errorCaught(ctx: self, error: error)
} catch {
// TODO: What to do ?
}
}
func invokeUserEventTriggered(event: Any) {
assert(inEventLoop)
do {
try handler.userEventTriggered(ctx: self, event: event)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeWrite(data: Any, promise: Promise<Void>) {
assert(inEventLoop)
handler.write(ctx: self, data: data, promise: promise)
}
func invokeFlush() {
assert(inEventLoop)
handler.flush(ctx: self)
}
func invokeWriteAndFlush(data: Any, promise: Promise<Void>) {
assert(inEventLoop)
handler.write(ctx: self, data: data, promise: promise)
handler.flush(ctx: self)
}
func invokeRead() {
assert(inEventLoop)
handler.read(ctx: self)
}
func invokeClose(promise: Promise<Void>) {
assert(inEventLoop)
handler.close(ctx: self, promise: promise)
}
func invokeHandlerAdded() throws {
assert(inEventLoop)
try handler.handlerAdded(ctx: self)
}
func invokeHandlerRemoved() throws {
assert(inEventLoop)
defer {
pipeline = nil
prev = nil
next = nil
}
try handler.handlerRemoved(ctx: self)
}
private var inEventLoop : Bool {
return eventLoop.inEventLoop
}
private func safeErrorCaught(ctx: ChannelHandlerContext, error: Error) {
do {
try handler.errorCaught(ctx: ctx, error: error)
} catch {
// TOOO: What to do here ?
}
}
}
+288 -7
View File
@@ -21,11 +21,11 @@ public class ChannelPipeline : ChannelInboundInvoker, ChannelOutboundInvoker {
private var head: ChannelHandlerContext?
private var tail: ChannelHandlerContext?
private var idx: Int = 0
fileprivate weak var channel: Channel?
public let config: ChannelConfig
public let eventLoop: EventLoop
public func add(name: String? = nil, handler: ChannelHandler, first: Bool = false) throws {
let ctx = ChannelHandlerContext(name: name ?? nextName(), handler: handler, pipeline: self)
if first {
@@ -84,7 +84,6 @@ public class ChannelPipeline : ChannelInboundInvoker, ChannelOutboundInvoker {
}
return true
}
public func contains(name: String) -> Bool {
@@ -263,10 +262,8 @@ private class HeadChannelHandler : ChannelHandler {
}
private func readIfNeeded() {
if pipeline.config.autoRead {
if let ch = pipeline.channel {
ch.read()
}
if pipeline.config.autoRead, let ch = pipeline.channel {
ch.read()
}
}
}
@@ -313,3 +310,287 @@ private class TailChannelHandler : ChannelHandler {
public enum ChannelPipelineException : Error {
case alreadyRemoved
}
public class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboundInvoker {
// visible for ChannelPipeline to modify and also marked as weak to ensure we not create a
// reference-cycle for the doubly-linked-list
fileprivate weak var prev: ChannelHandlerContext?
fileprivate var next: ChannelHandlerContext?
// marked as weak to not create a reference cycle between this instance and the pipeline
public private(set) weak var pipeline: ChannelPipeline?
public let handler: ChannelHandler
public let name: String
public let eventLoop: EventLoop
// Only created from within ChannelPipeline
init(name: String, handler: ChannelHandler, pipeline: ChannelPipeline) {
self.name = name
self.handler = handler
self.pipeline = pipeline
self.eventLoop = pipeline.eventLoop
}
public func fireChannelRegistered() {
if let ctx = next {
ctx.invokeChannelRegistered()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelUnregistered() {
if let ctx = next {
ctx.invokeChannelUnregistered()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelActive() {
if let ctx = next {
ctx.invokeChannelActive()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelInactive() {
if let ctx = next {
ctx.invokeChannelInactive()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelRead(data: Any) {
if let ctx = next {
ctx.invokeChannelRead(data: data)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelReadComplete() {
if let ctx = next {
ctx.invokeChannelReadComplete()
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireChannelWritabilityChanged(writable: Bool) {
if let ctx = next {
ctx.invokeChannelWritabilityChanged(writable: writable)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireErrorCaught(error: Error) {
if let ctx = next {
ctx.invokeErrorCaught(error: error)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func fireUserEventTriggered(event: Any) {
if let ctx = next {
ctx.invokeUserEventTriggered(event: event)
} else {
safeErrorCaught(ctx: self, error: ChannelPipelineException.alreadyRemoved)
}
}
public func write(data: Any, promise: Promise<Void>) -> Future<Void> {
if let ctx = prev {
ctx.invokeWrite(data: data, promise: promise)
} else {
promise.fail(error: ChannelPipelineException.alreadyRemoved)
}
return promise.futureResult
}
public func writeAndFlush(data: Any, promise: Promise<Void>) -> Future<Void> {
if let ctx = prev {
ctx.invokeWriteAndFlush(data: data, promise: promise)
} else {
promise.fail(error: ChannelPipelineException.alreadyRemoved)
}
return promise.futureResult
}
public func flush() {
prev?.invokeFlush()
}
public func read() {
prev?.invokeRead()
}
public func close(promise: Promise<Void>) -> Future<Void> {
if let ctx = prev {
ctx.invokeClose(promise: promise)
} else {
promise.fail(error: ChannelPipelineException.alreadyRemoved)
}
return promise.futureResult
}
func invokeChannelRegistered() {
assert(inEventLoop)
do {
try handler.channelRegistered(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelUnregistered() {
assert(inEventLoop)
do {
try handler.channelUnregistered(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelActive() {
assert(inEventLoop)
do {
try handler.channelActive(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelInactive() {
assert(inEventLoop)
do {
try handler.channelInactive(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelRead(data: Any) {
assert(inEventLoop)
do {
try handler.channelRead(ctx: self, data: data)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelReadComplete() {
assert(inEventLoop)
do {
try handler.channelReadComplete(ctx: self)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeChannelWritabilityChanged(writable: Bool) {
assert(inEventLoop)
do {
try handler.channelWritabilityChanged(ctx: self, writable: writable)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeErrorCaught(error: Error) {
assert(inEventLoop)
do {
try handler.errorCaught(ctx: self, error: error)
} catch {
// TODO: What to do ?
}
}
func invokeUserEventTriggered(event: Any) {
assert(inEventLoop)
do {
try handler.userEventTriggered(ctx: self, event: event)
} catch let err {
safeErrorCaught(ctx: self, error: err)
}
}
func invokeWrite(data: Any, promise: Promise<Void>) {
assert(inEventLoop)
handler.write(ctx: self, data: data, promise: promise)
}
func invokeFlush() {
assert(inEventLoop)
handler.flush(ctx: self)
}
func invokeWriteAndFlush(data: Any, promise: Promise<Void>) {
assert(inEventLoop)
handler.write(ctx: self, data: data, promise: promise)
handler.flush(ctx: self)
}
func invokeRead() {
assert(inEventLoop)
handler.read(ctx: self)
}
func invokeClose(promise: Promise<Void>) {
assert(inEventLoop)
handler.close(ctx: self, promise: promise)
}
func invokeHandlerAdded() throws {
assert(inEventLoop)
try handler.handlerAdded(ctx: self)
}
func invokeHandlerRemoved() throws {
assert(inEventLoop)
defer {
pipeline = nil
prev = nil
next = nil
}
try handler.handlerRemoved(ctx: self)
}
private var inEventLoop : Bool {
return eventLoop.inEventLoop
}
private func safeErrorCaught(ctx: ChannelHandlerContext, error: Error) {
do {
try handler.errorCaught(ctx: ctx, error: error)
} catch {
// TOOO: What to do here ?
}
}
}