@@ -62,6 +62,25 @@ public extension AsyncSocketPool where Self == SocketPool<Poll> {
62
62
63
63
public struct AsyncSocket : Sendable {
64
64
65
+ public struct Message {
66
+ public let peerAddress : sockaddr_storage
67
+ public let bytes : [ UInt8 ]
68
+ public let interfaceIndex : UInt32 ?
69
+ public let localAddress : sockaddr_storage ?
70
+
71
+ public init (
72
+ peerAddress: sockaddr_storage ,
73
+ bytes: [ UInt8 ] ,
74
+ interfaceIndex: UInt32 ? = nil ,
75
+ localAddress: sockaddr_storage ? = nil
76
+ ) {
77
+ self . peerAddress = peerAddress
78
+ self . bytes = bytes
79
+ self . interfaceIndex = interfaceIndex
80
+ self . localAddress = localAddress
81
+ }
82
+ }
83
+
65
84
public let socket : Socket
66
85
let pool : any AsyncSocketPool
67
86
@@ -143,6 +162,21 @@ public struct AsyncSocket: Sendable {
143
162
} while true
144
163
}
145
164
165
+ public func receive( atMost length: Int ) async throws -> Message {
166
+ try Task . checkCancellation ( )
167
+
168
+ repeat {
169
+ do {
170
+ let ( peerAddress, bytes, interfaceIndex, localAddress) = try socket. receive ( length: length)
171
+ return Message ( peerAddress: peerAddress, bytes: bytes, interfaceIndex: interfaceIndex, localAddress: localAddress)
172
+ } catch SocketError . blocked {
173
+ try await pool. suspendSocket ( socket, untilReadyFor: . read)
174
+ } catch {
175
+ throw error
176
+ }
177
+ } while true
178
+ }
179
+
146
180
/// Reads bytes from the socket up to by not over/
147
181
/// - Parameter bytes: The max number of bytes to read
148
182
/// - Returns: an array of the read bytes capped to the number of bytes provided.
@@ -190,6 +224,29 @@ public struct AsyncSocket: Sendable {
190
224
try await send ( Array ( data) , to: address)
191
225
}
192
226
227
+ public func send(
228
+ message: [ UInt8 ] ,
229
+ to peerAddress: some SocketAddress ,
230
+ interfaceIndex: UInt32 ? = nil ,
231
+ from localAddress: ( some SocketAddress ) ? = nil
232
+ ) async throws {
233
+ let sent = try await pool. loopUntilReady ( for: . write, on: socket) {
234
+ try socket. send ( message: message, to: peerAddress, interfaceIndex: interfaceIndex, from: localAddress)
235
+ }
236
+ guard sent == message. count else {
237
+ throw SocketError . disconnected
238
+ }
239
+ }
240
+
241
+ public func send(
242
+ message: Data ,
243
+ to peerAddress: some SocketAddress ,
244
+ interfaceIndex: UInt32 ? = nil ,
245
+ from localAddress: ( some SocketAddress ) ? = nil
246
+ ) async throws {
247
+ try await send ( message: Array ( message) , to: peerAddress, interfaceIndex: interfaceIndex, from: localAddress)
248
+ }
249
+
193
250
public func close( ) throws {
194
251
try socket. close ( )
195
252
}
@@ -275,7 +332,7 @@ public struct AsyncSocketSequence: AsyncSequence, AsyncIteratorProtocol, Sendabl
275
332
public struct AsyncSocketMessageSequence : AsyncSequence , AsyncIteratorProtocol , Sendable {
276
333
public static let DefaultMaxMessageLength : Int = 1500
277
334
278
- public typealias Element = ( sockaddr_storage , [ UInt8 ] )
335
+ public typealias Element = AsyncSocket . Message
279
336
280
337
private let socket : AsyncSocket
281
338
private let maxMessageLength : Int
0 commit comments