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