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 {
|
do {
|
||||||
try await self.startSuperClient()
|
try await self.startSuperClient()
|
||||||
} catch let err {
|
} catch let err {
|
||||||
NSLog("[SDLContext] SuperClient get error: \(err), will restart")
|
NSLog("SuperClient get error: \(err)")
|
||||||
await self.arpServer.clear()
|
await self.arpServer.clear()
|
||||||
try? await Task.sleep(for: .seconds(2))
|
try? await Task.sleep(for: .seconds(2))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,8 @@ 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)
|
||||||
@ -67,10 +69,6 @@ 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)
|
||||||
@ -84,14 +82,9 @@ 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)
|
||||||
@ -99,28 +92,17 @@ 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 true {
|
while !Task.isCancelled {
|
||||||
do {
|
await self.ping()
|
||||||
await self.ping()
|
try? await Task.sleep(nanoseconds: 5 * 1_000_000_000)
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,6 +182,7 @@ actor SDLSuperClient {
|
|||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
try! group.syncShutdownGracefully()
|
try! group.syncShutdownGracefully()
|
||||||
|
self.inboundContinuation.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -100,11 +100,7 @@ actor SDLUDPHole {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for try await _ in group {
|
try await group.waitForAll()
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
SDLLogger.log("[SDLUDPHole] group closed", level: .debug)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user