diff --git a/Tun/Punchnet/UDPHole/SDLUDPCounter.swift b/Tun/Punchnet/UDPHole/SDLUDPCounter.swift deleted file mode 100644 index 09c76d0..0000000 --- a/Tun/Punchnet/UDPHole/SDLUDPCounter.swift +++ /dev/null @@ -1,74 +0,0 @@ -// -// SDLUDPCounter.swift -// punchnet -// -// Created by 安礼成 on 2026/5/6. -// - -import Foundation - -actor SDLUDPCounter { - - enum Direction { - case inbound - case outbound - } - - class Metrics { - var packetsNum: Int - var bytesNum: Int - - init() { - self.packetsNum = 0 - self.bytesNum = 0 - } - } - - private var inboundCounters: [String: Metrics] = [:] - private var outboundCounters: [String: Metrics] = [:] - - private var printTask: Task? - - func start() { - self.printTask = Task { - while true { - do { - try Task.checkCancellation() - try await Task.sleep(for: .seconds(1)) - for (from, metric) in inboundCounters { - SDLLogger.log("[SDLUDPCounter] inbound from: \(from), packet: \(metric.packetsNum), bytes: \(metric.bytesNum)") - } - self.inboundCounters.removeAll() - - for (from, metric) in outboundCounters { - SDLLogger.log("[SDLUDPCounter] outbound from: \(from), packet: \(metric.packetsNum), bytes: \(metric.bytesNum)") - } - self.outboundCounters.removeAll() - } catch { - break - } - } - } - } - - func increment(direction: Direction, from: String, bytes: Int) { - switch direction { - case .inbound: - let metric = inboundCounters[from, default: .init()] - metric.packetsNum += 1 - metric.bytesNum += bytes - inboundCounters[from] = metric - case .outbound: - let metric = outboundCounters[from, default: .init()] - metric.packetsNum += 1 - metric.bytesNum += bytes - outboundCounters[from] = metric - } - } - - func stop() { - self.printTask?.cancel() - self.printTask = nil - } - -} diff --git a/Tun/Punchnet/UDPHole/SDLUDPHole.swift b/Tun/Punchnet/UDPHole/SDLUDPHole.swift index 1c480a4..ee91ba3 100644 --- a/Tun/Punchnet/UDPHole/SDLUDPHole.swift +++ b/Tun/Punchnet/UDPHole/SDLUDPHole.swift @@ -24,14 +24,13 @@ actor SDLUDPHole { } private var state: State = .idle - private let udpHoleHandler: SDLUDPHoleHandler init() throws { self.udpHoleHandler = try SDLUDPHoleHandler() } - func start() throws -> SocketAddress { + func start() async throws -> SocketAddress { let localAddress = try self.udpHoleHandler.start() self.state = .running @@ -50,10 +49,11 @@ actor SDLUDPHole { self.udpHoleHandler.send(type: type, data: data, remoteAddress: remoteAddress) } - func stop() { + func stop() async { guard self.state != .stopped else { return } + self.state = .stopped self.udpHoleHandler.stop() } @@ -76,15 +76,11 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { private let messageContinuation: AsyncThrowingStream<(SocketAddress, SDLHoleMessage), Error>.Continuation private var isMessageContinuationFinished: Bool = false - private let counterActor: SDLUDPCounter - // 启动函数 init() throws { let (stream, continuation) = AsyncThrowingStream.makeStream(of: (SocketAddress, SDLHoleMessage).self, bufferingPolicy: .bufferingNewest(2048)) self.messageStream = stream self.messageContinuation = continuation - - self.counterActor = SDLUDPCounter() } func start() throws -> SocketAddress { @@ -102,11 +98,6 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { self.channel = channel - let counterActor = self.counterActor - Task { - await counterActor.start() - } - return localAddress } @@ -118,11 +109,8 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { let remoteAddress = envelope.remoteAddress let bytesCount = buffer.readableBytes - let counterActor = self.counterActor - Task { - await counterActor.increment(direction: .inbound, from: remoteAddress.description, bytes: bytesCount) - } - + SDLLogger.log("[SDLUDPHole] read data: \(bytesCount), from: \(remoteAddress.description)", for: .debug) + do { if let message = try SDLHoleMessage.decode(buffer: &buffer) { self.messageContinuation.yield((remoteAddress, message)) @@ -149,11 +137,6 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { return } - let counterActor = self.counterActor - Task { - await counterActor.increment(direction: .outbound, from: remoteAddress.description, bytes: data.count) - } - var buffer = channel.allocator.buffer(capacity: data.count + 1) buffer.writeBytes([type.rawValue]) buffer.writeBytes(data) @@ -177,11 +160,6 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { try? channel?.close().wait() try? self.group.syncShutdownGracefully() - let counterActor = self.counterActor - Task { - await counterActor.stop() - } - SDLLogger.log("[SDLUDPHole] stopped", for: .debug) }