|
| 1 | +// |
| 2 | +// AsyncMessageSocket.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Luke Howard on 11/11/2024. |
| 6 | +// Copyright © 2024 PADL Software Pty Ltd. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +import Foundation |
| 33 | + |
| 34 | +public struct AsyncMessageSocket<A: SocketAddress>: Sendable { |
| 35 | + public let socket: Socket |
| 36 | + |
| 37 | + let pool: any AsyncSocketPool |
| 38 | + |
| 39 | + public struct Message: Sendable { |
| 40 | + public let address: A |
| 41 | + public let data: Data |
| 42 | + |
| 43 | + public init(address: A, data: Data) { |
| 44 | + self.address = address |
| 45 | + self.data = data |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public init(socket: Socket, pool: some AsyncSocketPool) throws { |
| 50 | + self.socket = socket |
| 51 | + self.pool = pool |
| 52 | + try socket.setFlags(.nonBlocking) |
| 53 | + } |
| 54 | + |
| 55 | + public static func connected(to address: A, timeout: TimeInterval = 5) async throws -> Self { |
| 56 | + try await connected( |
| 57 | + to: address, |
| 58 | + pool: ClientPoolLoader.shared.getPool(), |
| 59 | + timeout: timeout |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + public static func connected(to address: A, |
| 64 | + pool: some AsyncSocketPool, |
| 65 | + timeout: TimeInterval = 5) async throws -> Self { |
| 66 | + try await withThrowingTimeout(seconds: timeout) { |
| 67 | + let socket = try Socket(domain: Int32(type(of: address).family), type: Socket.datagram) |
| 68 | + let asyncMessageSocket = try AsyncMessageSocket(socket: socket, pool: pool) |
| 69 | + try await asyncMessageSocket.connect(to: address) |
| 70 | + return asyncMessageSocket |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public func connect(to address: A) async throws { |
| 75 | + return try await pool.loopUntilReady(for: [.write], on: socket) { |
| 76 | + try socket.connect(to: address) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public func receive(atMost length: Int = 4096) async throws -> Message { |
| 81 | + try Task.checkCancellation() |
| 82 | + |
| 83 | + repeat { |
| 84 | + do { |
| 85 | + let (address, bytes): (A, [UInt8]) = try socket.receive(length: length) |
| 86 | + return Message(address: address, data: Data(bytes)) |
| 87 | + } catch SocketError.blocked { |
| 88 | + try await pool.suspendSocket(socket, untilReadyFor: .read) |
| 89 | + } catch { |
| 90 | + throw error |
| 91 | + } |
| 92 | + } while true |
| 93 | + } |
| 94 | + |
| 95 | + public func send(message: Message) async throws { |
| 96 | + let sent = try await pool.loopUntilReady(for: .write, on: socket) { |
| 97 | + try socket.send(Array(message.data), to: message.address) |
| 98 | + } |
| 99 | + guard sent == message.data.count else { |
| 100 | + throw SocketError.disconnected |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public func close() throws { |
| 105 | + try socket.close() |
| 106 | + } |
| 107 | + |
| 108 | + public var messages: AsyncSocketMessageSequence<A> { |
| 109 | + AsyncSocketMessageSequence(socket: self) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +public struct AsyncSocketMessageSequence<A: SocketAddress>: AsyncSequence, AsyncIteratorProtocol, Sendable { |
| 114 | + public typealias Element = AsyncMessageSocket<A>.Message |
| 115 | + |
| 116 | + let socket: AsyncMessageSocket<A> |
| 117 | + |
| 118 | + public func makeAsyncIterator() -> Self { self } |
| 119 | + |
| 120 | + public mutating func next() async throws -> Element? { |
| 121 | + return try await socket.receive() |
| 122 | + } |
| 123 | + |
| 124 | + public func nextMessage(atMost length: Int) async throws -> Element? { |
| 125 | + return try await socket.receive(atMost: length) |
| 126 | + } |
| 127 | +} |
0 commit comments