移除不必要的代码
This commit is contained in:
parent
3f5beb8410
commit
d26fbf9797
@ -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<Void, Never>?
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -24,14 +24,13 @@ actor SDLUDPHole {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var state: State = .idle
|
private var state: State = .idle
|
||||||
|
|
||||||
private let udpHoleHandler: SDLUDPHoleHandler
|
private let udpHoleHandler: SDLUDPHoleHandler
|
||||||
|
|
||||||
init() throws {
|
init() throws {
|
||||||
self.udpHoleHandler = try SDLUDPHoleHandler()
|
self.udpHoleHandler = try SDLUDPHoleHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() throws -> SocketAddress {
|
func start() async throws -> SocketAddress {
|
||||||
let localAddress = try self.udpHoleHandler.start()
|
let localAddress = try self.udpHoleHandler.start()
|
||||||
self.state = .running
|
self.state = .running
|
||||||
|
|
||||||
@ -50,10 +49,11 @@ actor SDLUDPHole {
|
|||||||
self.udpHoleHandler.send(type: type, data: data, remoteAddress: remoteAddress)
|
self.udpHoleHandler.send(type: type, data: data, remoteAddress: remoteAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() {
|
func stop() async {
|
||||||
guard self.state != .stopped else {
|
guard self.state != .stopped else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.state = .stopped
|
self.state = .stopped
|
||||||
self.udpHoleHandler.stop()
|
self.udpHoleHandler.stop()
|
||||||
}
|
}
|
||||||
@ -76,15 +76,11 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
|
|||||||
private let messageContinuation: AsyncThrowingStream<(SocketAddress, SDLHoleMessage), Error>.Continuation
|
private let messageContinuation: AsyncThrowingStream<(SocketAddress, SDLHoleMessage), Error>.Continuation
|
||||||
private var isMessageContinuationFinished: Bool = false
|
private var isMessageContinuationFinished: Bool = false
|
||||||
|
|
||||||
private let counterActor: SDLUDPCounter
|
|
||||||
|
|
||||||
// 启动函数
|
// 启动函数
|
||||||
init() throws {
|
init() throws {
|
||||||
let (stream, continuation) = AsyncThrowingStream.makeStream(of: (SocketAddress, SDLHoleMessage).self, bufferingPolicy: .bufferingNewest(2048))
|
let (stream, continuation) = AsyncThrowingStream.makeStream(of: (SocketAddress, SDLHoleMessage).self, bufferingPolicy: .bufferingNewest(2048))
|
||||||
self.messageStream = stream
|
self.messageStream = stream
|
||||||
self.messageContinuation = continuation
|
self.messageContinuation = continuation
|
||||||
|
|
||||||
self.counterActor = SDLUDPCounter()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() throws -> SocketAddress {
|
func start() throws -> SocketAddress {
|
||||||
@ -102,11 +98,6 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
|
|||||||
|
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
|
|
||||||
let counterActor = self.counterActor
|
|
||||||
Task {
|
|
||||||
await counterActor.start()
|
|
||||||
}
|
|
||||||
|
|
||||||
return localAddress
|
return localAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,11 +109,8 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
|
|||||||
let remoteAddress = envelope.remoteAddress
|
let remoteAddress = envelope.remoteAddress
|
||||||
|
|
||||||
let bytesCount = buffer.readableBytes
|
let bytesCount = buffer.readableBytes
|
||||||
let counterActor = self.counterActor
|
SDLLogger.log("[SDLUDPHole] read data: \(bytesCount), from: \(remoteAddress.description)", for: .debug)
|
||||||
Task {
|
|
||||||
await counterActor.increment(direction: .inbound, from: remoteAddress.description, bytes: bytesCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if let message = try SDLHoleMessage.decode(buffer: &buffer) {
|
if let message = try SDLHoleMessage.decode(buffer: &buffer) {
|
||||||
self.messageContinuation.yield((remoteAddress, message))
|
self.messageContinuation.yield((remoteAddress, message))
|
||||||
@ -149,11 +137,6 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
|
|||||||
return
|
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)
|
var buffer = channel.allocator.buffer(capacity: data.count + 1)
|
||||||
buffer.writeBytes([type.rawValue])
|
buffer.writeBytes([type.rawValue])
|
||||||
buffer.writeBytes(data)
|
buffer.writeBytes(data)
|
||||||
@ -177,11 +160,6 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
|
|||||||
try? channel?.close().wait()
|
try? channel?.close().wait()
|
||||||
try? self.group.syncShutdownGracefully()
|
try? self.group.syncShutdownGracefully()
|
||||||
|
|
||||||
let counterActor = self.counterActor
|
|
||||||
Task {
|
|
||||||
await counterActor.stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
SDLLogger.log("[SDLUDPHole] stopped", for: .debug)
|
SDLLogger.log("[SDLUDPHole] stopped", for: .debug)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user