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