Compare commits

...

12 Commits

Author SHA1 Message Date
Gus Cairo d597722cca Replace sleep in tests with waiter channel handler 2025-04-14 14:28:52 +01:00
Gus Cairo c90c6432fd Add tests 2025-04-14 14:28:52 +01:00
Gus Cairo 08a31ceb2f Call param configurator when wrapping an existing NWConnection 2025-04-14 14:28:52 +01:00
Gus Cairo 9e6e4ed7ab Turn vars into lets 2025-04-14 14:28:52 +01:00
Gus Cairo 7a8dcde582 Add tests 2025-04-14 14:28:52 +01:00
Gus Cairo b37cc8bd40 Add missing call to configurator to NIOTSManagedListenerChannel 2025-04-14 14:28:52 +01:00
Gus Cairo d61bf6d098 Add missing configurator method to the NIOTSListenerBootstrap 2025-04-14 14:28:52 +01:00
Gus Cairo f129546483 Add missed initialization of the configurator to StateManagedListenerChannel 2025-04-14 14:28:52 +01:00
Gus Cairo fca19c5367 Undo NWParameters as inout in configurator closure
NWParameters is a class, so we don't need it to be inout when passing it to the configurator closure
2025-04-14 14:28:52 +01:00
Gus Cairo 1f354a871f Add NWParameters configurator to bootstraps 2025-04-14 14:28:52 +01:00
Rick Newton-Rogers 3d21b85af4
Enable Swift 6.1 jobs in CI (#232)
Motivation:

Swift 6.1 has been released, we should add it to our CI coverage.

Modifications:

Add additional Swift 6.1 jobs where appropriate in main.yml,
pull_request.yml

Result:

Improved test coverage.
2025-04-14 10:52:43 +01:00
Gus Cairo 9eb2ebde13
Fix missing strict concurrency error (#231)
Current strict concurrency checks don't work consistently in Xcode. This
PR adds an additional flag (`-Xfrontend`) to our set of strict
concurrency flags to make sure nothing has been missed when building. It
also fixes an additional error uncovered after adding it.
2025-04-09 15:08:12 +01:00
17 changed files with 260 additions and 37 deletions

View File

@ -14,6 +14,7 @@ jobs:
linux_5_9_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error"
linux_5_10_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_6_1_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"

View File

@ -17,6 +17,7 @@ jobs:
linux_5_9_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error"
linux_5_10_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_6_1_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"

View File

@ -27,7 +27,7 @@ let strictConcurrencySettings: [SwiftSetting] = {
if strictConcurrencyDevelopment {
// -warnings-as-errors here is a workaround so that IDE-based development can
// get tripped up on -require-explicit-sendable.
initialSettings.append(.unsafeFlags(["-require-explicit-sendable", "-warnings-as-errors"]))
initialSettings.append(.unsafeFlags(["-Xfrontend", "-require-explicit-sendable", "-warnings-as-errors"]))
}
return initialSettings

View File

@ -47,6 +47,7 @@ public final class NIOTSDatagramBootstrap {
private var qos: DispatchQoS?
private var udpOptions: NWProtocolUDP.Options = .init()
private var tlsOptions: NWProtocolTLS.Options?
private var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
/// Create a `NIOTSDatagramConnectionBootstrap` on the `EventLoopGroup` `group`.
///
@ -133,6 +134,14 @@ public final class NIOTSDatagramBootstrap {
return self
}
/// Customise the `NWParameters` to be used when creating the connection.
public func configureNWParameters(
_ configurator: @Sendable @escaping (NWParameters) -> Void
) -> Self {
self.nwParametersConfigurator = configurator
return self
}
/// Specify the `host` and `port` to connect to for the UDP `Channel` that will be established.
///
/// - parameters:
@ -188,7 +197,8 @@ public final class NIOTSDatagramBootstrap {
eventLoop: self.group.next() as! NIOTSEventLoop,
qos: self.qos,
udpOptions: self.udpOptions,
tlsOptions: self.tlsOptions
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator
)
let initializer = self.channelInitializer ?? { @Sendable _ in conn.eventLoop.makeSucceededFuture(()) }

View File

@ -140,8 +140,12 @@ internal final class NIOTSDatagramChannel: StateManagedNWConnectionChannel {
internal var allowLocalEndpointReuse = false
internal var multipathServiceType: NWParameters.MultipathServiceType = .disabled
internal let nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
var parameters: NWParameters {
NWParameters(dtls: self.tlsOptions, udp: self.udpOptions)
let parameters = NWParameters(dtls: self.tlsOptions, udp: self.udpOptions)
self.nwParametersConfigurator?(parameters)
return parameters
}
var _inboundStreamOpen: Bool {
@ -182,7 +186,8 @@ internal final class NIOTSDatagramChannel: StateManagedNWConnectionChannel {
minimumIncompleteReceiveLength: Int = 1,
maximumReceiveLength: Int = 8192,
udpOptions: NWProtocolUDP.Options,
tlsOptions: NWProtocolTLS.Options?
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.tsEventLoop = eventLoop
self.closePromise = eventLoop.makePromise()
@ -192,6 +197,7 @@ internal final class NIOTSDatagramChannel: StateManagedNWConnectionChannel {
self.connectionQueue = eventLoop.channelQueue(label: "nio.nioTransportServices.connectionchannel", qos: qos)
self.udpOptions = udpOptions
self.tlsOptions = tlsOptions
self.nwParametersConfigurator = nwParametersConfigurator
// Must come last, as it requires self to be completely initialized.
self._pipeline = ChannelPipeline(channel: self)
@ -206,7 +212,8 @@ internal final class NIOTSDatagramChannel: StateManagedNWConnectionChannel {
minimumIncompleteReceiveLength: Int = 1,
maximumReceiveLength: Int = 8192,
udpOptions: NWProtocolUDP.Options,
tlsOptions: NWProtocolTLS.Options?
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
eventLoop: eventLoop,
@ -215,7 +222,8 @@ internal final class NIOTSDatagramChannel: StateManagedNWConnectionChannel {
minimumIncompleteReceiveLength: minimumIncompleteReceiveLength,
maximumReceiveLength: maximumReceiveLength,
udpOptions: udpOptions,
tlsOptions: tlsOptions
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator
)
self.connection = connection
}

View File

@ -66,6 +66,7 @@ public final class NIOTSDatagramListenerBootstrap {
private var udpOptions: NWProtocolUDP.Options = .init()
private var tlsOptions: NWProtocolTLS.Options?
private var bindTimeout: TimeAmount?
private var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
/// Create a ``NIOTSListenerBootstrap`` for the `EventLoopGroup` `group`.
///
@ -236,6 +237,14 @@ public final class NIOTSDatagramListenerBootstrap {
return self
}
/// Customise the `NWParameters` to be used when creating the connection.
public func configureNWParameters(
_ configurator: @Sendable @escaping (NWParameters) -> Void
) -> Self {
self.nwParametersConfigurator = configurator
return self
}
/// Bind the `NIOTSListenerChannel` to `host` and `port`.
///
/// - parameters:
@ -327,10 +336,12 @@ public final class NIOTSDatagramListenerBootstrap {
qos: self.serverQoS,
udpOptions: self.udpOptions,
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator,
childLoopGroup: self.childGroup,
childChannelQoS: self.childQoS,
childUDPOptions: self.udpOptions,
childTLSOptions: self.tlsOptions
childTLSOptions: self.tlsOptions,
childNWParametersConfigurator: self.nwParametersConfigurator
)
} else {
serverChannel = NIOTSDatagramListenerChannel(
@ -338,10 +349,12 @@ public final class NIOTSDatagramListenerBootstrap {
qos: self.serverQoS,
udpOptions: self.udpOptions,
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator,
childLoopGroup: self.childGroup,
childChannelQoS: self.childQoS,
childUDPOptions: self.udpOptions,
childTLSOptions: self.tlsOptions
childTLSOptions: self.tlsOptions,
childNWParametersConfigurator: self.nwParametersConfigurator
)
}

View File

@ -81,19 +81,23 @@ internal final class NIOTSDatagramListenerChannel: StateManagedListenerChannel<N
qos: DispatchQoS? = nil,
udpOptions: NWProtocolUDP.Options,
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?,
childLoopGroup: EventLoopGroup,
childChannelQoS: DispatchQoS?,
childUDPOptions: NWProtocolUDP.Options,
childTLSOptions: NWProtocolTLS.Options?
childTLSOptions: NWProtocolTLS.Options?,
childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
eventLoop: eventLoop,
protocolOptions: .udp(udpOptions),
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator,
childLoopGroup: childLoopGroup,
childChannelQoS: childChannelQoS,
childProtocolOptions: .udp(childUDPOptions),
childTLSOptions: childTLSOptions
childTLSOptions: childTLSOptions,
childNWParametersConfigurator: childNWParametersConfigurator
)
}
@ -104,20 +108,24 @@ internal final class NIOTSDatagramListenerChannel: StateManagedListenerChannel<N
qos: DispatchQoS? = nil,
udpOptions: NWProtocolUDP.Options,
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?,
childLoopGroup: EventLoopGroup,
childChannelQoS: DispatchQoS?,
childUDPOptions: NWProtocolUDP.Options,
childTLSOptions: NWProtocolTLS.Options?
childTLSOptions: NWProtocolTLS.Options?,
childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
wrapping: listener,
eventLoop: eventLoop,
protocolOptions: .udp(udpOptions),
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator,
childLoopGroup: childLoopGroup,
childChannelQoS: childChannelQoS,
childProtocolOptions: .udp(childUDPOptions),
childTLSOptions: childTLSOptions
childTLSOptions: childTLSOptions,
childNWParametersConfigurator: childNWParametersConfigurator
)
}
@ -132,7 +140,8 @@ internal final class NIOTSDatagramListenerChannel: StateManagedListenerChannel<N
on: self.childLoopGroup.next() as! NIOTSEventLoop,
parent: self,
udpOptions: self.childUDPOptions,
tlsOptions: self.childTLSOptions
tlsOptions: self.childTLSOptions,
nwParametersConfigurator: self.childNWParametersConfigurator
)
self.pipeline.fireChannelRead(newChannel)

View File

@ -62,6 +62,7 @@ public final class NIOTSConnectionBootstrap {
private var tcpOptions: NWProtocolTCP.Options = .init()
private var tlsOptions: NWProtocolTLS.Options?
private var protocolHandlers: (@Sendable () -> [ChannelHandler])? = nil
private var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
/// Create a `NIOTSConnectionBootstrap` on the `EventLoopGroup` `group`.
///
@ -165,6 +166,14 @@ public final class NIOTSConnectionBootstrap {
self.channelOption(NIOTSChannelOptions.multipathServiceType, value: type)
}
/// Customise the `NWParameters` to be used when creating the connection.
public func configureNWParameters(
_ configurator: @Sendable @escaping (NWParameters) -> Void
) -> Self {
self.nwParametersConfigurator = configurator
return self
}
/// Specify the `host` and `port` to connect to for the TCP `Channel` that will be established.
///
/// - parameters:
@ -243,14 +252,16 @@ public final class NIOTSConnectionBootstrap {
wrapping: newConnection,
on: self.group.next() as! NIOTSEventLoop,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator
)
} else {
conn = NIOTSConnectionChannel(
eventLoop: self.group.next() as! NIOTSEventLoop,
qos: self.qos,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator
)
}
let initializer = self.channelInitializer
@ -437,14 +448,16 @@ extension NIOTSConnectionBootstrap {
wrapping: newConnection,
on: self.group.next() as! NIOTSEventLoop,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator
)
} else {
connectionChannel = NIOTSConnectionChannel(
eventLoop: self.group.next() as! NIOTSEventLoop,
qos: self.qos,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator
)
}
let initializer = self.channelInitializer

View File

@ -164,8 +164,12 @@ internal final class NIOTSConnectionChannel: StateManagedNWConnectionChannel {
/// An `EventLoopPromise` that will be succeeded or failed when a connection attempt succeeds or fails.
internal var connectPromise: EventLoopPromise<Void>?
internal let nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
internal var parameters: NWParameters {
NWParameters(tls: self.tlsOptions, tcp: self.tcpOptions)
let parameters = NWParameters(tls: self.tlsOptions, tcp: self.tcpOptions)
self.nwParametersConfigurator?(parameters)
return parameters
}
/// The TCP options for this connection.
@ -242,7 +246,8 @@ internal final class NIOTSConnectionChannel: StateManagedNWConnectionChannel {
minimumIncompleteReceiveLength: Int = 1,
maximumReceiveLength: Int = 8192,
tcpOptions: NWProtocolTCP.Options,
tlsOptions: NWProtocolTLS.Options?
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.tsEventLoop = eventLoop
self.closePromise = eventLoop.makePromise()
@ -252,6 +257,7 @@ internal final class NIOTSConnectionChannel: StateManagedNWConnectionChannel {
self.connectionQueue = eventLoop.channelQueue(label: "nio.nioTransportServices.connectionchannel", qos: qos)
self.tcpOptions = tcpOptions
self.tlsOptions = tlsOptions
self.nwParametersConfigurator = nwParametersConfigurator
// Must come last, as it requires self to be completely initialized.
self._pipeline = ChannelPipeline(channel: self)
@ -266,7 +272,8 @@ internal final class NIOTSConnectionChannel: StateManagedNWConnectionChannel {
minimumIncompleteReceiveLength: Int = 1,
maximumReceiveLength: Int = 8192,
tcpOptions: NWProtocolTCP.Options,
tlsOptions: NWProtocolTLS.Options?
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
eventLoop: eventLoop,
@ -275,7 +282,8 @@ internal final class NIOTSConnectionChannel: StateManagedNWConnectionChannel {
minimumIncompleteReceiveLength: minimumIncompleteReceiveLength,
maximumReceiveLength: maximumReceiveLength,
tcpOptions: tcpOptions,
tlsOptions: tlsOptions
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator
)
self.connection = connection
}

View File

@ -21,7 +21,7 @@ import NIOCore
public protocol NIOTSError: Error, Equatable {}
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public enum NIOTSErrors {
public enum NIOTSErrors: Sendable {
/// ``InvalidChannelStateTransition`` is thrown when a channel has been asked to do something
/// that is incompatible with its current channel state: e.g. attempting to register an
/// already registered channel.

View File

@ -66,6 +66,7 @@ public final class NIOTSListenerBootstrap {
private var tcpOptions: NWProtocolTCP.Options = .init()
private var tlsOptions: NWProtocolTLS.Options?
private var bindTimeout: TimeAmount?
private var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
/// Create a ``NIOTSListenerBootstrap`` for the `EventLoopGroup` `group`.
///
@ -239,6 +240,14 @@ public final class NIOTSListenerBootstrap {
return self
}
/// Customise the `NWParameters` to be used when creating the connection.
public func configureNWParameters(
_ configurator: @Sendable @escaping (NWParameters) -> Void
) -> Self {
self.nwParametersConfigurator = configurator
return self
}
/// Specifies a type of Multipath service to use for this listener, instead of the default
/// service type for the event loop.
///
@ -337,10 +346,12 @@ public final class NIOTSListenerBootstrap {
qos: self.serverQoS,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator,
childLoopGroup: self.childGroup,
childChannelQoS: self.childQoS,
childTCPOptions: self.tcpOptions,
childTLSOptions: self.tlsOptions
childTLSOptions: self.tlsOptions,
childNWParametersConfigurator: self.nwParametersConfigurator
)
} else {
serverChannel = NIOTSListenerChannel(
@ -348,10 +359,12 @@ public final class NIOTSListenerBootstrap {
qos: self.serverQoS,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator,
childLoopGroup: self.childGroup,
childChannelQoS: self.childQoS,
childTCPOptions: self.tcpOptions,
childTLSOptions: self.tlsOptions
childTLSOptions: self.tlsOptions,
childNWParametersConfigurator: self.nwParametersConfigurator
)
}
@ -558,10 +571,12 @@ extension NIOTSListenerBootstrap {
qos: self.serverQoS,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator,
childLoopGroup: self.childGroup,
childChannelQoS: self.childQoS,
childTCPOptions: self.tcpOptions,
childTLSOptions: self.tlsOptions
childTLSOptions: self.tlsOptions,
childNWParametersConfigurator: self.nwParametersConfigurator
)
} else {
serverChannel = NIOTSListenerChannel(
@ -569,10 +584,12 @@ extension NIOTSListenerBootstrap {
qos: self.serverQoS,
tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions,
nwParametersConfigurator: self.nwParametersConfigurator,
childLoopGroup: self.childGroup,
childChannelQoS: self.childQoS,
childTCPOptions: self.tcpOptions,
childTLSOptions: self.tlsOptions
childTLSOptions: self.tlsOptions,
childNWParametersConfigurator: self.nwParametersConfigurator
)
}

View File

@ -81,19 +81,23 @@ internal final class NIOTSListenerChannel: StateManagedListenerChannel<NIOTSConn
qos: DispatchQoS? = nil,
tcpOptions: NWProtocolTCP.Options,
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?,
childLoopGroup: EventLoopGroup,
childChannelQoS: DispatchQoS?,
childTCPOptions: NWProtocolTCP.Options,
childTLSOptions: NWProtocolTLS.Options?
childTLSOptions: NWProtocolTLS.Options?,
childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
eventLoop: eventLoop,
protocolOptions: .tcp(tcpOptions),
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator,
childLoopGroup: childLoopGroup,
childChannelQoS: childChannelQoS,
childProtocolOptions: .tcp(childTCPOptions),
childTLSOptions: childTLSOptions
childTLSOptions: childTLSOptions,
childNWParametersConfigurator: childNWParametersConfigurator
)
}
@ -104,10 +108,12 @@ internal final class NIOTSListenerChannel: StateManagedListenerChannel<NIOTSConn
qos: DispatchQoS? = nil,
tcpOptions: NWProtocolTCP.Options,
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?,
childLoopGroup: EventLoopGroup,
childChannelQoS: DispatchQoS?,
childTCPOptions: NWProtocolTCP.Options,
childTLSOptions: NWProtocolTLS.Options?
childTLSOptions: NWProtocolTLS.Options?,
childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
wrapping: listener,
@ -115,10 +121,12 @@ internal final class NIOTSListenerChannel: StateManagedListenerChannel<NIOTSConn
qos: qos,
protocolOptions: .tcp(tcpOptions),
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator,
childLoopGroup: childLoopGroup,
childChannelQoS: childChannelQoS,
childProtocolOptions: .tcp(childTCPOptions),
childTLSOptions: childTLSOptions
childTLSOptions: childTLSOptions,
childNWParametersConfigurator: childNWParametersConfigurator
)
}
@ -134,7 +142,8 @@ internal final class NIOTSListenerChannel: StateManagedListenerChannel<NIOTSConn
parent: self,
qos: self.childChannelQoS,
tcpOptions: self.childTCPOptions,
tlsOptions: self.childTLSOptions
tlsOptions: self.childTLSOptions,
nwParametersConfigurator: self.childNWParametersConfigurator
)
self.pipeline.fireChannelRead(newChannel)

View File

@ -67,6 +67,9 @@ internal class StateManagedListenerChannel<ChildChannel: StateManagedChannel>: S
/// The TLS options for this listener.
internal let tlsOptions: NWProtocolTLS.Options?
/// A customization point for this listener's `NWParameters`.
internal let nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?
/// The `DispatchQueue` that socket events for this connection will be dispatched onto.
internal let connectionQueue: DispatchQueue
@ -113,6 +116,9 @@ internal class StateManagedListenerChannel<ChildChannel: StateManagedChannel>: S
/// The TLS options to use for child channels.
internal let childTLSOptions: NWProtocolTLS.Options?
/// A customization point for each child's `NWParameters`.
internal let childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
/// The cache of the local and remote socket addresses. Must be accessed using _addressCacheLock.
internal var addressCache = AddressCache(local: nil, remote: nil)
@ -130,20 +136,24 @@ internal class StateManagedListenerChannel<ChildChannel: StateManagedChannel>: S
qos: DispatchQoS? = nil,
protocolOptions: ProtocolOptions,
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?,
childLoopGroup: EventLoopGroup,
childChannelQoS: DispatchQoS?,
childProtocolOptions: ProtocolOptions,
childTLSOptions: NWProtocolTLS.Options?
childTLSOptions: NWProtocolTLS.Options?,
childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.tsEventLoop = eventLoop
self.closePromise = eventLoop.makePromise()
self.connectionQueue = eventLoop.channelQueue(label: "nio.transportservices.listenerchannel", qos: qos)
self.protocolOptions = protocolOptions
self.tlsOptions = tlsOptions
self.nwParametersConfigurator = nwParametersConfigurator
self.childLoopGroup = childLoopGroup
self.childChannelQoS = childChannelQoS
self.childProtocolOptions = childProtocolOptions
self.childTLSOptions = childTLSOptions
self.childNWParametersConfigurator = childNWParametersConfigurator
// Must come last, as it requires self to be completely initialized.
self._pipeline = ChannelPipeline(channel: self)
@ -155,20 +165,24 @@ internal class StateManagedListenerChannel<ChildChannel: StateManagedChannel>: S
qos: DispatchQoS? = nil,
protocolOptions: ProtocolOptions,
tlsOptions: NWProtocolTLS.Options?,
nwParametersConfigurator: (@Sendable (NWParameters) -> Void)?,
childLoopGroup: EventLoopGroup,
childChannelQoS: DispatchQoS?,
childProtocolOptions: ProtocolOptions,
childTLSOptions: NWProtocolTLS.Options?
childTLSOptions: NWProtocolTLS.Options?,
childNWParametersConfigurator: (@Sendable (NWParameters) -> Void)?
) {
self.init(
eventLoop: eventLoop,
qos: qos,
protocolOptions: protocolOptions,
tlsOptions: tlsOptions,
nwParametersConfigurator: nwParametersConfigurator,
childLoopGroup: childLoopGroup,
childChannelQoS: childChannelQoS,
childProtocolOptions: childProtocolOptions,
childTLSOptions: childTLSOptions
childTLSOptions: childTLSOptions,
childNWParametersConfigurator: childNWParametersConfigurator
)
self.nwListener = listener
}
@ -398,6 +412,8 @@ extension StateManagedListenerChannel {
parameters.multipathServiceType = self.multipathServiceType
self.nwParametersConfigurator?(parameters)
let listener: NWListener
do {
listener = try NWListener(using: parameters)

View File

@ -83,6 +83,8 @@ internal protocol StateManagedNWConnectionChannel: StateManagedChannel where Act
var multipathServiceType: NWParameters.MultipathServiceType { get }
var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)? { get }
func setChannelSpecificOption0<Option: ChannelOption>(option: Option, value: Option.Value) throws
func getChannelSpecificOption0<Option: ChannelOption>(option: Option) throws -> Option.Value
@ -242,6 +244,7 @@ extension StateManagedNWConnectionChannel {
connection.betterPathUpdateHandler = self.betterPathHandler
connection.viabilityUpdateHandler = self.viabilityUpdateHandler
connection.pathUpdateHandler = self.pathChangedHandler(newPath:)
self.nwParametersConfigurator?(connection.parameters)
connection.start(queue: self.connectionQueue)
}

View File

@ -18,7 +18,7 @@ import XCTest
import Network
import NIOCore
import NIOEmbedded
import NIOTransportServices
@testable import NIOTransportServices
import NIOConcurrencyHelpers
import Foundation
@ -371,6 +371,60 @@ final class NIOTSBootstrapTests: XCTestCase {
XCTAssertEqual(try listenerChannel.getOption(NIOTSChannelOptions.multipathServiceType).wait(), .handover)
XCTAssertEqual(try connectionChannel.getOption(NIOTSChannelOptions.multipathServiceType).wait(), .handover)
}
func testNWParametersConfigurator() async throws {
final class WaitForConnectionHandler: ChannelInboundHandler, Sendable {
typealias InboundIn = Never
let connectionPromise: EventLoopPromise<Void>
init(connectionPromise: EventLoopPromise<Void>) {
self.connectionPromise = connectionPromise
}
func channelActive(context: ChannelHandlerContext) {
self.connectionPromise.succeed()
}
}
let group = NIOTSEventLoopGroup(loopCount: 1)
let configuratorListenerCounter = NIOLockedValueBox(0)
let configuratorConnectionCounter = NIOLockedValueBox(0)
let waitForConnectionHandler = WaitForConnectionHandler(
connectionPromise: group.next().makePromise()
)
let listenerChannel = try await NIOTSListenerBootstrap(group: group)
.childChannelInitializer { connectionChannel in
connectionChannel.eventLoop.makeCompletedFuture {
try connectionChannel.pipeline.syncOperations.addHandler(waitForConnectionHandler)
}
}
.configureNWParameters { _ in
configuratorListenerCounter.withLockedValue { $0 += 1 }
}
.bind(host: "localhost", port: 0)
.get()
let connectionChannel: Channel = try await NIOTSConnectionBootstrap(group: group)
.configureNWParameters { _ in
configuratorConnectionCounter.withLockedValue { $0 += 1 }
}
.connect(to: listenerChannel.localAddress!)
.get()
// Wait for the server to activate the connection channel to the client.
try await waitForConnectionHandler.connectionPromise.futureResult.get()
try await listenerChannel.close().get()
try await connectionChannel.close().get()
XCTAssertEqual(2, configuratorListenerCounter.withLockedValue { $0 })
XCTAssertEqual(1, configuratorConnectionCounter.withLockedValue { $0 })
try await group.shutdownGracefully()
}
}
extension Channel {

View File

@ -37,13 +37,14 @@ final class NIOTSChannelMetadataTests: XCTestCase {
}.wait()
}
func testThowsIfCalledOnANonInitializedChannel() {
func testThrowsIfCalledOnANonInitializedChannel() {
let eventLoopGroup = NIOTSEventLoopGroup()
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let channel = NIOTSConnectionChannel(
eventLoop: eventLoopGroup.next() as! NIOTSEventLoop,
tcpOptions: .init(),
tlsOptions: .init()
tlsOptions: .init(),
nwParametersConfigurator: nil
)
XCTAssertThrowsError(try channel.getMetadata(definition: NWProtocolTLS.definition).wait()) { error in
XCTAssertTrue(error is NIOTSConnectionNotInitialized, "unexpected error \(error)")

View File

@ -18,6 +18,7 @@ import Network
import NIOCore
import NIOTransportServices
import Foundation
import NIOConcurrencyHelpers
extension Channel {
func wait<T: Sendable>(for type: T.Type, count: Int) throws -> [T] {
@ -232,6 +233,65 @@ final class NIOTSDatagramConnectionChannelTests: XCTestCase {
XCTAssertNoThrow(try connection.close().wait())
}
func testNWParametersConfigurator() async throws {
final class WaitForConnectionHandler: ChannelInboundHandler, Sendable {
typealias InboundIn = Never
let connectionPromise: EventLoopPromise<Void>
init(connectionPromise: EventLoopPromise<Void>) {
self.connectionPromise = connectionPromise
}
func channelActive(context: ChannelHandlerContext) {
self.connectionPromise.succeed()
}
}
let group = NIOTSEventLoopGroup(loopCount: 1)
let configuratorListenerCounter = NIOLockedValueBox(0)
let configuratorConnectionCounter = NIOLockedValueBox(0)
let waitForConnectionHandler = WaitForConnectionHandler(
connectionPromise: group.next().makePromise()
)
let listenerChannel = try await NIOTSDatagramListenerBootstrap(group: group)
.childChannelInitializer { connectionChannel in
connectionChannel.eventLoop.makeCompletedFuture {
try connectionChannel.pipeline.syncOperations.addHandler(waitForConnectionHandler)
}
}
.configureNWParameters { _ in
configuratorListenerCounter.withLockedValue { $0 += 1 }
}
.bind(host: "localhost", port: 0)
.get()
let connectionChannel: Channel = try await NIOTSDatagramBootstrap(group: group)
.configureNWParameters { _ in
configuratorConnectionCounter.withLockedValue { $0 += 1 }
}
.connect(to: listenerChannel.localAddress!)
.get()
// Need to write something so the server can activate the connection channel: this is UDP,
// so there is no handshaking that happens and thus the server cannot know that the
// connection has been established and the channel can be activated.
try await connectionChannel.writeAndFlush(ByteBuffer(bytes: [42]))
// Wait for the server to activate the connection channel to the client.
try await waitForConnectionHandler.connectionPromise.futureResult.get()
try await listenerChannel.close().get()
try await connectionChannel.close().get()
XCTAssertEqual(2, configuratorListenerCounter.withLockedValue { $0 })
XCTAssertEqual(1, configuratorConnectionCounter.withLockedValue { $0 })
try await group.shutdownGracefully()
}
func testCanExtractTheConnection() throws {
guard #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) else {
throw XCTSkip("Option not available")