Files
swift-nio/Sources/NIO/ChannelOption.swift
T
Johannes Weiss bf3b87cb4e simplify ChannelOptions (#828)
Motivation:

ChannelOptions used a too complicated mechanism for what they are.

Modifications:

- change the ChannelOptions to a much simpler mechanism
- rename the generic parameters from T to Option

Result:

code easier to read and understand
2019-02-19 18:31:26 +00:00

189 lines
7.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
//
//===----------------------------------------------------------------------===//
/// A configuration option that can be set on a `Channel` to configure different behaviour.
public protocol ChannelOption: Equatable {
/// The type of the `ChannelOption`'s value.
associatedtype Value
}
public typealias SocketOptionName = Int32
#if os(Linux)
public typealias SocketOptionLevel = Int
public typealias SocketOptionValue = Int
#else
public typealias SocketOptionLevel = Int32
public typealias SocketOptionValue = Int32
#endif
/// `SocketOption` allows users to specify configuration settings that are directly applied to the underlying socket file descriptor.
///
/// Valid options are typically found in the various man pages like `man 4 tcp`.
public struct SocketOption: ChannelOption, Equatable {
public typealias Value = (SocketOptionValue)
public var level: SocketOptionLevel
public var name: SocketOptionName
/// Create a new `SocketOption`.
///
/// - parameters:
/// - level: The level for the option as defined in `man setsockopt`, e.g. SO_SOCKET.
/// - name: The name of the option as defined in `man setsockopt`, e.g. `SO_REUSEADDR`.
public init(level: SocketOptionLevel, name: SocketOptionName) {
self.level = level
self.name = name
}
}
/// `AllocatorOption` allows to specify the `ByteBufferAllocator` to use.
public struct AllocatorOption: ChannelOption {
public typealias Value = ByteBufferAllocator
public init() {}
}
/// `RecvAllocatorOption` allows users to specify the `RecvByteBufferAllocator` to use.
public struct RecvAllocatorOption: ChannelOption {
public typealias Value = RecvByteBufferAllocator
public init() {}
}
/// `AutoReadOption` allows users to configure if a `Channel` should automatically call `Channel.read` again once all data was read from the transport or
/// if the user is responsible to call `Channel.read` manually.
public struct AutoReadOption: ChannelOption {
public typealias Value = Bool
public init() {}
}
/// `WriteSpinOption` allows users to configure the number of repetitions of a only partially successful write call before considering the `Channel` not writable.
/// Setting this option to `0` means that we only issue one write call and if that call does not write all the bytes,
/// we consider the `Channel` not writable.
public struct WriteSpinOption: ChannelOption {
public typealias Value = UInt
public init() {}
}
/// `MaxMessagesPerReadOption` allows users to configure the maximum number of read calls to the underlying transport are performed before wait again until
/// there is more to read and be notified.
public struct MaxMessagesPerReadOption: ChannelOption {
public typealias Value = UInt
public init() {}
}
/// `BacklogOption` allows users to configure the `backlog` value as specified in `man 2 listen`. This is only useful for `ServerSocketChannel`s.
public struct BacklogOption: ChannelOption {
public typealias Value = Int32
public init() {}
}
/// The watermark used to detect when `Channel.isWritable` returns `true` or `false`.
public struct WriteBufferWaterMark {
/// The low mark setting for a `Channel`.
///
/// When the amount of buffered bytes in the `Channel`s outbound buffer drops below this value the `Channel` will be
/// marked as writable again (after it was non-writable).
public let low: Int
/// The high mark setting for a `Channel`.
///
/// When the amount of buffered bytes in the `Channel`s outbound exceeds this value the `Channel` will be
/// marked as non-writable. It will be marked as writable again once the amount of buffered bytes drops below `low`.
public let high: Int
/// Create a new instance.
///
/// Valid initialization is restricted to `1 <= low <= high`.
///
/// - parameters:
/// - low: The low watermark.
/// - high: The high watermark.
public init(low: Int, high: Int) {
precondition(low >= 1, "low must be >= 1 but was \(low)")
precondition(high >= low, "low must be <= high, but was low: \(low) high: \(high)")
self.low = low
self.high = high
}
}
/// `WriteBufferWaterMarkOption` allows users to configure when a `Channel` should be marked as writable or not. Once the amount of bytes queued in a
/// `Channel`s outbound buffer is larger than `WriteBufferWaterMark.high` the channel will be marked as non-writable and so
/// `Channel.isWritable` will return `false`. Once we were able to write some data out of the outbound buffer and the amount of bytes queued
/// falls below `WriteBufferWaterMark.low` the `Channel` will become writable again. Once this happens `Channel.writable` will return
/// `true` again. These writability changes are also propagated through the `ChannelPipeline` and so can be intercepted via `ChannelInboundHandler.channelWritabilityChanged`.
public struct WriteBufferWaterMarkOption: ChannelOption {
public typealias Value = WriteBufferWaterMark
public init() {}
}
/// `ConnectTimeoutOption` allows users to configure the `TimeAmount` after which a connect will fail if it was not established in the meantime. May be
/// `nil`, in which case the connection attempt will never time out.
public struct ConnectTimeoutOption: ChannelOption {
public typealias Value = TimeAmount?
public init() {}
}
/// `AllowRemoteHalfClosureOption` allows users to configure whether the `Channel` will close itself when its remote
/// peer shuts down its send stream, or whether it will remain open. If set to `false` (the default), the `Channel`
/// will be closed automatically if the remote peer shuts down its send stream. If set to true, the `Channel` will
/// not be closed: instead, a `ChannelEvent.inboundClosed` user event will be sent on the `ChannelPipeline`,
/// and no more data will be received.
public struct AllowRemoteHalfClosureOption: ChannelOption {
public typealias Value = Bool
public init() {}
}
/// Provides `ChannelOption`s to be used with a `Channel`, `Bootstrap` or `ServerBootstrap`.
public struct ChannelOptions {
/// - seealso: `SocketOption`.
public static let socket = { (level: SocketOptionLevel, name: SocketOptionName) -> SocketOption in
.init(level: level, name: name)
}
/// - seealso: `AllocatorOption`.
public static let allocator = AllocatorOption()
/// - seealso: `RecvAllocatorOption`.
public static let recvAllocator = RecvAllocatorOption()
/// - seealso: `AutoReadOption`.
public static let autoRead = AutoReadOption()
/// - seealso: `MaxMessagesPerReadOption`.
public static let maxMessagesPerRead = MaxMessagesPerReadOption()
/// - seealso: `BacklogOption`.
public static let backlog = BacklogOption()
/// - seealso: `WriteSpinOption`.
public static let writeSpin = WriteSpinOption()
/// - seealso: `WriteBufferWaterMarkOption`.
public static let writeBufferWaterMark = WriteBufferWaterMarkOption()
/// - seealso: `ConnectTimeoutOption`.
public static let connectTimeout = ConnectTimeoutOption()
/// - seealso: `AllowRemoteHalfClosureOption`.
public static let allowRemoteHalfClosure = AllowRemoteHalfClosureOption()
}