mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
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.
82 lines
2.8 KiB
Swift
82 lines
2.8 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 data structure for processing addressed datagrams, such as those used by UDP.
|
|
///
|
|
/// The AddressedEnvelope is used extensively on `DatagramChannel`s in order to keep track
|
|
/// of source or destination address metadata: that is, where some data came from or where
|
|
/// it is going.
|
|
public struct AddressedEnvelope<DataType> {
|
|
public var remoteAddress: SocketAddress
|
|
public var data: DataType
|
|
/// Any metadata associated with this `AddressedEnvelope`
|
|
public var metadata: Metadata? = nil
|
|
|
|
public init(remoteAddress: SocketAddress, data: DataType) {
|
|
self.remoteAddress = remoteAddress
|
|
self.data = data
|
|
}
|
|
|
|
public init(remoteAddress: SocketAddress, data: DataType, metadata: Metadata?) {
|
|
self.remoteAddress = remoteAddress
|
|
self.data = data
|
|
self.metadata = metadata
|
|
}
|
|
|
|
/// Any metadata associated with an `AddressedEnvelope`
|
|
public struct Metadata: Hashable {
|
|
/// Details of any congestion state.
|
|
public var ecnState: NIOExplicitCongestionNotificationState
|
|
public var packetInfo: NIOPacketInfo?
|
|
|
|
public init(ecnState: NIOExplicitCongestionNotificationState) {
|
|
self.ecnState = ecnState
|
|
self.packetInfo = nil
|
|
}
|
|
|
|
public init(ecnState: NIOExplicitCongestionNotificationState, packetInfo: NIOPacketInfo?) {
|
|
self.ecnState = ecnState
|
|
self.packetInfo = packetInfo
|
|
}
|
|
}
|
|
}
|
|
|
|
extension AddressedEnvelope: CustomStringConvertible {
|
|
public var description: String {
|
|
return "AddressedEnvelope { remoteAddress: \(self.remoteAddress), data: \(self.data) }"
|
|
}
|
|
}
|
|
|
|
/// Possible Explicit Congestion Notification States
|
|
public enum NIOExplicitCongestionNotificationState: Hashable {
|
|
/// Non-ECN Capable Transport.
|
|
case transportNotCapable
|
|
/// ECN Capable Transport (flag 0).
|
|
case transportCapableFlag0
|
|
/// ECN Capable Transport (flag 1).
|
|
case transportCapableFlag1
|
|
/// Congestion Experienced.
|
|
case congestionExperienced
|
|
}
|
|
|
|
public struct NIOPacketInfo: Hashable {
|
|
public var destinationAddress: SocketAddress
|
|
public var interfaceIndex: Int
|
|
|
|
public init(destinationAddress: SocketAddress, interfaceIndex: Int) {
|
|
self.destinationAddress = destinationAddress
|
|
self.interfaceIndex = interfaceIndex
|
|
}
|
|
}
|