|
| 1 | +// |
| 2 | +// DebugServerTests.swift |
| 3 | +// OpenGraphShims |
| 4 | + |
| 5 | +#if canImport(Darwin) |
| 6 | +public import Foundation |
| 7 | +import Network |
| 8 | + |
| 9 | +#if canImport(OpenGraphCxx_Private) |
| 10 | +public import OpenGraphCxx_Private.DebugServer |
| 11 | +#endif |
| 12 | + |
| 13 | +@_spi(Debug) |
| 14 | +public final class DebugClient { |
| 15 | + private var connection: NWConnection? |
| 16 | + private let queue = DispatchQueue(label: "opengraph.debugserver.client.queue") |
| 17 | + |
| 18 | + public func connect(to url: URL) async throws { |
| 19 | + guard let host = url.host, let port = url.port else { |
| 20 | + throw ClientError.invalidURL |
| 21 | + } |
| 22 | + |
| 23 | + let nwHost = NWEndpoint.Host(host) |
| 24 | + let nwPort = NWEndpoint.Port(integerLiteral: UInt16(port)) |
| 25 | + |
| 26 | + connection = NWConnection(host: nwHost, port: nwPort, using: .tcp) |
| 27 | + |
| 28 | + return try await withCheckedThrowingContinuation { continuation in |
| 29 | + connection?.stateUpdateHandler = { state in |
| 30 | + switch state { |
| 31 | + case .ready: |
| 32 | + continuation.resume() |
| 33 | + case let .failed(error): |
| 34 | + continuation.resume(throwing: error) |
| 35 | + case .cancelled: |
| 36 | + continuation.resume(throwing: ClientError.connectionCancelled) |
| 37 | + default: |
| 38 | + break |
| 39 | + } |
| 40 | + } |
| 41 | + connection?.start(queue: queue) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public func sendMessage(token: UInt32, data: Data) async throws { |
| 46 | + guard let connection else { |
| 47 | + throw ClientError.notConnected |
| 48 | + } |
| 49 | + let header = DebugServerMessageHeader( |
| 50 | + token: token, |
| 51 | + unknown: 0, |
| 52 | + length: numericCast(data.count), |
| 53 | + unknown2: 0 |
| 54 | + ) |
| 55 | + let headerData = withUnsafePointer(to: header) { |
| 56 | + Data(bytes: UnsafeRawPointer($0), count: MemoryLayout<DebugServerMessageHeader>.size) |
| 57 | + } |
| 58 | + try await send(data: headerData, on: connection) |
| 59 | + guard header.length > 0 else { |
| 60 | + return |
| 61 | + } |
| 62 | + try await send(data: data, on: connection) |
| 63 | + } |
| 64 | + |
| 65 | + public func receiveMessage() async throws -> (header: DebugServerMessageHeader, data: Data) { |
| 66 | + guard let connection = connection else { |
| 67 | + throw ClientError.notConnected |
| 68 | + } |
| 69 | + let headerData = try await receive( |
| 70 | + length: MemoryLayout<DebugServerMessageHeader>.size, |
| 71 | + from: connection |
| 72 | + ) |
| 73 | + let header = headerData.withUnsafeBytes { bytes in |
| 74 | + let buffer = bytes.bindMemory(to: UInt32.self) |
| 75 | + return DebugServerMessageHeader( |
| 76 | + token: buffer[0], |
| 77 | + unknown: buffer[1], |
| 78 | + length: buffer[2], |
| 79 | + unknown2: buffer[3] |
| 80 | + ) |
| 81 | + } |
| 82 | + guard header.length > 0 else { |
| 83 | + return (header: header, data: Data()) |
| 84 | + } |
| 85 | + let payloadData = try await receive( |
| 86 | + length: numericCast(header.length), |
| 87 | + from: connection |
| 88 | + ) |
| 89 | + return (header: header, data: payloadData) |
| 90 | + } |
| 91 | + |
| 92 | + public func disconnect() { |
| 93 | + connection?.cancel() |
| 94 | + connection = nil |
| 95 | + } |
| 96 | + |
| 97 | + private func send(data: Data, on connection: NWConnection) async throws { |
| 98 | + return try await withCheckedThrowingContinuation { continuation in |
| 99 | + connection.send(content: data, completion: .contentProcessed { error in |
| 100 | + if let error { |
| 101 | + continuation.resume(throwing: error) |
| 102 | + } else { |
| 103 | + continuation.resume() |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private func receive(length: Int, from connection: NWConnection) async throws -> Data { |
| 110 | + return try await withCheckedThrowingContinuation { continuation in |
| 111 | + connection.receive(minimumIncompleteLength: length, maximumLength: length) { data, _, isComplete, error in |
| 112 | + if let error { |
| 113 | + continuation.resume(throwing: error) |
| 114 | + } else if let data { |
| 115 | + continuation.resume(returning: data) |
| 116 | + } else { |
| 117 | + continuation.resume(throwing: ClientError.noDataReceived) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +enum ClientError: Error { |
| 125 | + case invalidURL |
| 126 | + case notConnected |
| 127 | + case connectionCancelled |
| 128 | + case noDataReceived |
| 129 | +} |
| 130 | + |
| 131 | +#endif |
0 commit comments