@@ -91,6 +91,7 @@ extension SocketDescriptor {
9191 /// - Returns: The number of bytes that were sent.
9292 ///
9393 /// The corresponding C function is `send`
94+ @_alwaysEmitIntoClient
9495 public func send(
9596 _ buffer: UnsafeRawBufferPointer ,
9697 flags: MessageFlags = . none,
@@ -110,6 +111,54 @@ extension SocketDescriptor {
110111 }
111112 }
112113
114+ /// Send a message from a socket
115+ ///
116+ /// - Parameters:
117+ /// - buffer: The region of memory that contains the data being sent.
118+ /// - recipient: The socket address of the recipient.
119+ /// - flags: see `send(2)`
120+ /// - retryOnInterrupt: Whether to retry the send operation
121+ /// if it throws ``Errno/interrupted``.
122+ /// The default is `true`.
123+ /// Pass `false` to try only once and throw an error upon interruption.
124+ /// - Returns: The number of bytes that were sent.
125+ ///
126+ /// The corresponding C function is `sendto`
127+ @_alwaysEmitIntoClient
128+ public func send(
129+ _ buffer: UnsafeRawBufferPointer ,
130+ to recipient: SocketAddress ,
131+ flags: MessageFlags = . none,
132+ retryOnInterrupt: Bool = true
133+ ) throws -> Int {
134+ try _send (
135+ buffer,
136+ to: recipient,
137+ flags: flags,
138+ retryOnInterrupt: retryOnInterrupt
139+ ) . get ( )
140+ }
141+
142+ @usableFromInline
143+ internal func _send(
144+ _ buffer: UnsafeRawBufferPointer ,
145+ to recipient: SocketAddress ,
146+ flags: MessageFlags ,
147+ retryOnInterrupt: Bool
148+ ) throws -> Result < Int , Errno > {
149+ recipient. withUnsafeCInterop { adr, adrlen in
150+ valueOrErrno ( retryOnInterrupt: retryOnInterrupt) {
151+ system_sendto (
152+ self . rawValue,
153+ buffer. baseAddress,
154+ buffer. count,
155+ flags. rawValue,
156+ adr,
157+ adrlen)
158+ }
159+ }
160+ }
161+
113162 /// Receive a message from a socket
114163 ///
115164 /// - Parameters:
@@ -122,6 +171,7 @@ extension SocketDescriptor {
122171 /// - Returns: The number of bytes that were received.
123172 ///
124173 /// The corresponding C function is `recv`
174+ @_alwaysEmitIntoClient
125175 public func receive(
126176 into buffer: UnsafeMutableRawBufferPointer ,
127177 flags: MessageFlags = . none,
@@ -143,12 +193,69 @@ extension SocketDescriptor {
143193 }
144194 }
145195
196+ /// Receive a message from a socket
197+ ///
198+ /// - Parameters:
199+ /// - buffer: The region of memory to receive into.
200+ /// - flags: see `recv(2)`
201+ /// - retryOnInterrupt: Whether to retry the receive operation
202+ /// if it throws ``Errno/interrupted``.
203+ /// The default is `true`.
204+ /// Pass `false` to try only once and throw an error upon interruption.
205+ /// - Returns: The number of bytes that were received.
206+ ///
207+ /// The corresponding C function is `recvfrom`
208+ @_alwaysEmitIntoClient
209+ public func receive(
210+ into buffer: UnsafeMutableRawBufferPointer ,
211+ sender: inout SocketAddress ,
212+ flags: MessageFlags = . none,
213+ retryOnInterrupt: Bool = true
214+ ) throws -> Int {
215+ try _receive (
216+ into: buffer,
217+ sender: & sender,
218+ flags: flags,
219+ retryOnInterrupt: retryOnInterrupt
220+ ) . get ( )
221+ }
222+
223+ @usableFromInline
224+ internal func _receive(
225+ into buffer: UnsafeMutableRawBufferPointer ,
226+ sender: inout SocketAddress ,
227+ flags: MessageFlags ,
228+ retryOnInterrupt: Bool
229+ ) throws -> Result < Int , Errno > {
230+ sender. _withMutableCInterop ( entireCapacity: true ) { adr, adrlen in
231+ valueOrErrno ( retryOnInterrupt: retryOnInterrupt) {
232+ system_recvfrom (
233+ self . rawValue,
234+ buffer. baseAddress,
235+ buffer. count,
236+ flags. rawValue,
237+ adr,
238+ & adrlen)
239+ }
240+ }
241+ }
242+
146243 /// Accept a connection on a socket.
147244 ///
148245 /// The corresponding C function is `accept`.
149246 @_alwaysEmitIntoClient
150247 public func accept( retryOnInterrupt: Bool = true ) throws -> SocketDescriptor {
151- try _accept ( nil , nil , retryOnInterrupt: retryOnInterrupt) . get ( )
248+ try _accept ( retryOnInterrupt: retryOnInterrupt) . get ( )
249+ }
250+
251+ @usableFromInline
252+ internal func _accept(
253+ retryOnInterrupt: Bool
254+ ) -> Result < SocketDescriptor , Errno > {
255+ let fd = valueOrErrno ( retryOnInterrupt: retryOnInterrupt) {
256+ return system_accept ( self . rawValue, nil , nil )
257+ }
258+ return fd. map { SocketDescriptor ( rawValue: $0) }
152259 }
153260
154261 /// Accept a connection on a socket.
@@ -161,25 +268,25 @@ extension SocketDescriptor {
161268 ///
162269 /// Having this as an inout parameter allows you to reuse the same address
163270 /// value across multiple connections, without reallocating it.
271+ @_alwaysEmitIntoClient
164272 public func accept(
165273 client: inout SocketAddress ,
166274 retryOnInterrupt: Bool = true
167275 ) throws -> SocketDescriptor {
168- try client. _withMutableCInterop ( entireCapacity: true ) { adr, adrlen in
169- try _accept ( adr, & adrlen, retryOnInterrupt: retryOnInterrupt) . get ( )
170- }
276+ try _accept ( client: & client, retryOnInterrupt: retryOnInterrupt) . get ( )
171277 }
172278
173279 @usableFromInline
174280 internal func _accept(
175- _ address: UnsafeMutablePointer < CInterop . SockAddr > ? ,
176- _ addressLength: UnsafeMutablePointer < CInterop . SockLen > ? ,
177- retryOnInterrupt: Bool = true
281+ client: inout SocketAddress ,
282+ retryOnInterrupt: Bool
178283 ) -> Result < SocketDescriptor , Errno > {
179- let fd = valueOrErrno ( retryOnInterrupt: retryOnInterrupt) {
180- return system_accept ( self . rawValue, address, addressLength)
284+ client. _withMutableCInterop ( entireCapacity: true ) { adr, adrlen in
285+ let fd = valueOrErrno ( retryOnInterrupt: retryOnInterrupt) {
286+ return system_accept ( self . rawValue, adr, & adrlen)
287+ }
288+ return fd. map { SocketDescriptor ( rawValue: $0) }
181289 }
182- return fd. map { SocketDescriptor ( rawValue: $0) }
183290 }
184291
185292 // TODO: acceptAndSockaddr or something that (tries to) returns the sockaddr
0 commit comments