Compare commits
No commits in common. "6cb8641869eabbb73564a6a8dc3d2ddaabd60eb5" and "3e961696907fbd1f254d7743f47600aaa659b759" have entirely different histories.
6cb8641869
...
3e96169690
@ -120,7 +120,7 @@ public class SDLContext: @unchecked Sendable {
|
||||
do {
|
||||
try await self.startSuperClient()
|
||||
} catch let err {
|
||||
NSLog("[SDLContext] SuperClient get error: \(err), will restart")
|
||||
NSLog("SuperClient get error: \(err)")
|
||||
await self.arpServer.clear()
|
||||
try? await Task.sleep(for: .seconds(2))
|
||||
}
|
||||
|
||||
@ -9,6 +9,8 @@ import Foundation
|
||||
import NIOCore
|
||||
import NIOPosix
|
||||
|
||||
|
||||
|
||||
// --MARK: 和SuperNode的客户端
|
||||
actor SDLSuperClient {
|
||||
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||
@ -67,10 +69,6 @@ actor SDLSuperClient {
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
defer {
|
||||
self.inboundContinuation.finish()
|
||||
}
|
||||
|
||||
for try await var packet in inbound {
|
||||
if let message = SDLSuperClientDecoder.decode(buffer: &packet) {
|
||||
SDLLogger.log("[SDLSuperTransport] read message: \(message)", level: .warning)
|
||||
@ -84,14 +82,9 @@ actor SDLSuperClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
NSLog("[SDLSuperClient] inbound closed")
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
defer {
|
||||
self.writeContinuation.finish()
|
||||
}
|
||||
|
||||
for try await message in self.writeStream {
|
||||
var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 5)
|
||||
buffer.writeInteger(message.packetId, as: UInt32.self)
|
||||
@ -99,28 +92,17 @@ actor SDLSuperClient {
|
||||
buffer.writeBytes(message.data)
|
||||
try await outbound.write(buffer)
|
||||
}
|
||||
NSLog("[SDLSuperClient] outbound closed")
|
||||
}
|
||||
|
||||
// --MARK: 心跳机制
|
||||
group.addTask {
|
||||
while true {
|
||||
do {
|
||||
while !Task.isCancelled {
|
||||
await self.ping()
|
||||
try await Task.sleep(nanoseconds: 5 * 1_000_000_000)
|
||||
} catch let err {
|
||||
NSLog("[SDLSuperClient] heartbeat cancelled with error: \(err)")
|
||||
break
|
||||
}
|
||||
try? await Task.sleep(nanoseconds: 5 * 1_000_000_000)
|
||||
}
|
||||
}
|
||||
|
||||
// 迭代等待所有任务的退出, 第一个异常会被抛出
|
||||
for try await _ in group {
|
||||
|
||||
}
|
||||
|
||||
NSLog("[SDLSuperClient] group closed")
|
||||
try await group.waitForAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -200,6 +182,7 @@ actor SDLSuperClient {
|
||||
|
||||
deinit {
|
||||
try! group.syncShutdownGracefully()
|
||||
self.inboundContinuation.finish()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -100,11 +100,7 @@ actor SDLUDPHole {
|
||||
}
|
||||
}
|
||||
|
||||
for try await _ in group {
|
||||
|
||||
}
|
||||
|
||||
SDLLogger.log("[SDLUDPHole] group closed", level: .debug)
|
||||
try await group.waitForAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user