add error support
This commit is contained in:
parent
7af3b235e7
commit
3e96169690
11
Sources/Punchnet/SDLError.swift
Normal file
11
Sources/Punchnet/SDLError.swift
Normal file
@ -0,0 +1,11 @@
|
||||
//
|
||||
// SDLError.swift
|
||||
// sdlan
|
||||
//
|
||||
// Created by 安礼成 on 2025/8/2.
|
||||
//
|
||||
|
||||
enum SDLError: Error {
|
||||
case socketClosed
|
||||
case socketError
|
||||
}
|
||||
@ -47,6 +47,12 @@ actor SDLNoticeClient {
|
||||
func start() async throws {
|
||||
try await self.asyncChannel.executeThenClose { inbound, outbound in
|
||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||
group.addTask {
|
||||
try await self.asyncChannel.channel.closeFuture.get()
|
||||
NSLog("[UDPHole] channel closed")
|
||||
throw SDLError.socketClosed
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
for try await message in self.writeStream {
|
||||
let buf = self.asyncChannel.channel.allocator.buffer(bytes: message)
|
||||
|
||||
@ -9,6 +9,8 @@ import Foundation
|
||||
import NIOCore
|
||||
import NIOPosix
|
||||
|
||||
|
||||
|
||||
// --MARK: 和SuperNode的客户端
|
||||
actor SDLSuperClient {
|
||||
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||
@ -63,6 +65,7 @@ actor SDLSuperClient {
|
||||
group.addTask {
|
||||
try await self.asyncChannel.channel.closeFuture.get()
|
||||
NSLog("[SDLSuperClient] socket closed")
|
||||
throw SDLError.socketClosed
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
|
||||
@ -60,6 +60,7 @@ actor SDLUDPHole {
|
||||
group.addTask {
|
||||
try await self.asyncChannel.channel.closeFuture.get()
|
||||
NSLog("[UDPHole] channel closed")
|
||||
throw SDLError.socketClosed
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
@ -83,6 +84,7 @@ actor SDLUDPHole {
|
||||
}
|
||||
} catch let err {
|
||||
SDLLogger.log("[SDLUDPHole] decode message, get error: \(err)", level: .debug)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user