fix quic
This commit is contained in:
parent
c12d2216df
commit
c3e93466b1
@ -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()))
|
||||
}
|
||||
cont.resume(returning: (isComplete, data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user