Compare commits

...
Author SHA1 Message Date
jackandGitHub 7d487e5bdb Merge branch 'main' into avoid-recreating-session 2025-10-15 01:04:51 +02:00
islam 4acb2cf540 Reuse an existing session instead of recreating 2025-10-14 23:45:03 +01:00
+8 -7
View File
@@ -60,23 +60,24 @@ final class NoiseSessionManager {
throw NoiseSessionError.alreadyEstablished throw NoiseSessionError.alreadyEstablished
} }
// Remove any existing non-established session let session: NoiseSession
if let existingSession = sessions[peerID], !existingSession.isEstablished() {
_ = sessions.removeValue(forKey: peerID)
}
if let existingSession = sessions[peerID] {
session = existingSession
session.reset()
} else {
// Create new initiator session // Create new initiator session
let session = SecureNoiseSession( session = SecureNoiseSession(
peerID: peerID, peerID: peerID,
role: .initiator, role: .initiator,
keychain: keychain, keychain: keychain,
localStaticKey: localStaticKey localStaticKey: localStaticKey
) )
sessions[peerID] = session sessions[peerID] = session
}
do { do {
let handshakeData = try session.startHandshake() return try session.startHandshake()
return handshakeData
} catch { } catch {
// Clean up failed session // Clean up failed session
_ = sessions.removeValue(forKey: peerID) _ = sessions.removeValue(forKey: peerID)