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>
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>
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.`
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>
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.
* 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>
`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.
* 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>
`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.
* 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>
* 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
* 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>
* 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>
* 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
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>
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.
* 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>
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!
* 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>
# Motivation
`HTTPResponseStatus` is currently not conforming to `Hashable`.
# Modification
Add `Hashable` conformance to `HTTPResponseStatus`
# Result
`HTTPResponseStatus` is `Hashable`