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 { private func handleUDPHolePacket(remoteAddress: SocketAddress, message: SDLHoleMessage, source: SDLUDPHoleKind) async {
switch message.inboundMessage { switch message {
case .control(let message): case .control(let message):
switch message { switch message {
case .stunReply(_), .stunProbeReply(_): 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: , // --MARK: ,
enum SDLHoleMessage { enum SDLHoleMessage {
case data(SDLData) case data(SDLData)
case register(SDLRegister) case control(SDLHoleControlMessage)
case registerAck(SDLRegisterAck)
case stunProbeReply(SDLStunProbeReply)
case stunReply(SDLStunReply)
} }
enum SDLHoleControlMessage { enum SDLHoleControlMessage {
@ -25,28 +22,6 @@ enum SDLHoleControlMessage {
case stunReply(SDLStunReply) 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 { extension SDLHoleMessage {
static func decode(buffer: inout ByteBuffer) throws -> SDLHoleMessage? { static func decode(buffer: inout ByteBuffer) throws -> SDLHoleMessage? {
@ -67,25 +42,25 @@ extension SDLHoleMessage {
let registerPacket = try? SDLRegister(serializedBytes: bytes) else { let registerPacket = try? SDLRegister(serializedBytes: bytes) else {
return nil return nil
} }
return .register(registerPacket) return .control(.register(registerPacket))
case .registerAck: case .registerAck:
guard let bytes = buffer.readBytes(length: buffer.readableBytes), guard let bytes = buffer.readBytes(length: buffer.readableBytes),
let registerAck = try? SDLRegisterAck(serializedBytes: bytes) else { let registerAck = try? SDLRegisterAck(serializedBytes: bytes) else {
return nil return nil
} }
return .registerAck(registerAck) return .control(.registerAck(registerAck))
case .stunProbeReply: case .stunProbeReply:
guard let bytes = buffer.readBytes(length: buffer.readableBytes), guard let bytes = buffer.readBytes(length: buffer.readableBytes),
let stunProbeReply = try? SDLStunProbeReply(serializedBytes: bytes) else { let stunProbeReply = try? SDLStunProbeReply(serializedBytes: bytes) else {
return nil return nil
} }
return .stunProbeReply(stunProbeReply) return .control(.stunProbeReply(stunProbeReply))
case .stunReply: case .stunReply:
guard let bytes = buffer.readBytes(length: buffer.readableBytes), guard let bytes = buffer.readBytes(length: buffer.readableBytes),
let stunReply = try? SDLStunReply(serializedBytes: bytes) else { let stunReply = try? SDLStunReply(serializedBytes: bytes) else {
return nil return nil
} }
return .stunReply(stunReply) return .control(.stunReply(stunReply))
default: default:
SDLLogger.log("[SDLUDPHole] decode miss type: \(type)", for: .debug) 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 { private func handleV4Message(remoteAddress: SocketAddress, message: SDLHoleMessage) async throws {
switch message.inboundMessage { switch message {
case .control(let control): case .control(let control):
switch control { switch control {
case .stunProbeReply(let probeReply): case .stunProbeReply(let probeReply):
@ -290,7 +290,7 @@ actor SDLUDPHoleServiceProxy {
switch event { switch event {
case .packet(_, let message, _): case .packet(_, let message, _):
switch message.inboundMessage { switch message {
case .data(let data): case .data(let data):
await self.packetInboundActor?.handleData(data) await self.packetInboundActor?.handleData(data)
case .control: case .control: