Skip to content

Commit 5eec031

Browse files
committed
WIP: some name changes
1 parent 763febd commit 5eec031

File tree

8 files changed

+44
-50
lines changed

8 files changed

+44
-50
lines changed

Sources/Samples/Connect.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct Connect: ParsableCommand {
8282
try line.withUTF8 { buffer in
8383
var buffer = UnsafeRawBufferPointer(buffer)
8484
while !buffer.isEmpty {
85-
let c = try socket.sendMessage(bytes: buffer, flags: flags)
85+
let c = try socket.send(buffer, flags: flags)
8686
buffer = .init(rebasing: buffer[c...])
8787
}
8888
}

Sources/Samples/Listen.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct Listen: ParsableCommand {
4343
info.protocol)
4444
do {
4545
try socket.bind(to: info.address)
46-
if !info.type.isConnectionLess {
46+
if !info.type.isConnectionless {
4747
try socket.listen(backlog: 10)
4848
}
4949
return (socket, info)
@@ -97,15 +97,15 @@ struct Listen: ParsableCommand {
9797
try socket.closeAfter {
9898
if udp {
9999
while true {
100-
let (count, flags) = try socket.receiveMessage(into: buffer, sender: &client)
100+
let (count, flags) = try socket.receive(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(into: buffer, sender: &client)
108+
let (count, flags) = try conn.receive(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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension SocketAddress {
5050
}
5151

5252
extension SocketDescriptor.ConnectionType {
53-
var isConnectionLess: Bool {
53+
var isConnectionless: Bool {
5454
self == .datagram || self == .reliablyDeliveredMessage
5555
}
5656
}

Sources/System/Sockets/SocketAddress+IPv4.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ extension SocketAddress {
4545
guard let value = value else { return nil }
4646
return IPv4(rawValue: value)
4747
}
48+
49+
/// Construct a `SocketAddress` holding an IPv4 address and port
50+
@_alwaysEmitIntoClient
51+
public init(ipv4 address: IPv4.Address, port: Port) {
52+
self.init(IPv4(address: address, port: port))
53+
}
4854
}
4955

5056
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)

Sources/System/Sockets/SocketAddress+IPv6.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ extension SocketAddress {
4646
return IPv6(rawValue: value)
4747
}
4848

49+
/// Construct a `SocketAddress` holding an IPv4 address and port
50+
@_alwaysEmitIntoClient
51+
public init(ipv6 address: IPv6.Address, port: Port) {
52+
self.init(IPv6(address: address, port: port))
53+
}
4954
}
5055

5156
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)

Sources/System/Sockets/SocketAddress.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extension SocketAddress {
144144
}
145145
}
146146
set {
147-
assert(newValue < _capacity)
147+
assert(newValue <= _capacity)
148148
switch _variant {
149149
case let .small(length: _, bytes: bytes):
150150
self._variant = .small(length: UInt8(newValue), bytes: bytes)

Sources/System/Sockets/SocketDescriptor+Messages.swift renamed to Sources/System/Sockets/SocketMessages.swift

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -391,26 +391,26 @@ extension SocketDescriptor {
391391
///
392392
/// The corresponding C function is `sendmsg`.
393393
@_alwaysEmitIntoClient
394-
public func sendMessage(
395-
bytes: UnsafeRawBufferPointer,
396-
recipient: SocketAddress? = nil,
394+
public func send(
395+
_ bytes: UnsafeRawBufferPointer,
396+
to recipient: SocketAddress? = nil,
397397
ancillary: AncillaryMessageBuffer? = nil,
398398
flags: MessageFlags = [],
399399
retryOnInterrupt: Bool = true
400400
) throws -> Int {
401-
try _sendMessage(
402-
bytes: bytes,
403-
recipient: recipient,
401+
try _send(
402+
bytes,
403+
to : recipient,
404404
ancillary: ancillary,
405405
flags: flags,
406406
retryOnInterrupt: retryOnInterrupt
407407
).get()
408408
}
409409

410410
@usableFromInline
411-
internal func _sendMessage(
412-
bytes: UnsafeRawBufferPointer,
413-
recipient: SocketAddress?,
411+
internal func _send(
412+
_ bytes: UnsafeRawBufferPointer,
413+
to recipient: SocketAddress?,
414414
ancillary: AncillaryMessageBuffer?,
415415
flags: MessageFlags,
416416
retryOnInterrupt: Bool
@@ -438,7 +438,7 @@ extension SocketDescriptor {
438438
}
439439
}
440440

441-
internal func _sendmsg(
441+
private func _sendmsg(
442442
_ message: UnsafePointer<CInterop.MsgHdr>,
443443
_ flags: CInt,
444444
retryOnInterrupt: Bool
@@ -451,40 +451,19 @@ extension SocketDescriptor {
451451

452452
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
453453
extension SocketDescriptor {
454-
455-
/// Receive a message from a socket.
456-
///
457-
/// TODO: describe every parameter and option.
458-
///
459-
/// The corresponding C function is `recvmsg`.
460-
@_alwaysEmitIntoClient
461-
public func receiveMessage(
462-
into bytes: UnsafeMutableRawBufferPointer,
463-
flags: MessageFlags = [],
464-
retryOnInterrupt: Bool = true
465-
) throws -> (received: Int, flags: MessageFlags) {
466-
return try _receiveMessage(
467-
into: bytes,
468-
sender: nil,
469-
ancillary: nil,
470-
flags: flags,
471-
retryOnInterrupt: retryOnInterrupt
472-
).get()
473-
}
474-
475454
/// Receive a message from a socket.
476455
///
477456
/// TODO: describe every parameter and option.
478457
///
479458
/// The corresponding C function is `recvmsg`.
480459
@_alwaysEmitIntoClient
481-
public func receiveMessage(
460+
public func receive(
482461
into bytes: UnsafeMutableRawBufferPointer,
483462
sender: inout SocketAddress,
484463
flags: MessageFlags = [],
485464
retryOnInterrupt: Bool = true
486465
) throws -> (received: Int, flags: MessageFlags) {
487-
return try _receiveMessage(
466+
return try _receive(
488467
into: bytes,
489468
sender: &sender,
490469
ancillary: nil,
@@ -499,13 +478,13 @@ extension SocketDescriptor {
499478
///
500479
/// The corresponding C function is `recvmsg`.
501480
@_alwaysEmitIntoClient
502-
public func receiveMessage(
481+
public func receive(
503482
into bytes: UnsafeMutableRawBufferPointer,
504483
ancillary: inout AncillaryMessageBuffer,
505484
flags: MessageFlags = [],
506485
retryOnInterrupt: Bool = true
507486
) throws -> (received: Int, flags: MessageFlags) {
508-
return try _receiveMessage(
487+
return try _receive(
509488
into: bytes,
510489
sender: nil,
511490
ancillary: &ancillary,
@@ -520,14 +499,14 @@ extension SocketDescriptor {
520499
///
521500
/// The corresponding C function is `recvmsg`.
522501
@_alwaysEmitIntoClient
523-
public func receiveMessage(
502+
public func receive(
524503
into bytes: UnsafeMutableRawBufferPointer,
525504
sender: inout SocketAddress,
526505
ancillary: inout AncillaryMessageBuffer,
527506
flags: MessageFlags = [],
528507
retryOnInterrupt: Bool = true
529508
) throws -> (received: Int, flags: MessageFlags) {
530-
return try _receiveMessage(
509+
return try _receive(
531510
into: bytes,
532511
sender: &sender,
533512
ancillary: &ancillary,
@@ -539,7 +518,7 @@ extension SocketDescriptor {
539518

540519
extension SocketDescriptor {
541520
@usableFromInline
542-
internal func _receiveMessage(
521+
internal func _receive(
543522
into bytes: UnsafeMutableRawBufferPointer,
544523
sender: UnsafeMutablePointer<SocketAddress>?,
545524
ancillary: UnsafeMutablePointer<AncillaryMessageBuffer>?,

Tests/SystemTests/SocketTest.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,21 @@ final class SocketTest: XCTestCase {
6161
name: "recvmsg", rawSocket, Wildcard(), 42,
6262
interruptable: true
6363
) { retryOnInterrupt in
64-
_ = try socket.receiveMessage(into: rawBuf,
65-
flags: .init(rawValue: 42),
66-
retryOnInterrupt: retryOnInterrupt)
64+
var sender = SocketAddress()
65+
_ = try socket.receive(into: rawBuf,
66+
sender: &sender,
67+
flags: .init(rawValue: 42),
68+
retryOnInterrupt: retryOnInterrupt)
6769
},
6870
MockTestCase(
6971
name: "sendmsg", rawSocket, Wildcard(), 42,
7072
interruptable: true
7173
) { retryOnInterrupt in
72-
_ = try socket.sendMessage(bytes: UnsafeRawBufferPointer(rawBuf),
73-
flags: .init(rawValue: 42),
74-
retryOnInterrupt: retryOnInterrupt)
74+
let recipient = SocketAddress(ipv4: .loopback, port: 123)
75+
_ = try socket.send(UnsafeRawBufferPointer(rawBuf),
76+
to: recipient,
77+
flags: .init(rawValue: 42),
78+
retryOnInterrupt: retryOnInterrupt)
7579
},
7680
]
7781

0 commit comments

Comments
 (0)