Commit Graph
2140 Commits
Author SHA1 Message Date
Saleem AbdulrasoolandCory Benfield 38d8c7105c NIOCore: add explicit type cast for Windows compatibility (#2156)
Windows does not have a `socklen_t` type, instead using `CInt` for the
value.  This adds an explicit type cast to repair the build on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-06 05:28:23 -07:00
Saleem AbdulrasoolandCory Benfield 9f9a0b2bc0 NIOCore: adjust socket address handling on Windows (#2152)
Match the non-Windows path and unwrap the address pointer before use.
The two paths had diverged, and now converge in behaviour even if not in
spelling.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-06 03:40:59 -07:00
Konrad `ktoso` Malawski b94a0696df Fix docc warning about missing symbol (#2162)
ByteBuffer is in the same NIOCore module, so no need to classify it (and even if, it should be `NIOCore` instead).

`warning: Topic reference 'ByteBuffer' couldn't be resolved. No local documentation matches this reference.`
2022-06-06 11:26:06 +01:00
Saleem AbdulrasoolandCory Benfield eff9c964fe NIOCore: import additional socket types on Windows (#2151)
Add imports for additional socket types to enable building more of
NIOCore on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 13:08:22 -07:00
David Nadoba 9c60117db0 Adopt Sendable in ChannelPipeline.swift and ChannelInvoker.swift (#2143) 2022-06-01 08:42:24 -07:00
David NadobaandCory Benfield 4574bdde82 Adopt Sendable in Codec.swift and SingleStepByteToMessageDecoder.swift (#2149)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 08:24:21 -07:00
David NadobaandCory Benfield 97a568ac61 Adopt Sendable in RecvByteBufferAllocator.swift (#2148)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 16:08:04 +01:00
David NadobaandCory Benfield 6f119c2995 Adopt Sendable in ChannelHandlers.swift (#2144)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 06:32:17 -07:00
David NadobaandCory Benfield 2f8d5408ae Adopt Sendable in ConvenienceOptionSupport.swift (#2145)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 05:58:16 -07:00
David NadobaandCory Benfield 6178126298 Adopt Sendable in NIOCloseOnErrorHandler.swift (#2146)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 05:28:43 -07:00
Saleem AbdulrasoolandCory Benfield 13a32273f7 NIOCore: adjust labels for inet_{ntop,pton} on Windows (#2141)
Update the callsites for the new labels for the BSDSocketAPI interfaces
that were not updated to reflect the new labels.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 05:05:50 -07:00
Saleem Abdulrasool afbf7566d5 NIOCore: handle structural differences on Windows (#2140)
The `sockaddr_t` type on Windows is not laid out identically to the Unix
counterpart (due to pedantically correct reasons).  There exist an inner
union type for the reinterpretation of the storage which requires naming
the union type.
2022-05-31 16:07:32 -07:00
Saleem AbdulrasoolandCory Benfield 15aaf884b4 NIOCore: replace mode_t with CInt (#2132)
* NIOCore: replace `mode_t` with `CInt`

Windows does not have a `mode_t` type alias, instead using the
de-sugared `CInt` type.  De-sugar the instances to permit building on
Windows.

* Update Sources/NIOCore/FileHandle.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-05-31 14:15:47 -07:00
Saleem Abdulrasool 4d4c942c35 NIOCore: use errno accessor on Windows (#2139)
`errno` on Windows cannot be access as a raw name as it goes through a
"complex" macro for the TLS access.  Use the `_get_errno` helper instead
of accessing the `errno` through the macro for building on Windows.
2022-05-31 10:30:32 -07:00
Saleem Abdulrasool 8b3acb66b4 NIOCore: import INET{,6}_ADDRSTRLEN constants (#2138)
This imports the address string length constants from WinSock for
Windows.
2022-05-31 17:50:08 +01:00
Si Beaumont 9bf5075241 Add initial support for connected datagram sockets (#2084)
* socket: Make destinationPtr param optional in sendmsg(...)

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

* pdwm: Fixup documentation: scalar writes use sendmsg, not sendto

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

* pdwm: Make sockaddr pointer param optional in scalarWriteOperation

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

* pdwm: Add isConnected property to PendingDatagramWritesState

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

* pdwm: If socket is connected use NULL msg_name in sendmsg(2)

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

* BaseSocketChannel: Support connect after bind

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

* DatagramChannel: Implement connectSocket(to:)

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

* bootstrap: Rename bind0(makeChannel:registerAndBind:) to withNewChannel(makeChannel:bringup:)

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

* bootstrap: Add set of DatagramBootstrap.connect(...) APIs

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

* test: Remove DatagramChannelTests.testConnectionFails

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

* test: Add ConnectedDatagramChannelTests, inheriting from DatagramChannelTests

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

* NIOUDPEchoClient: Use connected-mode UDP

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

* soundness: Update copyright notice

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

* fixup: cleanup bootstrap APIs

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

* pdwm: Check address of pending write if connected and add test

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

* Revert "pdwm: Check address of pending write if connected and add test"

This reverts commit a4ee0756d5.

* channel: Fail buffered writes on connect and validate writes when connected

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

* Run soundness.sh to get linux tests generated

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

* NIOUDPEchoClient: Connect socket to remote only if --connect is used

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

* socket: Support ByteBuffer (without AddressedEnvelope) for DatagramChannel

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

* test: Simplify some test code

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

* pdwm: Factor out common, private add(_ pendingWrite:)

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

* channel: Support AddressedEnvelope on connected socket for control messages

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

* channel: Defer to common unwrapData for error handling

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

* channel: Throw more specific (new) errors, instead of IOError

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

* SocketChannelLifecycleManager: Add supportsReconnect boolean property, used in DatagramChannel

Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-05-31 13:21:41 +01:00
Saleem Abdulrasool 083eba3652 NIOCore: correct typo in error namespace on Windows (#2133)
There was a missing `r` in the error type which caused a build failure
on Windows.  Correct the spelling for the error on Windows.
2022-05-31 10:52:07 +01:00
Saleem Abdulrasool e109615309 NIOCore: import WSAGetLastError, GetLastError (#2131)
We use these functions to report errors from WinSock.  This was likely
excluded accidentally from previous commits.
2022-05-30 11:00:34 -07:00
Saleem Abdulrasool 0f93ed145b NIOCore: adjust for label change on Windows (#2130)
`NIOBSDSocket.inet_pton` and `NIOBSDSocket.inet_ntop` altered the label
for the family parameter but failed to update the Windows path.  Adjust
the name accordingly.
2022-05-30 10:32:30 -07:00
Saleem Abdulrasool 78c97a3cda NIOCore: add missing import for IO (#2129)
Import `ucrt` for access to `strerror` on Windows.
2022-05-30 18:15:24 +01:00
Saleem Abdulrasool 5f8608bf0b _NIODataStructures: repair the build on Windows (#2128)
Import `ucrt` which is the MSVC C library to match the behaviour of the
other platforms.  This allows building `_NIODataStructures` on Windows.
2022-05-30 09:51:13 -07:00
David NadobaandCory Benfield e16a340c57 Adopt Sendable for EventLoopFuture and EventLoopPromise (#2107)
* Adopt `Sendable` for `EventLoopFuture` and `EventLoopPromise`

* use internal typealias to deduplicate method bodies

* fix `Sendable` warnings for nightly toolchains

* fix compliation for Swift 5.5 & 5.6

* Adopt `Sendable` only in Swift 5.7+

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-05-30 11:35:23 +01:00
David Nadoba 94969e7280 Adopt Sendable in EventLoop.swift (#2101)
* Adopt `Sendable` in `EventLoop.swift`

* only adopt `Sendable` in Swift 5.6+

* add `@Sendable` only for Swift 5.7

* fix swift 5.5

* use internal typealias to deduplicate method bodies
2022-05-30 10:41:18 +01:00
Si Beaumont 92d20d92b0 wip: Use clock_gettime for NIODeadline.now() (#2119)
* wip: Use clock_gettime for NIODeadline.now()

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

* fixup: Add #if os(Linux) for clock_gettime use

* fixup: Add doc comments

Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-05-19 09:28:31 +01:00
Si Beaumont 9294f8da3b NIOPerformanceTester: Increase operations used in lock benchmarks from 1M to 10M (#2121) 2022-05-18 10:53:51 +01:00
David Nadoba 3dcb0f5e9b Introduce internal UnsafeTransfer type (#2115)
Motivation:

Soon we will need to silence some `Sendable` warnings. 
The recommended way is to use a wrapper type that is unconditionally 
`Sendable`. 

https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md#adaptor-types-for-legacy-codebases

Modifications:

- add internal `UnsafeTransfer` type

Result:

We can silence `Sendable` warnings and errors by using `UnsafeTransfer` to pass non-`Sendable` values around.
2022-05-17 08:58:11 +01:00
Si Beaumont 771257cc23 NIOPerformanceTester: Add DeadlineNowBenchmark for NIODeadline.now() (#2117)
* NIOPerformanceTester: Add DeadlineNowBenchmark for NIODeadline.now()

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

* fixup: Return counter from benchmark

Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-05-16 19:21:31 +01:00
David Nadoba 4fb931da3f Deprecate unsafe ChannelPipeline.context(handler:) methods (#2109)
`ChannelHandlerContext` is not `Sendable` and can not safly be used outside of its  EventLoop.
2022-05-13 12:39:03 +02:00
David Nadoba bd4366b32c Adopt Sendable for Channel (#2104) 2022-05-11 09:59:51 +01:00
David NadobaandCory Benfield 291143d83d Adopt Sendable for NIONetworkDevice (#2103)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-05-10 09:01:56 -07:00
David Nadoba 54aadfaf38 Adopt Sendable for NIOFileHandle.Mode and NIOFileHandle.Flags (#2102)
`NIOFileHandler` and `FileRegion` are not thread-safe and can not conform to `Sendable`
2022-05-10 16:42:19 +01:00
David Nadoba 45b90b9506 Adopt Sendable for SocketOptionProvider (#2100) 2022-05-10 09:19:54 +01:00
David Nadoba dbbdabb24a Adopt Sendable for Types in ChannelOptions Namespace (#2098)
* Adopt `Sendable` for Types in `ChannelOptions` Namespace

* mark `ChannelOption` as @preconcurrecy Sendable

and introduce `NIOPreconcurrencySendable`
2022-05-09 15:31:36 +01:00
David Nadoba 4fb6bf135b Adopt Sendable for CircularBuffer, MarkedCircularBuffer and types in NIOBSDSocket namespace (#2097)
* Adopt `Sendable` for types in `BSDSocket` namespace

* Adopt `Sendable` for `CircularBuffer` and `MarkedCircularBuffer`

and also `Endianness`

* move `NIOSendable` into its own file and copy it over to syscall integration tests
2022-05-06 12:34:31 +01:00
David NadobaandCory Benfield 788ade1cc3 Adopt Sendable for SocketAddress, AddressedEnvelope and friends (#2096)
* Box: Sendable

* SocketAdress: Sendable and friends

* AddressEnvelop: Sendable and friends

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-05-04 09:33:57 -07:00
Cory BenfieldandFranz Busch ad8500b3d0 Define AsyncEmbeddedEventLoop (#2083)
Motivation

The rise of Swift concurrency has meant that a number of our APIs need
to be recontextualised as async/await capable. While generally this is a
straightforward task, any time those APIs were tested using
EmbeddedChannel we have a testing issue. Swift Concurrency requires the
use of its own cooperative thread pool, which is completely incapable of
safely interoperating with EmbeddedChannel and EmbeddedEventLoop. This
is becuase those two types "embed" into the current thread and are not
thread-safe, but our concurrency-focused APIs want to enable users to
use them from any Task.

To that end we need to develop new types that serve the needs of
EmbeddedChannel and EmbeddedEventLoop (control over I/O and task
scheduling) while remaining fully thread-safe. This is the first of a
series of patches that adds this functionality, starting with the
AsyncEmbeddedEventLoop.

Modifications

- Define AsyncEmbeddedEventLoop

Result

A required building block for AsyncEmbeddedChannel exists.

Co-authored-by: Franz Busch <privat@franz-busch.de>
2022-05-04 09:03:03 -07:00
David Nadoba 5868651f58 Adopt Sendable for ByteBuffer and friends (#2092)
- `ByteBuffer`
- `ByteBufferView`
- `_ByteBufferSlice`
- `_UInt24`
- `_UInt56`
2022-05-03 08:55:23 -07:00
David Nadoba d47e96ad87 Adopt sendable in _NIODataStructures (#2091) 2022-05-03 05:12:37 -07:00
David Evans 71f60c4fd5 Make NIOConcurrencyHelpers Sendable (#2090)
Stop Sendable related warnings when using types defined inside NIOConcurrencyHelpers. All the code should actually already be thread safe, so no changes needed other than labelling some classes as either Sendable or @unchecked Sendable.
2022-04-28 17:04:46 +01:00
Si Beaumont 124119f0bb BaseSocket: Remove dead code in BaseSocket.bind(to:) (#2086)
Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-04-22 09:42:09 -07:00
Si BeaumontandCory Benfield 684c09315f Use unbuffered IO for stdout in NIOPerformanceTester (#2072)
* Use unbuffered IO for stdout in NIOPerformanceTester

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

* CRASH PERF TESTS: Up task count to 1M to test logging change

* Revert "CRASH PERF TESTS: Up task count to 1M to test logging change"

This reverts commit d8fa50eb15.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-04-20 13:16:14 -07:00
Johannes Weiss 5ec860a3c8 http_parser uses "main" as the main branch (#2078) 2022-04-13 22:00:29 +01:00
Cory Benfield 8f60b523b5 Remove invalid math in ByteBuffer slice slow path (#2075)
Motivation:

ByteBuffer uses a synthetic UInt24 as the lower bound of the slice of
memory that it can access. This necessitates a slow path when we create
slices of ByteBuffers that would force the lower bound to be larger than
a UInt24.

This slow path has a bunch of math within it, and it turns out that some
of it is invalid. This means in rare cases slicing a ByteBuffer can
cause a crash. That's not great: the code should only crash if we broke
an internal invariant, not because we did some invalid math.

In particular, the relevant check is calculating the upper bound of the
storage to be copied. This was:

```
min(storageRebaseAmount + _toCapacity(self._slice.count), self._slice.upperBound, storageRebaseAmount + capacity)
```

In this case, `storageRebaseAmount` is either `self._slice.lowerBound` or
`self._slice.lowerBound + self._readerIndex`: that is,
`storageRebaseAmount >= self._slice.lowerBound`. Given that
`self._slice.count == self._slice.upperBound - self._slice.lowerBound`,
we know that `storageRebaseAmount + self._slice.count` is always
strictly greater than or equal to `self._slice.upperBound`. Calculating
this is unnecessary. Unfortunately, it can also overflow: if
`self._slice.upperBound + self._readerIndex` is greater than UInt32 then
this will crash.

Note that the other math, `storageRebaseAmount + capacity`, has been
kept. `capacity` is always computed by us and this should only ever
overflow if we are increasing the size of the buffer past UInt32.max,
which we cannot handle anyway.

Modifications:

- Restructured and heavily commented _copyStorageAndRebase.
- Removed the redundant crashing check.

Result:

Less trapping!
2022-04-13 13:23:25 +01:00
Max Desiatov 564b29e103 Fix doc comment formatting in SingleStepByteToMessageDecoder.swift (#2076) 2022-04-12 07:17:13 -07:00
Si BeaumontandCory Benfield 67a3b5fb17 Improve the performance of copying CircularBuffer (#2059)
* Improve the performance of copying CirularBuffer

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

* fixup: Drop consuming

* fixup: whitespace

* fixup: endIndex instead of count

* fixup: Add missing self and fix test name

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

* Avoid allocations by using @inline(__always) function

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

* Manually inline the copy for each range

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

* fixup: Linux test names after test rename

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

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-04-04 11:58:16 +01:00
Si Beaumont 1af9615110 Increase runtime of performance tests to O(10 ms) to increase SNR (#2063)
Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-03-24 07:42:24 -07:00
Franz Busch aae5fd056c Add Hashable conformance to HTTPResponseStatus (#2069)
# Motivation
`HTTPResponseStatus` is currently not conforming to `Hashable`.

# Modification
Add `Hashable` conformance to `HTTPResponseStatus`

# Result
`HTTPResponseStatus` is `Hashable`
2022-03-23 15:39:44 +00:00
Si BeaumontandCory Benfield be5fb3c170 Add benchmarks for copying CircularBuffer to Array (#2058)
Signed-off-by: Si Beaumont <beaumont@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-03-07 08:39:28 -08:00
Cory BenfieldandGuillaume Lessard b2195f7973 Rebind less load more (#2055)
* rebind less, load more.

* Rebind even less, load even more

* Remove the mutatings

Co-authored-by: Guillaume Lessard <guillaume.lessard@apple.com>
2022-03-07 03:20:16 -08:00
BenedictSt d489d9f38f Fixed some typos (#2051)
Fixed some typos.
2022-02-22 01:37:47 -08:00