Changes
1 changed files (+0/-77)
-
-
@@ -46,83 +46,6 @@ public let id: Stringpublic let name: String public let version: String public init(id: String) async throws { self.id = id let queue = DispatchQueue(label: "RoonKit.Server.init") let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) defer { group.shutdownGracefully(queue: queue) { _ in } } let channel = try await DatagramBootstrap(group: group) .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) .bind(host: "0.0.0.0", port: 0) { channel in channel.eventLoop.makeCompletedFuture { return try NIOAsyncChannel< AddressedEnvelope<ByteBuffer>, AddressedEnvelope<ByteBuffer> >( wrappingChannelSynchronously: channel, ) } } async let read = try await channel.executeThenClose { inbound, outbound in for try await envelope in inbound { let res = try ServerDiscoveryResponse( from: Data(buffer: envelope.data) ) logger.debug("Received a valid SOOD message") guard res.uniqueId == id, let ipAddress = envelope.remoteAddress.ipAddress else { continue } logger.debug("Found a server at \(envelope.remoteAddress)") return (ipAddress, res) } // Once the below "executeThenClose" completed, channel will be closed and // the iterator ("inbound") exits, and program reaches this line. throw ServerLookupError.notFound } async let _ = channel.executeThenClose { inbound, outbound in let req = ServerDiscoveryRequest().data() for _ in 1...4 { do { logger.debug( "Sending a discovery query to \(multicastAddr.0):\(multicastAddr.1)" ) try await outbound.write( AddressedEnvelope( remoteAddress: .init( ipAddress: multicastAddr.0, port: multicastAddr.1 ), data: ByteBuffer(data: req), ) ) } catch { logger.warning("Unable to send discovery query: \(error)") } try await Task.sleep(for: .seconds(3)) } } let (host, msg) = try await read self.host = host self.name = msg.displayName self.version = msg.version self.port = msg.httpPort } public init( id: String, name: String,
-