完善代码逻辑
This commit is contained in:
parent
b3526cac4d
commit
5e8b403fbf
@ -110,31 +110,3 @@ actor SDLSuperService {
|
||||
}
|
||||
}
|
||||
|
||||
actor SDLSuperServiceProxy {
|
||||
private var superService: SDLSuperService?
|
||||
private var generation: UInt64 = 0
|
||||
|
||||
func replace(_ superService: SDLSuperService?) async {
|
||||
self.generation &+= 1
|
||||
|
||||
let oldSuperService = self.superService
|
||||
self.superService = superService
|
||||
|
||||
if oldSuperService !== superService {
|
||||
await oldSuperService?.stop()
|
||||
}
|
||||
}
|
||||
|
||||
func stop() async {
|
||||
self.generation &+= 1
|
||||
|
||||
let superService = self.superService
|
||||
self.superService = nil
|
||||
|
||||
await superService?.stop()
|
||||
}
|
||||
|
||||
func send(type: SDLPacketType, data: Data) async {
|
||||
await self.superService?.send(type: type, data: data)
|
||||
}
|
||||
}
|
||||
|
||||
36
Tun/Super/SDLSuperServiceProxy.swift
Normal file
36
Tun/Super/SDLSuperServiceProxy.swift
Normal file
@ -0,0 +1,36 @@
|
||||
//
|
||||
// SDLSuperServiceProxy.swift
|
||||
// punchnet
|
||||
//
|
||||
// Created by 安礼成 on 2026/5/21.
|
||||
//
|
||||
import Foundation
|
||||
|
||||
actor SDLSuperServiceProxy {
|
||||
private var superService: SDLSuperService?
|
||||
private var generation: UInt64 = 0
|
||||
|
||||
func replace(_ superService: SDLSuperService?) async {
|
||||
self.generation &+= 1
|
||||
|
||||
let oldSuperService = self.superService
|
||||
self.superService = superService
|
||||
|
||||
if oldSuperService !== superService {
|
||||
await oldSuperService?.stop()
|
||||
}
|
||||
}
|
||||
|
||||
func stop() async {
|
||||
self.generation &+= 1
|
||||
|
||||
let superService = self.superService
|
||||
self.superService = nil
|
||||
|
||||
await superService?.stop()
|
||||
}
|
||||
|
||||
func send(type: SDLPacketType, data: Data) async {
|
||||
await self.superService?.send(type: type, data: data)
|
||||
}
|
||||
}
|
||||
@ -251,49 +251,3 @@ actor SDLUDPHoleService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actor SDLUDPHoleServiceProxy {
|
||||
typealias ControlEventHandler = @Sendable (SDLUDPHoleService.Event) async -> Void
|
||||
|
||||
private var udpHoleService: SDLUDPHoleService?
|
||||
private var generation: UInt64 = 0
|
||||
|
||||
func makeEventHandler(onControlEvent: @escaping ControlEventHandler) -> SDLUDPHoleService.EventHandler {
|
||||
self.generation &+= 1
|
||||
let generation = self.generation
|
||||
|
||||
return { [weak self] event in
|
||||
await self?.handleEvent(event, generation: generation, onControlEvent: onControlEvent)
|
||||
}
|
||||
}
|
||||
|
||||
func replace(_ udpHoleService: SDLUDPHoleService?) async {
|
||||
let oldUDPHoleService = self.udpHoleService
|
||||
self.udpHoleService = udpHoleService
|
||||
|
||||
if oldUDPHoleService !== udpHoleService {
|
||||
await oldUDPHoleService?.stop()
|
||||
}
|
||||
}
|
||||
|
||||
func stop() async {
|
||||
self.generation &+= 1
|
||||
|
||||
let udpHoleService = self.udpHoleService
|
||||
self.udpHoleService = nil
|
||||
|
||||
await udpHoleService?.stop()
|
||||
}
|
||||
|
||||
func send(type: SDLPacketType, data: Data, remoteAddress: SocketAddress) async {
|
||||
await self.udpHoleService?.send(type: type, data: data, remoteAddress: remoteAddress)
|
||||
}
|
||||
|
||||
private func handleEvent(_ event: SDLUDPHoleService.Event, generation: UInt64, onControlEvent: ControlEventHandler) async {
|
||||
guard generation == self.generation else {
|
||||
return
|
||||
}
|
||||
|
||||
await onControlEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
54
Tun/UDPHole/SDLUDPHoleServiceProxy.swift
Normal file
54
Tun/UDPHole/SDLUDPHoleServiceProxy.swift
Normal file
@ -0,0 +1,54 @@
|
||||
//
|
||||
// SDLUDPHoleServiceProxy.swift
|
||||
// punchnet
|
||||
//
|
||||
// Created by 安礼成 on 2026/5/21.
|
||||
//
|
||||
import Foundation
|
||||
import NIOCore
|
||||
|
||||
actor SDLUDPHoleServiceProxy {
|
||||
typealias ControlEventHandler = @Sendable (SDLUDPHoleService.Event) async -> Void
|
||||
|
||||
private var udpHoleService: SDLUDPHoleService?
|
||||
private var generation: UInt64 = 0
|
||||
|
||||
func makeEventHandler(onControlEvent: @escaping ControlEventHandler) -> SDLUDPHoleService.EventHandler {
|
||||
self.generation &+= 1
|
||||
let generation = self.generation
|
||||
|
||||
return { [weak self] event in
|
||||
await self?.handleEvent(event, generation: generation, onControlEvent: onControlEvent)
|
||||
}
|
||||
}
|
||||
|
||||
func replace(_ udpHoleService: SDLUDPHoleService?) async {
|
||||
let oldUDPHoleService = self.udpHoleService
|
||||
self.udpHoleService = udpHoleService
|
||||
|
||||
if oldUDPHoleService !== udpHoleService {
|
||||
await oldUDPHoleService?.stop()
|
||||
}
|
||||
}
|
||||
|
||||
func stop() async {
|
||||
self.generation &+= 1
|
||||
|
||||
let udpHoleService = self.udpHoleService
|
||||
self.udpHoleService = nil
|
||||
|
||||
await udpHoleService?.stop()
|
||||
}
|
||||
|
||||
func send(type: SDLPacketType, data: Data, remoteAddress: SocketAddress) async {
|
||||
await self.udpHoleService?.send(type: type, data: data, remoteAddress: remoteAddress)
|
||||
}
|
||||
|
||||
private func handleEvent(_ event: SDLUDPHoleService.Event, generation: UInt64, onControlEvent: ControlEventHandler) async {
|
||||
guard generation == self.generation else {
|
||||
return
|
||||
}
|
||||
|
||||
await onControlEvent(event)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user