-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PureSwift/feature/system-socket-api
Add `SocketDescriptor` and related low-level types.
- Loading branch information
Showing
42 changed files
with
3,946 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import SystemPackage | ||
|
||
#if swift(>=5.5) | ||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) | ||
public extension SocketDescriptor { | ||
|
||
/// Accept a connection on a socket. | ||
/// | ||
/// - Parameters: | ||
/// - retryOnInterrupt: Whether to retry the receive operation | ||
/// if it throws ``Errno/interrupted``. | ||
/// The default is `true`. | ||
/// Pass `false` to try only once and throw an error upon interruption. | ||
/// - sleep: The number of nanoseconds to sleep if the operation | ||
/// throws ``Errno/wouldBlock`` or other async I/O errors.. | ||
/// - Returns: The file descriptor of the new connection. | ||
/// | ||
/// The corresponding C function is `accept`. | ||
@_alwaysEmitIntoClient | ||
func accept( | ||
retryOnInterrupt: Bool = true, | ||
sleep: UInt64 = 10_000_000 | ||
) async throws -> SocketDescriptor { | ||
try await retry(sleep: sleep) { | ||
_accept(retryOnInterrupt: retryOnInterrupt) | ||
}.get() | ||
} | ||
|
||
/// Accept a connection on a socket. | ||
/// | ||
/// - Parameters: | ||
/// - address: The type of the `SocketAddress` expected for the new connection. | ||
/// - retryOnInterrupt: Whether to retry the receive operation | ||
/// if it throws ``Errno/interrupted``. | ||
/// The default is `true`. | ||
/// Pass `false` to try only once and throw an error upon interruption. | ||
/// - sleep: The number of nanoseconds to sleep if the operation | ||
/// throws ``Errno/wouldBlock`` or other async I/O errors. | ||
/// - Returns: A tuple containing the file descriptor and address of the new connection. | ||
/// | ||
/// The corresponding C function is `accept`. | ||
@_alwaysEmitIntoClient | ||
func accept<Address: SocketAddress>( | ||
_ address: Address.Type, | ||
retryOnInterrupt: Bool = true, | ||
sleep: UInt64 = 10_000_000 | ||
) async throws -> (SocketDescriptor, Address) { | ||
try await retry(sleep: sleep) { | ||
_accept(address, retryOnInterrupt: retryOnInterrupt) | ||
}.get() | ||
} | ||
|
||
/// Initiate a connection on a socket. | ||
/// | ||
/// - Parameters: | ||
/// - address: The peer address. | ||
/// - retryOnInterrupt: Whether to retry the receive operation | ||
/// if it throws ``Errno/interrupted``. | ||
/// The default is `true`. | ||
/// Pass `false` to try only once and throw an error upon interruption. | ||
/// - sleep: The number of nanoseconds to sleep if the operation | ||
/// throws ``Errno/wouldBlock`` or other async I/O errors. | ||
/// - Returns: The file descriptor of the new connection. | ||
/// | ||
/// The corresponding C function is `connect`. | ||
@_alwaysEmitIntoClient | ||
func connect<Address: SocketAddress>( | ||
to address: Address, | ||
retryOnInterrupt: Bool = true, | ||
sleep: UInt64 = 10_000_000 | ||
) async throws { | ||
try await retry(sleep: sleep) { | ||
_connect(to: address, retryOnInterrupt: retryOnInterrupt) | ||
}.get() | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.