Skip to content

Commit 763febd

Browse files
committed
WIP: fixup the samples and rename things to make me less nervous
1 parent 661207f commit 763febd

File tree

4 files changed

+61
-72
lines changed

4 files changed

+61
-72
lines changed

Sources/Samples/Listen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ struct Listen: ParsableCommand {
9797
try socket.closeAfter {
9898
if udp {
9999
while true {
100-
let (count, flags) = try socket.receiveMessage(bytes: buffer, sender: &client)
100+
let (count, flags) = try socket.receiveMessage(into: buffer, sender: &client)
101101
print(prefix(client: client, flags: flags), terminator: "")
102102
try FileDescriptor.standardOutput.writeAll(buffer[..<count])
103103
}
104104
} else {
105105
let conn = try socket.accept(client: &client)
106106
complain("Connection from \(client.niceDescription)")
107107
while true {
108-
let (count, flags) = try conn.receiveMessage(bytes: buffer, sender: &client)
108+
let (count, flags) = try conn.receiveMessage(into: buffer, sender: &client)
109109
guard count > 0 else { break }
110110
print(prefix(client: client, flags: flags), terminator: "")
111111
try FileDescriptor.standardOutput.writeAll(buffer[..<count])

Sources/Samples/Util.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,10 @@ internal func complain(_ message: String) {
4242

4343
extension SocketAddress {
4444
var niceDescription: String {
45-
switch family {
46-
case .ipv4:
47-
if let ipv4 = IPv4(self) { return ipv4.description }
48-
return self.description
49-
case .ipv6:
50-
if let ipv6 = IPv6(self) { return ipv6.description }
51-
return self.description
52-
case .local:
53-
if let local = Local(self) { return local.description }
54-
return self.description
55-
default:
56-
return self.description
57-
}
45+
if let ipv4 = self.ipv4 { return ipv4.description }
46+
if let ipv6 = self.ipv6 { return ipv6.description }
47+
if let local = self.local { return local.description }
48+
return self.description
5849
}
5950
}
6051

Sources/System/Sockets/SocketAddress.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,6 @@ extension SocketAddress {
164164
}
165165
}
166166

167-
extension Optional where Wrapped == SocketAddress {
168-
internal func _withUnsafeBytes<R>(
169-
_ body: (UnsafeRawBufferPointer) throws -> R
170-
) rethrows -> R {
171-
guard let address = self else {
172-
return try body(UnsafeRawBufferPointer(start: nil, count: 0))
173-
}
174-
return try address.withUnsafeBytes(body)
175-
}
176-
}
177-
178-
179167
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
180168
extension SocketAddress {
181169
/// Calls `body` with an unsafe raw buffer pointer to the raw bytes of this

Sources/System/Sockets/SocketDescriptor+Messages.swift

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ extension SocketDescriptor {
165165
}
166166
}
167167

168-
168+
// Optional mapper helpers, for use in setting up message header structs.
169169
extension Optional where Wrapped == SocketDescriptor.AncillaryMessageBuffer {
170-
internal func _withUnsafeBytes<R>(
170+
fileprivate func _withUnsafeBytesOrNull<R>(
171171
_ body: (UnsafeRawBufferPointer) throws -> R
172172
) rethrows -> R {
173173
guard let buffer = self else {
@@ -177,6 +177,56 @@ extension Optional where Wrapped == SocketDescriptor.AncillaryMessageBuffer {
177177
}
178178
}
179179

180+
extension Optional where Wrapped == SocketAddress {
181+
fileprivate func _withUnsafeBytesOrNull<R>(
182+
_ body: (UnsafeRawBufferPointer) throws -> R
183+
) rethrows -> R {
184+
guard let address = self else {
185+
return try body(UnsafeRawBufferPointer(start: nil, count: 0))
186+
}
187+
return try address.withUnsafeBytes(body)
188+
}
189+
}
190+
extension Optional where Wrapped == UnsafeMutablePointer<SocketAddress> {
191+
fileprivate func _withMutableCInteropOrNull<R>(
192+
entireCapacity: Bool,
193+
_ body: (
194+
UnsafeMutablePointer<CInterop.SockAddr>?,
195+
inout CInterop.SockLen
196+
) throws -> R
197+
) rethrows -> R {
198+
guard let ptr = self else {
199+
var c: CInterop.SockLen = 0
200+
let result = try body(nil, &c)
201+
precondition(c == 0)
202+
return result
203+
}
204+
return try ptr.pointee._withMutableCInterop(
205+
entireCapacity: entireCapacity,
206+
body)
207+
}
208+
}
209+
210+
extension Optional
211+
where Wrapped == UnsafeMutablePointer<SocketDescriptor.AncillaryMessageBuffer>
212+
{
213+
internal func _withMutableCInterop<R>(
214+
entireCapacity: Bool,
215+
_ body: (UnsafeMutableRawPointer?, inout CInterop.SockLen) throws -> R
216+
) rethrows -> R {
217+
guard let buffer = self else {
218+
var length: CInterop.SockLen = 0
219+
let r = try body(nil, &length)
220+
precondition(length == 0)
221+
return r
222+
}
223+
return try buffer.pointee._withMutableCInterop(
224+
entireCapacity: entireCapacity,
225+
body
226+
)
227+
}
228+
}
229+
180230
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
181231
extension SocketDescriptor.AncillaryMessageBuffer: Collection {
182232
/// The index type in an ancillary message buffer.
@@ -365,8 +415,8 @@ extension SocketDescriptor {
365415
flags: MessageFlags,
366416
retryOnInterrupt: Bool
367417
) -> Result<Int, Errno> {
368-
recipient._withUnsafeBytes { recipient in
369-
ancillary._withUnsafeBytes { ancillary in
418+
recipient._withUnsafeBytesOrNull { recipient in
419+
ancillary._withUnsafeBytesOrNull { ancillary in
370420
var iov = CInterop.IOVec()
371421
iov.iov_base = UnsafeMutableRawPointer(mutating: bytes.baseAddress)
372422
iov.iov_len = bytes.count
@@ -487,46 +537,6 @@ extension SocketDescriptor {
487537
}
488538
}
489539

490-
extension Optional where Wrapped == UnsafeMutablePointer<SocketAddress> {
491-
internal func _withMutableCInterop<R>(
492-
entireCapacity: Bool,
493-
_ body: (
494-
UnsafeMutablePointer<CInterop.SockAddr>?,
495-
inout CInterop.SockLen
496-
) throws -> R
497-
) rethrows -> R {
498-
guard let ptr = self else {
499-
var c: CInterop.SockLen = 0
500-
let result = try body(nil, &c)
501-
precondition(c == 0)
502-
return result
503-
}
504-
return try ptr.pointee._withMutableCInterop(
505-
entireCapacity: entireCapacity,
506-
body)
507-
}
508-
}
509-
510-
extension Optional
511-
where Wrapped == UnsafeMutablePointer<SocketDescriptor.AncillaryMessageBuffer>
512-
{
513-
internal func _withMutableCInterop<R>(
514-
entireCapacity: Bool,
515-
_ body: (UnsafeMutableRawPointer?, inout CInterop.SockLen) throws -> R
516-
) rethrows -> R {
517-
guard let buffer = self else {
518-
var length: CInterop.SockLen = 0
519-
let r = try body(nil, &length)
520-
precondition(length == 0)
521-
return r
522-
}
523-
return try buffer.pointee._withMutableCInterop(
524-
entireCapacity: entireCapacity,
525-
body
526-
)
527-
}
528-
}
529-
530540
extension SocketDescriptor {
531541
@usableFromInline
532542
internal func _receiveMessage(
@@ -539,7 +549,7 @@ extension SocketDescriptor {
539549
let result: Result<Int, Errno>
540550
let receivedFlags: CInt
541551
(result, receivedFlags) =
542-
sender._withMutableCInterop(entireCapacity: true) { adr, adrlen in
552+
sender._withMutableCInteropOrNull(entireCapacity: true) { adr, adrlen in
543553
ancillary._withMutableCInterop(entireCapacity: true) { anc, anclen in
544554
var iov = CInterop.IOVec()
545555
iov.iov_base = bytes.baseAddress

0 commit comments

Comments
 (0)