fix session

This commit is contained in:
anlicheng 2026-05-21 14:35:43 +08:00
parent 8383743f2c
commit 6e0e40c398
3 changed files with 79 additions and 67 deletions

View File

@ -0,0 +1,36 @@
//
// Session.swift
// sdlan
// Session
// Created by on 2025/7/14.
//
import Foundation
import NIOCore
struct Session {
enum AddressType: String, Hashable {
case v4
case v6
}
// ip,
let dstMac: Data
// nat
let natAddress: SocketAddress
//
let addressType: AddressType
// 使
var lastTimestamp: Int32
init?(dstMac: Data, natAddress: SocketAddress, addressType: AddressType) {
self.dstMac = dstMac
self.natAddress = natAddress
self.addressType = addressType
self.lastTimestamp = Int32(Date().timeIntervalSince1970)
}
mutating func updateLastTimestamp(_ lastTimestamp: Int32) {
self.lastTimestamp = lastTimestamp
}
}

View File

@ -0,0 +1,27 @@
//
// SessionSnapshot.swift
// Tun
//
// Created by Codex on 2026/5/21.
//
import Foundation
final class SessionSnapshot: Snapshot {
private let sessions: [Data: [Session.AddressType: Session]]
init(sessions: [Data: [Session.AddressType: Session]]) {
self.sessions = sessions
}
func getSession(toAddress: Data) -> Session? {
guard let peerSessions = self.sessions[toAddress] else {
return nil
}
return peerSessions.values.max(by: { $0.lastTimestamp < $1.lastTimestamp })
}
static func empty() -> SessionSnapshot {
return SessionSnapshot(sessions: [:])
}
}

View File

@ -1,60 +1,10 @@
//
// Session.swift
// sdlan
// Session
// Created by on 2025/7/14.
// SessionTable.swift
// Tun
//
// Created by Codex on 2026/5/21.
//
import Foundation
import NIOCore
import Darwin
struct Session {
enum AddressType: String, Hashable {
case v4
case v6
}
// ip,
let dstMac: Data
// nat
let natAddress: SocketAddress
//
let addressType: AddressType
// 使
var lastTimestamp: Int32
init?(dstMac: Data, natAddress: SocketAddress, addressType: AddressType) {
self.dstMac = dstMac
self.natAddress = natAddress
self.addressType = addressType
self.lastTimestamp = Int32(Date().timeIntervalSince1970)
}
mutating func updateLastTimestamp(_ lastTimestamp: Int32) {
self.lastTimestamp = lastTimestamp
}
}
final class SessionSnapshot: Snapshot {
private let sessions: [Data: [Session.AddressType: Session]]
init(sessions: [Data: [Session.AddressType: Session]]) {
self.sessions = sessions
}
func getSession(toAddress: Data) -> Session? {
guard let peerSessions = self.sessions[toAddress] else {
return nil
}
return peerSessions.values.max(by: { $0.lastTimestamp < $1.lastTimestamp })
}
static func empty() -> SessionSnapshot {
return SessionSnapshot(sessions: [:])
}
}
actor SessionManager {
private var sessions: [Data: [Session.AddressType: Session]] = [:]
@ -141,5 +91,4 @@ actor SessionManager {
}
return SessionSnapshot(sessions: sessions)
}
}