add error support

This commit is contained in:
anlicheng 2025-08-02 23:37:25 +08:00
parent 7af3b235e7
commit 3e96169690
4 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,11 @@
//
// SDLError.swift
// sdlan
//
// Created by on 2025/8/2.
//
enum SDLError: Error {
case socketClosed
case socketError
}

View File

@ -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)

View File

@ -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 {

View File

@ -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
}
}
}