Commit Graph

103 Commits

Author SHA1 Message Date
Rafael Cepeda 9224dc5159
Added buffer pool to NIOTSConnectionChannel (#236)
Added buffer pool to NIOTSConnectionChannel for reading messages.

### Motivation:

In order to avoid creating a new `ByteBuffer` for every single message
read from the channel, we decided to reuse the
`NIOPooledRecvBufferAllocator` type from NIOCore to leverage a pool of
buffers.

### Modifications:

Used the buffer allocation mechanism provided by the
`NIOPooledRecvBufferAllocator` to reuse and adapt previously created
receive buffers.

### Result:

Channel reads can now reuse previously created buffers, avoiding
unnecessary overhead by creating new buffers every single time.
2025-05-13 18:44:21 +01:00
Gus Cairo d8443227d1
Change naming of datagram-related types to make them more consistent + fix docs (#233)
### Motivation:

The datagram-related types logically mirror the TCP-based ones
(`NIOTSDatagramListenerBootstrap` vs `NIOTSListenerBootstrap`;
`NIOTSListenerChannel` vs `NIOTSDatagramListenerChannel`;
`NIOTSDatagramChannel` vs `NIOTSDatagramConnectionChannel`;
`NIOTSDatagramBootstrap` vs `NIOTSConnectionBootstrap`).
However, some of the type names could more closely resemble their TCP
counterparts to make it easier to navigate the code/understand what
their purpose is.
Additionally, the docs for some of these types are wrong, as they've
been copy-pasted into the datagram versions without changes.

### Modifications:

There are separate commits for each of the following changes:
- Fix docs for `NIOTSDatagramListenerBootstrap` and rename the file to
match the type name.
- Rename `NIOTSDatagramConnectionChannelTests` to
`NIOTSDatagramBootstrapTests` (to match `NIOTSBootstrapTests`).
- Rename `NIOTSDatagramChannel` to `NIOTSDatagramConnectionChannel` (to
match `NIOTSConnectionChannel`)
- Fix docs for `NIOTSConnectionBootstrap`.
- Rename `NIOTSDatagramBootstrap` to `NIOTSDatagramConnectionBootstrap`
(to match `NIOTSConnectionBootstrap`). This one required a deprecate and
replace since it's a public type.

### Result:

Better docs and more consistency in our type names.
2025-04-29 13:56:21 +01:00
Gus Cairo cd1e89816d
Add `NWParameters` configurator to bootstraps (#230)
Allow users to provide a closure taking an `NWParameters` to customise
them before they're used to create `NWConnection`s.

### Motivation:

`NWParameters` are currently created using the provided TLS and UDP
options, and then passed over to new `NWConnection`s. However, there are
more ways in which `NWParameters` can be customised, so this new API
provides a way for users to do this.

### Modifications:

Introduce new `configureNWParameters` methods to the existing bootstraps
to allow configuring a closure for customising `NWParameters`.

### Result:

Users can now customise the `NWParameters` used to create new
`NWConnection`s.
2025-04-16 14:33:56 +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
Gus Cairo 92bb536b7e
Strict concurrency for NIOTransportServices and tests (#228) 2025-04-02 10:54:00 +01:00
George Barnett 3c394067c0
Add missing Dispatch import (#223)
Motivation:

Dispatch wasn't imported in `NIOTSSingletons.swift` where `.default` was
used for the default QoS. This causes some build issues.

Modifications:

- Add missing Dispatch import

Result:

Fewer build issues

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-01-31 10:26:33 +00:00
Rick Newton-Rogers c73112efc0
Migrate CI to use GitHub Actions. (#213)
### Motivation:

To migrate to GitHub actions and centralised infrastructure.

### Modifications:

Changes of note:
* Adopt swift-format using rules from SwiftNIO. Modified so that it
ignores `NoAssignmentInExpressions` in some `XCTAssert` functions.
* Remove scripts and docker files which are no longer needed

### Result:

Feature parity with old CI.
2024-10-28 09:51:24 +00:00
Clinton Nkwocha bbd5e63cf9
Provide configurability for receiving connection data (#212)
Motivation:

Users are stuck with the hardcoded parameters for receiving data from a
connection.

Modifications:

- Add new options to `NIOTSChannelOptions` for configuring how to
receive data from a connection.

Result:

Users can configure how they receive data from a connection.
2024-10-17 13:14:45 +01:00
Anthony Doeraene afd169118c
Add configuration of multipathServiceType in NIOTSConnectionBootstrap (#205)
### Motivation:

Allow different devices to leverage the capabilities of Mutipath TCP
(MPTCP) to enhance the network reliability. Thanks to MPTCP, a
connection can for example automatically migrate to another interface if
it deteriorates on the first one. This can be especially interesting on
MacOS X and iOS, where devices may frequently benefit from multiple
interfaces (ethernet + Wi-Fi for Macs and Wi-fi + cellular for iOS).
Allowing developers to enable MPTCP on their connections seems thus like
a fine addition to this library.

### Modifications:

Added a function "withMultipath" on NIOTSConnectionBootstrap, that allow
to configure the type of service used for MPTCP (defaults to disabled).
This value will be stored in a field, and then propagated to the
underlying channel when the connect method will be called. Also updated
the parameters field of NIOTSConnectionChannel to set the
multipathServiceType accordingly.

### Result:

Users will now be able to easily enable Multipath TCP, allowing them to
benefit from seamless handover, interactive mode to automatically use
the lowest delay interface or aggregate mode to send data in parallel on
both interfaces.

Example: 
```swift
let group = NIOTSEventLoopGroup()
defer {
     try! group.syncShutdownGracefully()
}
let bootstrap = NIOTSConnectionBootstrap(group: group)
    .withMultipath(.handover)   // set handover mode
    .channelInitializer { channel in
        channel.pipeline.addHandler(MyChannelHandler())
    }
try! bootstrap.connect(host: "example.org", port: 12345).wait()
/* the Channel is now connected */
```

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-09-18 09:50:40 -07:00
Cartisim Development 99d28e05f7
Added support for connection viability updates (#207)
# Enhancement: Integration of Viability Handler in `Network.framework`

We have successfully integrated the `viabilityUpdateHandler` from
`Network.framework` into `NIOTransportServices`, enabling developers to
leverage the full potential of the `Network.framework` API.

This enhancement provides adopters with critical insights into the
viability of connections to remote endpoints. By being informed about
the current network status, developers can implement a more responsive
and adaptive approach to network behavior and conditions.

## Modifications

- Introduced two new methods along with corresponding calls to these
methods within the `Network.framework` handler.
- These handlers trigger the `NIO` *InboundEventsTriggered* method,
allowing consumers to respond to network changes effectively.
- Developed two structs within *NIOTSNetworkEvents* to accompany the
*InboundEventsTriggered* methods, ensuring seamless data transfer.
- Conformed these structs to Sendability for compatibility with the
required operating systems.

## Result

Clients can now receive viability events from `Network.framework`,
enhancing their ability to manage network connections dynamically

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-09-16 14:11:19 +01:00
Cory Benfield 38ac8221dd
Add ChannelOptions to extract base types. (#203)
Motivation:

In some cases users may want to access APIs we haven't exposed
in NIOTransportServices. We should have a fallback that allows users
to do this.

Modifications:

- Add ChannelOptions for getting NWConnection and NWListener.

Result:

Users have an escape hatch
2024-05-13 18:30:13 +01:00
Cory Benfield 715e3179d3
Fix the syncOptions on most channels (#202)
Motivation:

Looks like when we previously added syncOptions support to our
channels, we had a few issues. The listeners had code added, but
the code never worked. This is because the code was defined in
subclasses, but the protocol conformance comes from the parent class,
and that parent class did not have a customized protocol witness.

The datagram channel was also entirely missing its support.

Modifications:

- Added the missing unit tests for sync options.
- Added syncOptions to StateManagedListenerChannel base class.
- Added overrides to the listener subclasses.
- Added syncOptions to datagram channel

Result:

Sync options actually work across the board.
2024-05-13 16:12:01 +01:00
George Barnett 4a0fad7658
Raise minimum Swift version to 5.8 (#199) 2024-03-11 09:32:56 +00:00
Franz Busch 8bf5a6b65f
Fix availability guards and compiler warnings (#195)
# Motivation
We missed a few availability guards in our tests which caused errors when compiling for older Darwin platforms.

# Modification
This PR adds the missing guards.

# Result
We should build on all supported platforms again
2023-12-08 13:02:43 +00:00
Franz Busch ebf8b9c365
Add new typed async bootstrap APIs back and drop SPI (#191)
* Revert "Back out SPI(AsyncChannel) changes"

This reverts commit 33d2b2993f.

* Add new typed async bootstrap APIs back and drop SPI

# Motivation
We just merged the removal of the `AsyncChannel` SPI in NIO and can now add back the new APIs in transport services as well.

# Modification
This PR brings back the previous SPI and promotes it to API.

# Result
New typed async bootstraps API for `NIOTransportServices`.

* George review
2023-10-25 14:09:26 +01:00
Cartisim Development 16ca413e3f
Fire the pipelines error caught method when NWConnection's state changes to failed (#187)
* Fire the pipelines  error caughted method when NWConnection's state changes to failed

* Added check if failed error is ChannelError.eof and added a unit test for forwarding failed connnection state errors

* Completing a promise for the error caught

* Fixed Typo

* Removed whitespace

* Binding the listener to port 0 and connecting to listerner's localAddress
2023-10-25 09:22:53 +01:00
Rick Newton-Rogers 0561bee80c
Bump minimum Swift version to 5.7 (#189)
Motivation:

Now that Swift 5.9 is GM we should update the supported versions and
remove 5.6

Modifications:

* Update `Package.swift`
* Delete the 5.6 docker compose file and make a 5.10 one
* Update docs

Result:

Remove support for Swift 5.6, add 5.10
2023-10-04 10:18:36 +01:00
Rayman Rosevear e4f1815b6a
Fix a typo when casting the allowLocalEndpointReuse channel option value (#186)
Motivation:

There was a typo when setting the allowLocalEndpointReuse channel option, where it was cast to NIOTSEnablePeerToPeerOption.Value
instead of NIOTSAllowLocalEndpointReuse.Value. Both of these types are Bools, so this wouldn't cause any actual issues, this change
is to more to keep the code consistent, and reduce confusion for future contributors or others reading the code base.

Modifications:

This commit casts the allowLocalEndpointReuse property in StateManagedListenerChannel and StateManagedNWConnectionChannel.

Result:

The typo is fixed after this change.
2023-09-25 07:34:20 +01:00
Joannis Orlandos e7403c35ca
Add support for UDP clients and servers.
Motivation:

This change was made because UDP support was lacking on iOS. It's needed by my DNS client implementation, which I am in turn using for an iOS app I'm working on relying on SRV typed records.

Modifications:

Adds a NIOTSDatagramListenerBootstrap for making UDP services
Adds a NIOTSDatagramListenerChannel that accepts UDP connections
Adds a NIOTSDatagramChannel for UDP client connections
Adds a NIOTSDatagramBootstrap that can create a new UDP client
2023-08-10 14:02:19 +01:00
Franz Busch 5fd1458c24
Back out SPI(AsyncChannel) changes (#184)
# Motivation
We want to release a new `NIOTS` version without the SPI changes for now.

# Modification
This PR backs out the new `NIOAsyncChannel` APIs.

# Result
No more SPI usage so we can safely release.
2023-08-09 13:13:21 +01:00
Johannes Weiss 39ece4ed45
NIOSingletonsTransportServices: Use NIOTS in easy mode (#180)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2023-08-09 03:29:24 -07:00
Franz Busch fbc49d892b
Adopt latest SPI(AsyncChannel) changes (#181)
# Motivation
We introduced some breaking SPI(AsyncChannel) changes in NIO that we have to adopt here.

# Modification
This PR adopts the latest `NIOProtocolNegotiationResult` APIs. Additionally, it also drops all bind/connect methods on the bootstraps that are specific to protocol negotiation or `NIOAsyncChannel`.

# Result
Green CI on `main` and alignment between `NIOPosix` and `NIOTS.
2023-07-26 07:48:49 -07:00
Franz Busch 01fc0ae7e5
Adopt the `NIOAsyncChannel.Configuration` (#179)
# Motivation
We introduced a new configuration struct for `NIOAsyncChannel` to make handling and configuring it easier.

# Modification
This PR adopts the new APIs.

# Result
Less boilerplate in our bootstrap methods
2023-07-11 16:34:56 +01:00
Joannis Orlandos a22d2b1294
State Managed Listeners (#175)
* Extract the NWConnection code into StateManagedNWConnectionChannel

* Add a general setup for NWListener implementations

* Add support for UDPOptions as a NWOptionsProtocol type

* Complete the rebase to main

* Fix nits from PR review

* Clean up some of the invalid rebase
2023-07-09 07:41:03 -04:00
Joannis Orlandos 7a18352b5e
Extract the NWConnection code into StateManagedNWConnectionChannel (#174)
* Extract the NWConnection code into StateManagedNWConnectionChannel

* Process cory's feedback

* Remove dead code whose functionality is already handled by the helper protocol

* Make the address cache non-locked, because its lock has been moved to the accessors

* Move getting the Multipath option into the TCP channel, re-introduce the underscores to the addressCache properties

* Drop the umbrella import

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-07-08 12:41:46 +01:00
Franz Busch ee0c7ffcd6
Async methods for `NIOTSListenerBootstrap` and `NIOTSConnectionBootstrap` (#178)
* Async methods for `NIOTSListenerBootstrap` and `NIOTSConnectionBootstrap`

# Motivation
We want to support async bootstrapping with all our bootstraps.

# Modification
This PR adds support for the `NIOTSListenerBootstrap` and `NIOTSConnectionBootstrap`. It also adds the three variants of methods to it (abstract output, `NIOAsyncChannel` based and protocol negotiation based)

# Result
We now support asynchronous interaction with the `NIOTSListenerBootstrap` and `NIOTSConnectionBootstrap`

* Use protocolHandlers properly

* Update NIO version

* code review

* REview

* Doc indention
2023-07-06 15:17:56 +02:00
David Nadoba 41f4098903
Use underscore version of `NIOPreconcurrencySendable` to silence warning (#173)
### Motivation
`swift-nio` has deprecated `NIOPreconcurrencySendable` in `2.51.0`. Removing the protocol conformance would be an API breaking change. Keeping the conformance is low cost and not worth the risk.

### Changes
- replace `NIOPreconcurrencySendable` with underscore version `_NIOPreconcurrencySendable`

### Result
We compile again with the latest `swift-nio` release without warnings
2023-04-25 15:00:26 +02:00
Si Beaumont dd408dc2d4
Drop support for Swift 5.5 (#172)
* Drop support for Swift 5.5

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Next version will be 1.17.0

Signed-off-by: Si Beaumont <beaumont@apple.com>

---------

Signed-off-by: Si Beaumont <beaumont@apple.com>
2023-04-14 17:06:35 +01:00
Cory Benfield cf8315c87d
Drop Topics section (#171)
Motivation:

We try to support building our docs on Linux too, but that means we
can't explicitly reference any symbols from `index.md`.

Modifications:

Enable CI-ing docs.

Result:

Docs will render on Linux too.
2023-04-13 09:59:34 +01:00
Cory Benfield 59b966415d
Fix documentation and add infrastructure for CI-ing it (#169)
* Fix documentation and add infrastructure for CI-ing it

* Add 2023 to soundness
2023-04-11 13:35:16 +01:00
George Barnett 2c1fb7999d
Point docs to Swift Package Index (#168)
Motivation:

The NIO docs are now published on the Swift Package Index but the README
still refers to GitHub pages.

Modifications:

- Update README and other docs to point to Swift Package Index.

Result:

Documentation links work
2023-01-23 13:41:52 +00:00
Cory Benfield e676b1f044
Expose multicast service type (#165)
Motivation

As we've rolled out support for multicast on Linux, it makes sense to
add equivalent configuration for swift-nio-transport-services. The two
features aren't one-to-one, as Network.framework has substantially more
capability than the Linux functionality.

Modifications

- Expose a multipathServiceType channel option
- Add a test to confirm we use it properly

Result

Multicast service types are available.
2022-11-09 11:00:20 +00:00
David Nadoba c0d9a144cf
Allow retrieval of metadata from `NWConnection` (#163)
* Allow retrieval of metadata for a specific protocol from the underlying `NWConnection`

* Address review comments

* Address review comment

* Add availability annotation to tests

* Rename `NIOTSChannelIsNotATransportServicesChannel ` to `NIOTSChannelIsNotANIOTSConnectionChannel `
2022-10-19 15:59:33 +01:00
David Nadoba de5ea3d0b4
Fix Swift 5.5 (#162) 2022-10-13 14:47:28 +01:00
David Nadoba 330f4ee104
Remove `#if compiler(>=5.5)` (#161) 2022-10-13 12:50:25 +01:00
George Barnett 8fda939e1b
Raise minimum supported Swift version from 5.4 to 5.5 (#158)
Motivation:

SwiftNIO periodically drops support for older Swift versions. Now that
5.7 has been released, 5.4 will be dropped.

Modifications:

- Remove 5.4 specific Package.swift and docker-compose
- Update the 5.7 docker-compose to use the released 5.7 and move from
  focal (2004) to jammy (2204)
- Update docs

Results:

Minimum Swift version is 5.5
2022-09-29 01:23:09 -07:00
George Barnett b6e37a0d44
Add missing availability annotation to NIOTSConnectionBootstrap (#160)
Motivation:

- NIOTSConnectionBootstrap was extended to be marked as non-Sendable,
  this extenion missed the availability annotations.

Modifications:

- Add appropriate availability annotation

Result:

Resolves #159
2022-09-28 16:04:35 +01:00
Cory Benfield b39e53ad42
Replace Lock with NIOLock (#157)
Motivation:

Lock has been deprecated in favour of NIOLock. Warnings aren't great.

Modifications:

- Replace Lock with NIOLock
- Update the minimum required NIO version to 2.42.0.

Result:

Everything builds cleanly
2022-09-27 13:48:00 +01:00
carolinacass 5cd6fd45f7
Launching Services with existing NWConnection or NWListener objects (#156)
NIO Transport Services is not capable of launching services with existing NWConnection or NWListener objects. Being able to get
an existing NWConnection through a connection bootstrap and into a channel is a useful capability for advanced use cases.

Modifications:
* Added an option to bootstrap with existing NWListener and NWConnection
* Completed promise connection earlier within NIOTSChannels when AlreadyConfigured is called
* Added test with new NWConnection and NWListener to register Channels

Result:
Able to create and register a channel using an existing NWListener and NWConnection
2022-09-21 10:52:08 +01:00
David Nadoba c2e373fec5
Adopt `Sendable` (#155)
* Adopt `Sendable`

* Workaround Swift 5.4 compiler bug
2022-08-31 16:22:32 +01:00
Cory Benfield 4e02d9cf35
Use Docc for documentation (#154)
Motivation

Documentation is nice, and we can help support users by providing useful
clear docs.

Modifications

Add Docc to 5.6 and later builds
Make sure symbol references work
Add overview docs

Result

Nice rendering docs
2022-07-29 15:02:01 +01:00
David Nadoba 94645c8fcd
Use `swift-atomics` instead of `NIOAtomics` (#153) 2022-07-07 17:50:28 +01:00
Cory Benfield 605f7a4c55
Add defensive lifetime management for security metadata (#152)
Motivation:

Right now we're playing a little fast and loose with the lifetimes of
the sec_protocol_metadata_t. As a practical matter it is highly likely
that this is owned (and so kept alive by) the NWConnection, but rather
than risk that we should tighten up the lifetime management.

Modifications:

Use withExtendedLifetime to extend the lifetime.

Result:

Better lifetime management.
2022-07-05 09:25:11 +01:00
Cory Benfield 7d09200afa
Don't close when waiting unless we were asked to. (#149)
Motivation:

When we're waiting for connectivity, the user might tell us that they
aren't interested in waiting. In that case we take advantage of the
signal and close early.

However, the code as-written had a bug: we didn't care whether the user
told us they _didn't_ want to wait, or they _did_: we just closed
because they had an opinion! That's no good! We should only close if
they don't want to wait.

Modifications:

- Only close if the user doesn't want to wait.
- Add tests

Result:

We won't close if users don't want us to.
2022-06-07 14:36:37 +01:00
František Mikš e90ac29391
change NIOTSNetworkEvent initializers visibility to pubilc level (#148) 2022-06-06 03:35:54 -07:00
George Barnett 8ab824b140
Explicitly retain timer source when scheduling tasks (#138)
Motivation:

When a task is scheduled using a `DispatchTimerSource` on a `NIOTSEventLoop`
the source is retain via a callback on the promise associated with the
scheduled task. This stops the source from deinit'd before the task is
run. However, the callback being added is an implementation details of
`Scheduled` and the retain cycle is incidental.

If that detail chnaged (such as in
https://github.com/apple/swift-nio/pull/2011) then tasks may not run and
the promise could be leaked.

Modifications:

- Explicitly add a retain cycle between the future and the timer source
  which is broken by the promise being completed and callbacks run.

Result:

The timer source is kept alive until the event fires.
2022-01-12 10:37:50 +00:00
Cory Benfield e7f5278a26
Clean up imports and dependencies. (#130)
Motivation:

With NIO 2.32.0 we broke the core NIO module up into modules that split
apart the POSIX layer and the core abstractions. As a result, this
package no longer needs to express a hard dependency on the POSIX layer.

Modifications:

- Rewrote imports of NIO to NIOCore.
- Added NIOEmbedded imports where necessary in tests.
- Note that the main package still _depends_ on NIO, which is necessary
  for backwards-compatibility reasons. This dependency is unused.

Result:

No need to use NIOPosix.
2021-09-14 10:06:42 +01:00
Peter Adams 9571a61d23
Move check for event loop shutdown into taskQueue (#127)
Motivation:

The event loop state is not protected for access on different threads.
This means it must only be accessed from the task queue.

Modifications:

Move check for event loop shutdown into taskQueue

Result:

Event Loop state only accessed from the task queue.
2021-08-19 10:57:47 +01:00
George Barnett 5fd5ba4d3e
Use non-deprecated API for removing handlers (#125)
Motivation:

`removeHandlers(channel:)` was deprecated in NIO 2.32.0.

Modifications:

- Raise minimum required NIO version to 2.32.0
- Use `removeHandlers(pipeline:)`

Result:

We don't use deprecated API.
2021-08-17 18:01:25 +01:00
George Barnett 657537c2cf
Add support for syncOptions to NIOTSListenerChannel and NIOTSConnectionChannel (#117)
Motivation:

NIO 2.27.0 added optional support for synchronous channel options.
NIOTSListenerChannel and NIOTSConnectionChannel should support this.

Modifications:

- Add synchronous options for NIOTSConnectionChannel and
  NIOTSListenerChannel
- Avoid an allocation in 'getOption' for each channel if the caller is
  already on the right event loop by using 'makeSucceededFuture'
- Remove a no longer used internal function and an already dead private
  helper

Result:

- Support for sync options and fewer allocations
2021-03-16 08:38:47 +00:00