From 4acb2cf540a7789afd70091547e520c6b944cda4 Mon Sep 17 00:00:00 2001 From: islam <2553451+qalandarov@users.noreply.github.com> Date: Tue, 14 Oct 2025 23:44:31 +0100 Subject: [PATCH] Reuse an existing session instead of recreating --- bitchat/Noise/NoiseSessionManager.swift | 29 +++++++++++++------------ 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bitchat/Noise/NoiseSessionManager.swift b/bitchat/Noise/NoiseSessionManager.swift index 92ccf9d6..7d7f9943 100644 --- a/bitchat/Noise/NoiseSessionManager.swift +++ b/bitchat/Noise/NoiseSessionManager.swift @@ -60,23 +60,24 @@ final class NoiseSessionManager { throw NoiseSessionError.alreadyEstablished } - // Remove any existing non-established session - if let existingSession = sessions[peerID], !existingSession.isEstablished() { - _ = sessions.removeValue(forKey: peerID) + let session: NoiseSession + + if let existingSession = sessions[peerID] { + session = existingSession + session.reset() + } else { + // Create new initiator session + session = SecureNoiseSession( + peerID: peerID, + role: .initiator, + keychain: keychain, + localStaticKey: localStaticKey + ) + sessions[peerID] = session } - // Create new initiator session - let session = SecureNoiseSession( - peerID: peerID, - role: .initiator, - keychain: keychain, - localStaticKey: localStaticKey - ) - sessions[peerID] = session - do { - let handshakeData = try session.startHandshake() - return handshakeData + return try session.startHandshake() } catch { // Clean up failed session _ = sessions.removeValue(forKey: peerID)