137 lines
3.8 KiB
Swift
137 lines
3.8 KiB
Swift
import Foundation
|
|
import NetworkExtension
|
|
|
|
actor SDLPacketReaderService {
|
|
enum Event {
|
|
case packet(IPPacket)
|
|
}
|
|
|
|
typealias EventHandler = @Sendable (Event) async -> Void
|
|
|
|
private final class CancellationToken: @unchecked Sendable {
|
|
private let lock = NSLock()
|
|
private var cancelled: Bool = false
|
|
|
|
var isCancelled: Bool {
|
|
lock.lock()
|
|
defer {
|
|
lock.unlock()
|
|
}
|
|
return cancelled
|
|
}
|
|
|
|
func cancel() {
|
|
lock.lock()
|
|
cancelled = true
|
|
lock.unlock()
|
|
}
|
|
}
|
|
|
|
private final class PacketReadContinuation: @unchecked Sendable {
|
|
typealias Result = (packets: [Data], protocols: [NSNumber])?
|
|
|
|
private let lock = NSLock()
|
|
private var continuation: CheckedContinuation<Result, Never>?
|
|
private var finished: Bool = false
|
|
|
|
func set(_ continuation: CheckedContinuation<Result, Never>) {
|
|
lock.lock()
|
|
if finished {
|
|
lock.unlock()
|
|
continuation.resume(returning: nil)
|
|
return
|
|
}
|
|
|
|
self.continuation = continuation
|
|
lock.unlock()
|
|
}
|
|
|
|
func resume(returning result: Result) {
|
|
lock.lock()
|
|
guard !finished else {
|
|
lock.unlock()
|
|
return
|
|
}
|
|
|
|
finished = true
|
|
let continuation = self.continuation
|
|
self.continuation = nil
|
|
lock.unlock()
|
|
|
|
continuation?.resume(returning: result)
|
|
}
|
|
|
|
func cancel() {
|
|
resume(returning: nil)
|
|
}
|
|
}
|
|
|
|
private let provider: NEPacketTunnelProvider
|
|
private let onEvent: EventHandler
|
|
private var readTask: Task<Void, Never>?
|
|
private var readToken: CancellationToken?
|
|
|
|
init(provider: NEPacketTunnelProvider, onEvent: @escaping EventHandler) {
|
|
self.provider = provider
|
|
self.onEvent = onEvent
|
|
}
|
|
|
|
func start() {
|
|
guard self.readTask == nil else {
|
|
return
|
|
}
|
|
|
|
let provider = self.provider
|
|
let onEvent = self.onEvent
|
|
let readToken = CancellationToken()
|
|
self.readToken = readToken
|
|
|
|
self.readTask = Task(priority: .high) {
|
|
while !Task.isCancelled && !readToken.isCancelled {
|
|
guard let batch = await Self.readPackets(from: provider) else {
|
|
break
|
|
}
|
|
|
|
if Task.isCancelled || readToken.isCancelled {
|
|
break
|
|
}
|
|
|
|
for (data, number) in zip(batch.packets, batch.protocols) where number.int32Value == 2 {
|
|
if Task.isCancelled || readToken.isCancelled {
|
|
break
|
|
}
|
|
|
|
if let packet = IPPacket(data) {
|
|
await onEvent(.packet(packet))
|
|
}
|
|
}
|
|
}
|
|
SDLLogger.log("[SDLPacketReaderService] readTask finished")
|
|
}
|
|
}
|
|
|
|
func stop() {
|
|
let readTask = self.readTask
|
|
self.readTask = nil
|
|
let readToken = self.readToken
|
|
self.readToken = nil
|
|
readToken?.cancel()
|
|
readTask?.cancel()
|
|
}
|
|
|
|
private static func readPackets(from provider: NEPacketTunnelProvider) async -> PacketReadContinuation.Result {
|
|
let readContinuation = PacketReadContinuation()
|
|
|
|
return await withTaskCancellationHandler {
|
|
await withCheckedContinuation { continuation in
|
|
readContinuation.set(continuation)
|
|
provider.packetFlow.readPackets { packets, protocols in
|
|
readContinuation.resume(returning: (packets: packets, protocols: protocols))
|
|
}
|
|
}
|
|
} onCancel: {
|
|
readContinuation.cancel()
|
|
}
|
|
}
|
|
}
|