punchnet-macos/Tun/Punchnet/Policy/PolicyRequesterActor.swift
2026-02-05 23:13:37 +08:00

57 lines
1.6 KiB
Swift

//
// SDLPuncherActor.swift
// Tun
//
// Created by on 2026/1/7.
//
import Foundation
import NIOCore
actor PolicyRequesterActor {
nonisolated private let cooldown: Duration = .seconds(5)
// identityId
private var coolingDown: Set<UInt32> = []
// , map[identityId] = version
private var versions: [UInt32: UInt32] = [:]
// holer
nonisolated private let querySocketAddress: SocketAddress
init(querySocketAddress: SocketAddress) {
self.querySocketAddress = querySocketAddress
}
//
func submitPolicyRequest(using udpHole: SDLUDPHole?, request: inout SDLPolicyRequest) {
let identityId = request.srcIdentityID
guard let udpHole, !coolingDown.contains(identityId) else {
return
}
//
coolingDown.insert(identityId)
let version = self.versions[identityId, default: 1]
request.version = version
//
self.versions[identityId] = version + 1
//
if let queryData = try? request.serializedData() {
udpHole.send(type: .policyRequest, data: queryData, remoteAddress: self.querySocketAddress)
}
Task {
//
try? await Task.sleep(for: .seconds(5))
self.endCooldown(for: identityId)
}
}
private func endCooldown(for key: UInt32) {
self.coolingDown.remove(key)
}
}