逻辑集中管理
This commit is contained in:
parent
1fb7364c66
commit
4e781e881c
@ -76,7 +76,6 @@ actor SDLContextActor {
|
||||
// 处理权限控制
|
||||
private let identifyStore: IdentityStore
|
||||
private let snapshotPublisher: SnapshotPublisher<IdentitySnapshot>
|
||||
private let policyRequesterActor: PolicyRequesterActor
|
||||
|
||||
// 注册任务
|
||||
private var registerTask: Task<Void, Never>?
|
||||
@ -97,7 +96,6 @@ actor SDLContextActor {
|
||||
let snapshotPublisher = SnapshotPublisher(initial: IdentitySnapshot.empty())
|
||||
self.identifyStore = IdentityStore(publisher: snapshotPublisher)
|
||||
self.snapshotPublisher = snapshotPublisher
|
||||
self.policyRequesterActor = PolicyRequesterActor()
|
||||
}
|
||||
|
||||
public func start() {
|
||||
@ -578,7 +576,7 @@ actor SDLContextActor {
|
||||
policyRequest.srcIdentityID = data.identityID
|
||||
policyRequest.dstIdentityID = self.config.identityId
|
||||
|
||||
await self.policyRequesterActor.submitPolicyRequest(using: self.quicClient, request: &policyRequest)
|
||||
await self.identifyStore.policyRequest(using: self.quicClient, request: &policyRequest)
|
||||
}
|
||||
default:
|
||||
SDLLogger.shared.log("[SDLContext] get invalid packet", level: .debug)
|
||||
|
||||
@ -10,6 +10,13 @@ import NIO
|
||||
actor IdentityStore {
|
||||
typealias IdentityID = UInt32
|
||||
|
||||
// 处理权限的请求问题
|
||||
nonisolated private let cooldown: Duration = .seconds(5)
|
||||
// identityId
|
||||
private var coolingDown: Set<UInt32> = []
|
||||
// 处理各个请求的版本问题, map[identityId] = version
|
||||
private var versions: [UInt32: UInt32] = [:]
|
||||
|
||||
nonisolated private let alloctor = ByteBufferAllocator()
|
||||
|
||||
private let publisher: SnapshotPublisher<IdentitySnapshot>
|
||||
@ -19,6 +26,32 @@ actor IdentityStore {
|
||||
self.publisher = publisher
|
||||
}
|
||||
|
||||
// 提交权限请求
|
||||
func policyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) {
|
||||
let identityId = request.srcIdentityID
|
||||
guard let quicClient, !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() {
|
||||
quicClient.send(type: .policyRequest, data: queryData)
|
||||
}
|
||||
|
||||
Task {
|
||||
// 启动冷却期
|
||||
try? await Task.sleep(for: .seconds(5))
|
||||
self.endCooldown(for: identityId)
|
||||
}
|
||||
}
|
||||
|
||||
func apply(policyResponse: SDLPolicyResponse) {
|
||||
let id = policyResponse.srcIdentityID
|
||||
let version = policyResponse.version
|
||||
@ -50,4 +83,8 @@ actor IdentityStore {
|
||||
return IdentitySnapshot(identityMap: identityMap)
|
||||
}
|
||||
|
||||
private func endCooldown(for key: UInt32) {
|
||||
self.coolingDown.remove(key)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,54 +0,0 @@
|
||||
//
|
||||
// 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] = [:]
|
||||
|
||||
init() {
|
||||
|
||||
}
|
||||
|
||||
// 提交权限请求
|
||||
func submitPolicyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) {
|
||||
let identityId = request.srcIdentityID
|
||||
guard let quicClient, !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() {
|
||||
quicClient.send(type: .policyRequest, data: queryData)
|
||||
}
|
||||
|
||||
Task {
|
||||
// 启动冷却期
|
||||
try? await Task.sleep(for: .seconds(5))
|
||||
self.endCooldown(for: identityId)
|
||||
}
|
||||
}
|
||||
|
||||
private func endCooldown(for key: UInt32) {
|
||||
self.coolingDown.remove(key)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user