Skip to content

Commit

Permalink
Return type-erased any SocketAddress for new UDP APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Nov 14, 2024
1 parent 09b09bf commit 9ce84ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions FlyingSocks/Sources/AsyncSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public extension AsyncSocketPool where Self == SocketPool<Poll> {
public struct AsyncSocket: Sendable {

public struct Message: Sendable {
public let peerAddress: sockaddr_storage
public let peerAddress: any SocketAddress
public let bytes: [UInt8]
public let interfaceIndex: UInt32?
public let localAddress: sockaddr_storage?
public let localAddress: (any SocketAddress)?

public init(
peerAddress: sockaddr_storage,
peerAddress: any SocketAddress,
bytes: [UInt8],
interfaceIndex: UInt32? = nil,
localAddress: sockaddr_storage? = nil
localAddress: (any SocketAddress)? = nil
) {
self.peerAddress = peerAddress
self.bytes = bytes
Expand Down Expand Up @@ -148,7 +148,7 @@ public struct AsyncSocket: Sendable {
return buffer
}

public func receive(atMost length: Int = 4096) async throws -> (sockaddr_storage, [UInt8]) {
public func receive(atMost length: Int = 4096) async throws -> (any SocketAddress, [UInt8]) {
try Task.checkCancellation()

repeat {
Expand Down Expand Up @@ -353,7 +353,7 @@ public struct AsyncSocketMessageSequence: AsyncSequence, AsyncIteratorProtocol,
#if !canImport(WinSDK)
try await socket.receive(atMost: maxMessageLength)

Check warning on line 354 in FlyingSocks/Sources/AsyncSocket.swift

View check run for this annotation

Codecov / codecov/patch

FlyingSocks/Sources/AsyncSocket.swift#L354

Added line #L354 was not covered by tests
#else
let peerAddress: sockaddr_storage
let peerAddress: any SocketAddress
let bytes: [UInt8]

(peerAddress, bytes) = try await socket.receive(atMost: maxMessageLength)
Expand Down
14 changes: 7 additions & 7 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,16 @@ public struct Socket: Sendable, Hashable {
return count
}

public func receive(length: Int) throws -> (sockaddr_storage, [UInt8]) {
var address: sockaddr_storage?
public func receive(length: Int) throws -> (any SocketAddress, [UInt8]) {
var address: (any SocketAddress)?
let bytes = try [UInt8](unsafeUninitializedCapacity: length) { buffer, count in
(address, count) = try receive(into: buffer.baseAddress!, length: length)
}

return (address!, bytes)
}

private func receive(into buffer: UnsafeMutablePointer<UInt8>, length: Int) throws -> (sockaddr_storage, Int) {
private func receive(into buffer: UnsafeMutablePointer<UInt8>, length: Int) throws -> (any SocketAddress, Int) {
var addr = sockaddr_storage()
var size = socklen_t(MemoryLayout<sockaddr_storage>.size)
let count = withUnsafeMutablePointer(to: &addr) {
Expand All @@ -291,10 +291,10 @@ public struct Socket: Sendable, Hashable {
}

#if !canImport(WinSDK)
public func receive(length: Int) throws -> (sockaddr_storage, [UInt8], UInt32?, sockaddr_storage?) {
var peerAddress: sockaddr_storage?
public func receive(length: Int) throws -> (any SocketAddress, [UInt8], UInt32?, (any SocketAddress)?) {
var peerAddress: (any SocketAddress)?
var interfaceIndex: UInt32?
var localAddress: sockaddr_storage?
var localAddress: (any SocketAddress)?

let bytes = try [UInt8](unsafeUninitializedCapacity: length) { buffer, count in
(peerAddress, count, interfaceIndex, localAddress) = try receive(into: buffer.baseAddress!, length: length, flags: 0)
Expand All @@ -309,7 +309,7 @@ public struct Socket: Sendable, Hashable {
into buffer: UnsafeMutablePointer<UInt8>,
length: Int,
flags: Int32
) throws -> (sockaddr_storage, Int, UInt32?, sockaddr_storage?) {
) throws -> (any SocketAddress, Int, UInt32?, (any SocketAddress)?) {
var iov = iovec()
var msg = msghdr()
var peerAddress = sockaddr_storage()
Expand Down
2 changes: 1 addition & 1 deletion FlyingSocks/Tests/AsyncSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ struct AsyncSocketTests {
func datagramSocketReceivesChunk_WhenAvailable() async throws {
let (s1, s2, addr) = try await AsyncSocket.makeDatagramPair()

async let d2: (sockaddr_storage, [UInt8]) = s2.receive(atMost: 100)
async let d2: (any SocketAddress, [UInt8]) = s2.receive(atMost: 100)
// TODO: calling send() on Darwin to an unconnected datagram domain
// socket returns EISCONN
#if canImport(Darwin)
Expand Down

0 comments on commit 9ce84ec

Please sign in to comment.