Files
swift-nio/Sources/NIOCore/DeadChannel.swift
T
Cory Benfield ca136e8f80 Move Channel abstractions to NIOCore. (#1920)
Motivation:

The most important API surface area in NIO are the Channel abstractions.
These are shared in all NIO programs, and are also used by several
projects to implement their I/O abstraction. There are several moving
parts to this abstraction, all of which are moving:

- Channel itself
- ChannelPipeline
- ChannelHandler

As these all move, they force several other pieces of API to move with
them. Most notably they force us to move NIOAny, which also forces us to
move FileHandle and FileRegion. That also forces us to bring over part
of our syscall abstraction. This duplication is acceptable due to its
minimal surface area, but it is definitely a flaw in our abstraction
design that we had to do that at all.

We also need to move the channel option abstraction, AddressedEnvelope,
and the DeadChannel.

Modifications:

- Moved a bunch of the Channel abstraction over.
- Moved Channel-associated types.

Result:

Channel will be part of NIOCore.
2021-08-02 12:39:28 +01:00

118 lines
4.0 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
//
//===----------------------------------------------------------------------===//
/// A `DeadChannelCore` is a `ChannelCore` for a `DeadChannel`. A `DeadChannel` is used as a replacement `Channel` when
/// the original `Channel` is closed. Given that the original `Channel` is closed the `DeadChannelCore` should fail
/// all operations.
private final class DeadChannelCore: ChannelCore {
func localAddress0() throws -> SocketAddress {
throw ChannelError.ioOnClosedChannel
}
func remoteAddress0() throws -> SocketAddress {
throw ChannelError.ioOnClosedChannel
}
func register0(promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.ioOnClosedChannel)
}
func registerAlreadyConfigured0(promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.ioOnClosedChannel)
}
func bind0(to: SocketAddress, promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.ioOnClosedChannel)
}
func connect0(to: SocketAddress, promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.ioOnClosedChannel)
}
func write0(_ data: NIOAny, promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.ioOnClosedChannel)
}
func flush0() {
}
func read0() {
}
func close0(error: Error, mode: CloseMode, promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.alreadyClosed)
}
func triggerUserOutboundEvent0(_ event: Any, promise: EventLoopPromise<Void>?) {
promise?.fail(ChannelError.ioOnClosedChannel)
}
func channelRead0(_ data: NIOAny) {
// a `DeadChannel` should never be in any running `ChannelPipeline` and therefore the `TailChannelHandler`
// should never invoke this.
fatalError("\(#function) called on DeadChannelCore")
}
func errorCaught0(error: Error) {
// a `DeadChannel` should never be in any running `ChannelPipeline` and therefore the `TailChannelHandler`
// should never invoke this.
fatalError("\(#function) called on DeadChannelCore")
}
}
/// This represents a `Channel` which is already closed and therefore all the operations do fail.
/// A `ChannelPipeline` that is associated with a closed `Channel` must be careful to no longer use that original
/// channel as it only holds an unowned reference to the original `Channel`. `DeadChannel` serves as a replacement
/// that can be used when the original `Channel` might no longer be valid.
internal final class DeadChannel: Channel {
let eventLoop: EventLoop
let pipeline: ChannelPipeline
public var closeFuture: EventLoopFuture<Void> {
return self.eventLoop.makeSucceededFuture(())
}
internal init(pipeline: ChannelPipeline) {
self.pipeline = pipeline
self.eventLoop = pipeline.eventLoop
}
// This is `Channel` API so must be thread-safe.
var allocator: ByteBufferAllocator {
return ByteBufferAllocator()
}
var localAddress: SocketAddress? {
return nil
}
var remoteAddress: SocketAddress? {
return nil
}
let parent: Channel? = nil
func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
return self.pipeline.eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
}
func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
return eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
}
let isWritable = false
let isActive = false
let _channelCore: ChannelCore = DeadChannelCore()
}