fix HoleMessage

This commit is contained in:
anlicheng 2026-05-21 14:21:08 +08:00
parent 5cb8e4646c
commit 8383743f2c
4 changed files with 8 additions and 70 deletions

View File

@ -470,7 +470,7 @@ extension SDLContextActor {
}
private func handleUDPHolePacket(remoteAddress: SocketAddress, message: SDLHoleMessage, source: SDLUDPHoleKind) async {
switch message.inboundMessage {
switch message {
case .control(let message):
switch message {
case .stunReply(_), .stunProbeReply(_):

View File

@ -1,37 +0,0 @@
//
// SDLQPSCounter.swift
// Tun
//
// Created by on 2024/4/16.
//
import Foundation
// qps
class SDLQPSCounter: @unchecked Sendable {
private var count = 0
private let timer: DispatchSourceTimer
private let label: String
private let queue = DispatchQueue(label: "com.punchnet.qps")
init(label: String) {
self.label = label
timer = DispatchSource.makeTimerSource(queue: queue)
timer.schedule(deadline: .now(), repeating: .seconds(1), leeway: .milliseconds(100))
timer.setEventHandler { [weak self] in
guard let self = self else { return }
self.count = 0
}
timer.resume()
}
func increment(num: Int = 1) {
queue.async {
self.count += num
}
}
deinit {
timer.cancel()
}
}

View File

@ -12,10 +12,7 @@ import SwiftProtobuf
// --MARK: ,
enum SDLHoleMessage {
case data(SDLData)
case register(SDLRegister)
case registerAck(SDLRegisterAck)
case stunProbeReply(SDLStunProbeReply)
case stunReply(SDLStunReply)
case control(SDLHoleControlMessage)
}
enum SDLHoleControlMessage {
@ -25,28 +22,6 @@ enum SDLHoleControlMessage {
case stunReply(SDLStunReply)
}
enum SDLHoleInboundMessage {
case control(SDLHoleControlMessage)
case data(SDLData)
}
extension SDLHoleMessage {
var inboundMessage: SDLHoleInboundMessage {
switch self {
case .data(let data):
return .data(data)
case .register(let register):
return .control(.register(register))
case .registerAck(let registerAck):
return .control(.registerAck(registerAck))
case .stunProbeReply(let stunProbeReply):
return .control(.stunProbeReply(stunProbeReply))
case .stunReply(let stunReply):
return .control(.stunReply(stunReply))
}
}
}
extension SDLHoleMessage {
static func decode(buffer: inout ByteBuffer) throws -> SDLHoleMessage? {
@ -67,25 +42,25 @@ extension SDLHoleMessage {
let registerPacket = try? SDLRegister(serializedBytes: bytes) else {
return nil
}
return .register(registerPacket)
return .control(.register(registerPacket))
case .registerAck:
guard let bytes = buffer.readBytes(length: buffer.readableBytes),
let registerAck = try? SDLRegisterAck(serializedBytes: bytes) else {
return nil
}
return .registerAck(registerAck)
return .control(.registerAck(registerAck))
case .stunProbeReply:
guard let bytes = buffer.readBytes(length: buffer.readableBytes),
let stunProbeReply = try? SDLStunProbeReply(serializedBytes: bytes) else {
return nil
}
return .stunProbeReply(stunProbeReply)
return .control(.stunProbeReply(stunProbeReply))
case .stunReply:
guard let bytes = buffer.readBytes(length: buffer.readableBytes),
let stunReply = try? SDLStunReply(serializedBytes: bytes) else {
return nil
}
return .stunReply(stunReply)
return .control(.stunReply(stunReply))
default:
SDLLogger.log("[SDLUDPHole] decode miss type: \(type)", for: .debug)

View File

@ -165,7 +165,7 @@ actor SDLUDPHoleService {
}
private func handleV4Message(remoteAddress: SocketAddress, message: SDLHoleMessage) async throws {
switch message.inboundMessage {
switch message {
case .control(let control):
switch control {
case .stunProbeReply(let probeReply):
@ -290,7 +290,7 @@ actor SDLUDPHoleServiceProxy {
switch event {
case .packet(_, let message, _):
switch message.inboundMessage {
switch message {
case .data(let data):
await self.packetInboundActor?.handleData(data)
case .control: