This commit is contained in:
anlicheng 2026-02-20 00:32:25 +08:00
parent c12d2216df
commit c3e93466b1

View File

@ -25,12 +25,6 @@ final class SDLQUICClient {
private let (closeStream, closeCont) = AsyncStream.makeStream(of: Void.self) private let (closeStream, closeCont) = AsyncStream.makeStream(of: Void.self)
private let (readyStream, readyCont) = AsyncStream.makeStream(of: Void.self) private let (readyStream, readyCont) = AsyncStream.makeStream(of: Void.self)
enum Event {
case ready
case failed(Error)
case cancelled
}
init(host: String, port: UInt16) { init(host: String, port: UInt16) {
let options = NWProtocolQUIC.Options(alpn: ["punchnet/1.0"]) let options = NWProtocolQUIC.Options(alpn: ["punchnet/1.0"])
@ -41,7 +35,7 @@ final class SDLQUICClient {
// //
complete(true) // true = complete(true) // true =
}, },
DispatchQueue.global() self.queue
) )
let params = NWParameters(quic: options) let params = NWParameters(quic: options)
@ -49,7 +43,6 @@ final class SDLQUICClient {
} }
func start() { func start() {
SDLLogger.shared.log("[SDLQUICTransport] call start")
connection.stateUpdateHandler = { state in connection.stateUpdateHandler = { state in
SDLLogger.shared.log("[SDLQUICTransport] new state: \(state)") SDLLogger.shared.log("[SDLQUICTransport] new state: \(state)")
switch state { switch state {
@ -80,7 +73,11 @@ final class SDLQUICClient {
packet.append(type.rawValue) packet.append(type.rawValue)
packet.append(data) packet.append(data)
connection.send(content: packet, completion: .contentProcessed { _ in }) connection.send(content: packet, completion: .contentProcessed { error in
if let error {
SDLLogger.shared.log("[SDLQUICClient] send data get error: \(error)")
}
})
} }
func waitReady() async throws { func waitReady() async throws {
@ -123,7 +120,7 @@ actor SDLQUICReader {
do { do {
while !Task.isCancelled { while !Task.isCancelled {
let (isComplete, data) = try await self.readOnce() let (isComplete, data) = try await self.readOnce()
if !data.isEmpty { if let data, !data.isEmpty {
buffer.writeBytes(data) buffer.writeBytes(data)
let frames = try parseFrames(buffer: &buffer) let frames = try parseFrames(buffer: &buffer)
for frame in frames { for frame in frames {
@ -178,20 +175,14 @@ actor SDLQUICReader {
} }
// //
private func readOnce() async throws -> (Bool, Data) { private func readOnce() async throws -> (Bool, Data?) {
return try await withCheckedThrowingContinuation { cont in return try await withCheckedThrowingContinuation { cont in
connection.receive(minimumIncompleteLength: 1, maximumLength: maxPacketSize) { data, _, isComplete, error in connection.receive(minimumIncompleteLength: 1, maximumLength: maxPacketSize) { data, _, isComplete, error in
if let error { if let error {
cont.resume(throwing: error) cont.resume(throwing: error)
return return
} }
cont.resume(returning: (isComplete, data))
if let data, !data.isEmpty {
SDLLogger.shared.log("[SDLQUICTransport] read bytes: \(data.count)")
cont.resume(returning: (isComplete, data))
} else {
cont.resume(returning: (isComplete, Data()))
}
} }
} }
} }