From 97c1683b0bc4c59917b0fb1fe3df255c8a5c4fe1 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 14 Nov 2024 03:29:34 -0500 Subject: [PATCH] Add support for typed throws --- Sources/Socket/System/CInternetAddress.swift | 2 +- Sources/Socket/System/CInterop.swift | 4 +- Sources/Socket/System/Constants.swift | 18 ++--- Sources/Socket/System/FileChange.swift | 10 +-- .../System/InputOutput/IOOperations.swift | 6 +- Sources/Socket/System/NetworkInterface.swift | 2 +- Sources/Socket/System/Poll.swift | 4 +- .../LinkLayerSocketAddress.swift | 8 +- .../Socket/System/SocketAddressFamily.swift | 2 +- Sources/Socket/System/SocketOperations.swift | 80 ++++++++++--------- Sources/Socket/System/SocketOption.swift | 12 +-- .../SocketProtocol/LinkLayerProtocol.swift | 4 +- Sources/Socket/System/Syscalls.swift | 20 +++-- 13 files changed, 94 insertions(+), 78 deletions(-) diff --git a/Sources/Socket/System/CInternetAddress.swift b/Sources/Socket/System/CInternetAddress.swift index 997a369..e783c5d 100644 --- a/Sources/Socket/System/CInternetAddress.swift +++ b/Sources/Socket/System/CInternetAddress.swift @@ -33,7 +33,7 @@ internal extension CInternetAddress { internal extension String { @usableFromInline - init(_ cInternetAddress: T) throws { + init(_ cInternetAddress: T) throws(Errno) { let cString = UnsafeMutablePointer.allocate(capacity: T.stringLength) defer { cString.deallocate() } let success = withUnsafePointer(to: cInternetAddress) { diff --git a/Sources/Socket/System/CInterop.swift b/Sources/Socket/System/CInterop.swift index 37751ff..5c19a46 100644 --- a/Sources/Socket/System/CInterop.swift +++ b/Sources/Socket/System/CInterop.swift @@ -1,6 +1,6 @@ import SystemPackage -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) import Glibc @@ -65,7 +65,7 @@ public extension CInterop { /// The C `sockaddr_in6` type typealias IPv6SocketAddress = sockaddr_in6 - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if canImport(Darwin) /// The C `sockaddr_dl` type typealias LinkLayerAddress = sockaddr_dl #elseif os(Linux) diff --git a/Sources/Socket/System/Constants.swift b/Sources/Socket/System/Constants.swift index 196f42f..ccc2bc1 100644 --- a/Sources/Socket/System/Constants.swift +++ b/Sources/Socket/System/Constants.swift @@ -1,6 +1,6 @@ import SystemPackage -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) import Glibc @@ -34,7 +34,7 @@ internal var _O_NONBLOCK: CInt { O_NONBLOCK } @_alwaysEmitIntoClient internal var _O_APPEND: CInt { O_APPEND } -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _O_SHLOCK: CInt { O_SHLOCK } @@ -60,7 +60,7 @@ internal var _O_TRUNC: CInt { O_TRUNC } @_alwaysEmitIntoClient internal var _O_EXCL: CInt { O_EXCL } -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _O_EVTONLY: CInt { O_EVTONLY } #endif @@ -74,7 +74,7 @@ internal var _O_NOCTTY: CInt { O_NOCTTY } internal var _O_DIRECTORY: CInt { O_DIRECTORY } #endif -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _O_SYMLINK: CInt { O_SYMLINK } #endif @@ -93,7 +93,7 @@ internal var _SEEK_CUR: CInt { SEEK_CUR } @_alwaysEmitIntoClient internal var _SEEK_END: CInt { SEEK_END } -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _SEEK_HOLE: CInt { SEEK_HOLE } @@ -196,7 +196,7 @@ internal var _AF_VSOCK: CInt { AF_VSOCK } internal var _AF_ISDN: CInt { AF_ISDN } #endif -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _AF_IMPLINK: CInt { AF_IMPLINK } @@ -341,7 +341,7 @@ internal var _SO_DONTROUTE: CInt { SO_DONTROUTE } @_alwaysEmitIntoClient internal var _SO_BROADCAST: CInt { SO_BROADCAST } -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _SO_USELOOPBACK: CInt { SO_USELOOPBACK } #endif @@ -381,7 +381,7 @@ internal var _MSG_WAITALL: CInt { numericCast(MSG_WAITALL) } /* wait for full re @_alwaysEmitIntoClient internal var _MSG_DONTWAIT: CInt { numericCast(MSG_DONTWAIT) } /* this message should be nonblocking */ -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) @_alwaysEmitIntoClient internal var _MSG_EOF: CInt { numericCast(MSG_EOF) } /* data completes connection */ @@ -416,7 +416,7 @@ internal var _MSG_MORE: CInt { numericCast(MSG_MORE) } @_alwaysEmitIntoClient internal var _fd_set_count: Int { -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) // __DARWIN_FD_SETSIZE is number of *bits*, so divide by number bits in each element to get element count // at present this is 1024 / 32 == 32 return Int(__DARWIN_FD_SETSIZE) / 32 diff --git a/Sources/Socket/System/FileChange.swift b/Sources/Socket/System/FileChange.swift index f720734..ddc9a64 100644 --- a/Sources/Socket/System/FileChange.swift +++ b/Sources/Socket/System/FileChange.swift @@ -7,7 +7,7 @@ public extension SocketDescriptor { func duplicate( closeOnExec: Bool, retryOnInterrupt: Bool = true - ) throws -> FileDescriptor { + ) throws(Errno) -> FileDescriptor { let fileDescriptor = try _change( closeOnExec ? .duplicateCloseOnExec : .duplicate, self.rawValue, @@ -18,7 +18,7 @@ public extension SocketDescriptor { /// Get Flags @_alwaysEmitIntoClient - func getFlags(retryOnInterrupt: Bool = true) throws -> FileDescriptor.Flags { + func getFlags(retryOnInterrupt: Bool = true) throws(Errno) -> FileDescriptor.Flags { let rawValue = try _change( .getFileDescriptorFlags, retryOnInterrupt: retryOnInterrupt @@ -31,7 +31,7 @@ public extension SocketDescriptor { func setFlags( _ newValue: FileDescriptor.Flags, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { let _ = try _change( .setFileDescriptorFlags, newValue.rawValue, @@ -41,7 +41,7 @@ public extension SocketDescriptor { /// Get Status @_alwaysEmitIntoClient - func getStatus(retryOnInterrupt: Bool = true) throws -> FileDescriptor.OpenOptions { + func getStatus(retryOnInterrupt: Bool = true) throws(Errno) -> FileDescriptor.OpenOptions { let rawValue = try _change( .getStatusFlags, retryOnInterrupt: retryOnInterrupt @@ -54,7 +54,7 @@ public extension SocketDescriptor { func setStatus( _ newValue: FileDescriptor.OpenOptions, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { let _ = try _change( .setStatusFlags, newValue.rawValue, diff --git a/Sources/Socket/System/InputOutput/IOOperations.swift b/Sources/Socket/System/InputOutput/IOOperations.swift index 70cec98..5717713 100644 --- a/Sources/Socket/System/InputOutput/IOOperations.swift +++ b/Sources/Socket/System/InputOutput/IOOperations.swift @@ -7,7 +7,7 @@ extension SocketDescriptor { public func inputOutput( _ request: T, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _inputOutput(request, retryOnInterrupt: true).get() } @@ -27,7 +27,7 @@ extension SocketDescriptor { public func inputOutput( _ request: T, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _inputOutput(request, retryOnInterrupt: retryOnInterrupt).get() } @@ -47,7 +47,7 @@ extension SocketDescriptor { public func inputOutput( _ request: inout T, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _inputOutput(&request, retryOnInterrupt: retryOnInterrupt).get() } diff --git a/Sources/Socket/System/NetworkInterface.swift b/Sources/Socket/System/NetworkInterface.swift index 0b71828..daea7e7 100644 --- a/Sources/Socket/System/NetworkInterface.swift +++ b/Sources/Socket/System/NetworkInterface.swift @@ -78,7 +78,7 @@ public struct NetworkInterfaceID: Equatable, Hashable { public extension NetworkInterfaceID { static var interfaces: [NetworkInterfaceID] { - get throws { + get throws(Errno) { // get null terminated list guard let pointer = system_if_nameindex() else { throw Errno.current diff --git a/Sources/Socket/System/Poll.swift b/Sources/Socket/System/Poll.swift index 870063a..2f545ed 100644 --- a/Sources/Socket/System/Poll.swift +++ b/Sources/Socket/System/Poll.swift @@ -57,7 +57,7 @@ extension SocketDescriptor { for events: FileEvents, timeout: Int = 0, retryOnInterrupt: Bool = true - ) throws -> FileEvents { + ) throws(Errno) -> FileEvents { try _poll( events: events, timeout: CInt(timeout), @@ -128,7 +128,7 @@ extension Array where Element == SocketDescriptor.Poll { public mutating func poll( timeout: Int = 0, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { guard isEmpty == false else { return } try SocketDescriptor._poll(&self, timeout: CInt(timeout), retryOnInterrupt: retryOnInterrupt).get() } diff --git a/Sources/Socket/System/SocketAddress/LinkLayerSocketAddress.swift b/Sources/Socket/System/SocketAddress/LinkLayerSocketAddress.swift index 16e8aa3..8425953 100644 --- a/Sources/Socket/System/SocketAddress/LinkLayerSocketAddress.swift +++ b/Sources/Socket/System/SocketAddress/LinkLayerSocketAddress.swift @@ -5,7 +5,7 @@ // Created by Alsey Coleman Miller on 10/1/22. // -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(Linux) +#if canImport(Darwin) || os(Linux) import Foundation import SystemPackage @_implementationOnly import CSocket @@ -15,7 +15,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable { public typealias ProtocolID = LinkLayerProtocol - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if canImport(Darwin) /// Index type public typealias Index = UInt16 @@ -34,7 +34,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable { public let address: String internal init(_ cValue: CSocketAddressType) { - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if canImport(Darwin) let index = cValue.sdl_index let address = Swift.withUnsafePointer(to: cValue) { String(cString: system_link_ntoa($0)) @@ -57,7 +57,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable { ) throws -> Result) rethrows -> Result { var socketAddress = CSocketAddressType() - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if canImport(Darwin) socketAddress.sdl_index = index self.address.withCString { system_link_addr($0, &socketAddress) diff --git a/Sources/Socket/System/SocketAddressFamily.swift b/Sources/Socket/System/SocketAddressFamily.swift index 7dc1f69..c6ede3b 100644 --- a/Sources/Socket/System/SocketAddressFamily.swift +++ b/Sources/Socket/System/SocketAddressFamily.swift @@ -55,7 +55,7 @@ public extension SocketAddressFamily { } #endif -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) public extension SocketAddressFamily { /// NetBIOS protocol diff --git a/Sources/Socket/System/SocketOperations.swift b/Sources/Socket/System/SocketOperations.swift index 92fce45..f5f756b 100644 --- a/Sources/Socket/System/SocketOperations.swift +++ b/Sources/Socket/System/SocketOperations.swift @@ -16,7 +16,7 @@ extension SocketDescriptor { public init( _ protocolID: T, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { self = try Self._socket(T.family, type: protocolID.type.rawValue, protocol: protocolID.rawValue, retryOnInterrupt: retryOnInterrupt).get() } @@ -37,7 +37,7 @@ extension SocketDescriptor { _ protocolID: T, flags: SocketFlags, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { self = try Self._socket(T.family, type: protocolID.type.rawValue | flags.rawValue, protocol: protocolID.rawValue, retryOnInterrupt: retryOnInterrupt).get() } #endif @@ -70,7 +70,7 @@ extension SocketDescriptor { _ protocolID: Address.ProtocolID, bind address: Address, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { self = try Self._socket( address: address, type: protocolID.type.rawValue, @@ -97,7 +97,7 @@ extension SocketDescriptor { bind address: Address, flags: SocketFlags, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { self = try Self._socket( address: address, type: protocolID.type.rawValue | flags.rawValue, @@ -139,7 +139,7 @@ extension SocketDescriptor { public func bind( _ address: Address, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _bind(address, retryOnInterrupt: retryOnInterrupt).get() } @@ -165,21 +165,21 @@ extension SocketDescriptor { /// /// The method corresponds to the C function `setsockopt`. @_alwaysEmitIntoClient - public func setSocketOption( - _ option: T, + public func setSocketOption( + _ option: Option, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _setSocketOption(option, retryOnInterrupt: retryOnInterrupt).get() } @usableFromInline - internal func _setSocketOption( - _ option: T, + internal func _setSocketOption( + _ option: Option, retryOnInterrupt: Bool ) -> Result<(), Errno> { nothingOrErrno(retryOnInterrupt: retryOnInterrupt) { option.withUnsafeBytes { bufferPointer in - system_setsockopt(self.rawValue, T.ID.optionLevel.rawValue, T.id.rawValue, bufferPointer.baseAddress!, UInt32(bufferPointer.count)) + system_setsockopt(self.rawValue, Option.ID.optionLevel.rawValue, Option.id.rawValue, bufferPointer.baseAddress!, UInt32(bufferPointer.count)) } } } @@ -194,23 +194,29 @@ extension SocketDescriptor { /// /// The method corresponds to the C function `getsockopt`. @_alwaysEmitIntoClient - public func getSocketOption( - _ option: T.Type, + public func getSocketOption( + _ option: Option.Type, retryOnInterrupt: Bool = true - ) throws -> T { - return try _getSocketOption(option, retryOnInterrupt: retryOnInterrupt) + ) throws(Errno) -> Option { + return try _getSocketOption(option, retryOnInterrupt: retryOnInterrupt).get() } @usableFromInline - internal func _getSocketOption( - _ option: T.Type, + internal func _getSocketOption( + _ option: Option.Type, retryOnInterrupt: Bool - ) throws -> T { - return try T.withUnsafeBytes { bufferPointer in - var length = UInt32(bufferPointer.count) - guard system_getsockopt(self.rawValue, T.ID.optionLevel.rawValue, T.id.rawValue, bufferPointer.baseAddress!, &length) != -1 else { - throw Errno.current + ) -> Result { + do { + let value = try Option.withUnsafeBytes { bufferPointer throws(Errno) -> () in + var length = UInt32(bufferPointer.count) + guard system_getsockopt(self.rawValue, Option.ID.optionLevel.rawValue, Option.id.rawValue, bufferPointer.baseAddress!, &length) != -1 else { + throw Errno.current + } } + return .success(value) + } + catch { + return .failure(error as! Errno) // TODO: Compiler error? } } @@ -231,7 +237,7 @@ extension SocketDescriptor { _ buffer: UnsafeRawBufferPointer, flags: MessageFlags = [], retryOnInterrupt: Bool = true - ) throws -> Int { + ) throws(Errno) -> Int { try _send(buffer, flags: flags, retryOnInterrupt: retryOnInterrupt).get() } @@ -252,7 +258,7 @@ extension SocketDescriptor { _ data: Data, flags: MessageFlags = [], retryOnInterrupt: Bool = true - ) throws -> Int where Data: Sequence, Data.Element == UInt8 { + ) throws(Errno) -> Int where Data: Sequence, Data.Element == UInt8 { try data._withRawBufferPointer { dataPointer in _send(dataPointer, flags: flags, retryOnInterrupt: retryOnInterrupt) }.get() @@ -288,7 +294,7 @@ extension SocketDescriptor { to address: Address, flags: MessageFlags = [], retryOnInterrupt: Bool = true - ) throws -> Int { + ) throws(Errno) -> Int { try _sendto(buffer, to: address, flags: flags, retryOnInterrupt: retryOnInterrupt).get() } @@ -310,7 +316,7 @@ extension SocketDescriptor { to address: Address, flags: MessageFlags = [], retryOnInterrupt: Bool = true - ) throws -> Int where Address: SocketAddress, Data: Sequence, Data.Element == UInt8 { + ) throws(Errno) -> Int where Address: SocketAddress, Data: Sequence, Data.Element == UInt8 { try data._withRawBufferPointer { dataPointer in _sendto(dataPointer, to: address, flags: flags, retryOnInterrupt: retryOnInterrupt) }.get() @@ -348,7 +354,7 @@ extension SocketDescriptor { into buffer: UnsafeMutableRawBufferPointer, flags: MessageFlags = [], retryOnInterrupt: Bool = true - ) throws -> Int { + ) throws(Errno) -> Int { try _receive( into: buffer, flags: flags, retryOnInterrupt: retryOnInterrupt ).get() @@ -384,7 +390,7 @@ extension SocketDescriptor { fromAddressOf addressType: Address.Type = Address.self, flags: MessageFlags = [], retryOnInterrupt: Bool = true - ) throws -> (Int, Address) { + ) throws(Errno) -> (Int, Address) { try _receivefrom( into: buffer, fromAddressOf: addressType, flags: flags, retryOnInterrupt: retryOnInterrupt ).get() @@ -423,7 +429,7 @@ extension SocketDescriptor { public func listen( backlog: Int, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _listen(backlog: Int32(backlog), retryOnInterrupt: retryOnInterrupt).get() } @@ -452,7 +458,7 @@ extension SocketDescriptor { public func accept( _ address: Address.Type, retryOnInterrupt: Bool = true - ) throws -> (SocketDescriptor, Address) { + ) throws(Errno) -> (SocketDescriptor, Address) { return try _accept(address, retryOnInterrupt: retryOnInterrupt).get() } @@ -484,7 +490,7 @@ extension SocketDescriptor { @_alwaysEmitIntoClient public func accept( retryOnInterrupt: Bool = true - ) throws -> SocketDescriptor { + ) throws(Errno) -> SocketDescriptor { return try _accept(retryOnInterrupt: retryOnInterrupt).get() } @@ -513,7 +519,7 @@ extension SocketDescriptor { public func connect( to address: Address, retryOnInterrupt: Bool = true - ) throws { + ) throws(Errno) { try _connect(to: address, retryOnInterrupt: retryOnInterrupt).get() } @@ -538,7 +544,7 @@ extension SocketDescriptor { /// /// The corresponding C function is `close`. @_alwaysEmitIntoClient - public func close() throws { try _close().get() } + public func close() throws(Errno) { try _close().get() } @usableFromInline internal func _close() -> Result<(), Errno> { @@ -569,7 +575,7 @@ extension SocketDescriptor { public func read( into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool = true - ) throws -> Int { + ) throws(Errno) -> Int { try _read(into: buffer, retryOnInterrupt: retryOnInterrupt).get() } @@ -603,7 +609,7 @@ extension SocketDescriptor { public func write( _ buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool = true - ) throws -> Int { + ) throws(Errno) -> Int { try _write(buffer, retryOnInterrupt: retryOnInterrupt).get() } @@ -621,7 +627,7 @@ extension SocketDescriptor { public func address( _ address: Address.Type, retryOnInterrupt: Bool = true - ) throws -> Address { + ) throws(Errno) -> Address { return try _getAddress(address, retryOnInterrupt: retryOnInterrupt).get() } @@ -644,7 +650,7 @@ extension SocketDescriptor { public func peerAddress( _ address: Address.Type, retryOnInterrupt: Bool = true - ) throws -> Address { + ) throws(Errno) -> Address { return try _getPeerAddress(address, retryOnInterrupt: retryOnInterrupt).get() } diff --git a/Sources/Socket/System/SocketOption.swift b/Sources/Socket/System/SocketOption.swift index 7722c78..0f288d4 100644 --- a/Sources/Socket/System/SocketOption.swift +++ b/Sources/Socket/System/SocketOption.swift @@ -6,11 +6,11 @@ public protocol SocketOption { static var id: ID { get } - func withUnsafeBytes(_ pointer: ((UnsafeRawBufferPointer) throws -> (Result))) rethrows -> Result + func withUnsafeBytes(_ pointer: ((UnsafeRawBufferPointer) throws(Error) -> (Result))) rethrows -> Result where Error: Swift.Error - static func withUnsafeBytes( - _ body: (UnsafeMutableRawBufferPointer) throws -> () - ) rethrows -> Self + static func withUnsafeBytes( + _ body: (UnsafeMutableRawBufferPointer) throws(Error) -> () + ) rethrows -> Self where Error: Swift.Error } public protocol BooleanSocketOption: SocketOption { @@ -30,13 +30,13 @@ extension BooleanSocketOption where Self: ExpressibleByBooleanLiteral { public extension BooleanSocketOption { - func withUnsafeBytes(_ pointer: ((UnsafeRawBufferPointer) throws -> (Result))) rethrows -> Result { + func withUnsafeBytes(_ pointer: ((UnsafeRawBufferPointer) throws(Error) -> (Result))) rethrows -> Result where Error: Swift.Error { return try Swift.withUnsafeBytes(of: boolValue.cInt) { bufferPointer in try pointer(bufferPointer) } } - static func withUnsafeBytes(_ body: (UnsafeMutableRawBufferPointer) throws -> ()) rethrows -> Self { + static func withUnsafeBytes(_ body: (UnsafeMutableRawBufferPointer) throws(Error) -> ()) rethrows -> Self where Error: Swift.Error { var value: CInt = 0 try Swift.withUnsafeMutableBytes(of: &value, body) return Self.init(Bool(value)) diff --git a/Sources/Socket/System/SocketProtocol/LinkLayerProtocol.swift b/Sources/Socket/System/SocketProtocol/LinkLayerProtocol.swift index 408b037..5d3ccc7 100644 --- a/Sources/Socket/System/SocketProtocol/LinkLayerProtocol.swift +++ b/Sources/Socket/System/SocketProtocol/LinkLayerProtocol.swift @@ -5,7 +5,7 @@ // Created by Alsey Coleman Miller on 10/1/22. // -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(Linux) +#if canImport(Darwin) || os(Linux) /// Unix Protocol Family public enum LinkLayerProtocol: Int32, Codable, SocketProtocol { @@ -13,7 +13,7 @@ public enum LinkLayerProtocol: Int32, Codable, SocketProtocol { @_alwaysEmitIntoClient public static var family: SocketAddressFamily { - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if canImport(Darwin) .link #elseif os(Linux) .packet diff --git a/Sources/Socket/System/Syscalls.swift b/Sources/Socket/System/Syscalls.swift index 32ed83f..30663d0 100644 --- a/Sources/Socket/System/Syscalls.swift +++ b/Sources/Socket/System/Syscalls.swift @@ -1,11 +1,21 @@ import SystemPackage -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(Android) -import Glibc #elseif os(Windows) +import CSystem import ucrt +#elseif canImport(Glibc) +@_implementationOnly import CSystem +import Glibc +#elseif canImport(Musl) +@_implementationOnly import CSystem +import Musl +#elseif canImport(WASILibc) +import WASILibc +#elseif canImport(Bionic) +@_implementationOnly import CSystem +import Bionic #else #error("Unsupported Platform") #endif @@ -74,7 +84,7 @@ internal func _mockInt( #endif // ENABLE_MOCKING -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if canImport(Darwin) internal var system_errno: CInt { get { Darwin.errno } set { Darwin.errno = newValue } @@ -452,7 +462,7 @@ internal func system_freeifaddrs(_ pointer: UnsafeMutablePointer, _ address: UnsafeMutablePointer) { #if ENABLE_MOCKING if mockingEnabled { return _mock(cString) }