11// SPDX-FileCopyrightText: 2025 Guus Kuiper
2- //
2+ //
33// SPDX-License-Identifier: MIT
44
55using System . Buffers ;
@@ -18,14 +18,22 @@ public class StoreInfo
1818 /// Gets or sets the last known sequence number for the store.
1919 /// </summary>
2020 public Seq Seq { get ; set ; }
21+
2122 /// <summary>
2223 /// Gets or sets the outgoing ID for the store in the connection.
24+ /// The value 0 indicates not connected.
2325 /// </summary>
2426 public Id IdOut { get ; set ; }
27+
2528 /// <summary>
2629 /// Gets or sets a value indicating whether this store is a source in the connection.
2730 /// </summary>
2831 public bool Source { get ; set ; }
32+
33+ /// <summary>
34+ ///
35+ /// </summary>
36+ public bool IsConnected => IdOut != 0 ;
2937}
3038
3139/// <summary>
@@ -133,7 +141,7 @@ public Seq Process(StoreJournal store)
133141 return 0 ;
134142 }
135143
136- if ( ! store . HasChanged ( info . Seq ) )
144+ if ( ! info . IsConnected || ! store . HasChanged ( info . Seq ) )
137145 {
138146 // No recent changes
139147 return 0 ;
@@ -209,6 +217,7 @@ public override void Decode(Span<byte> buffer)
209217 } ;
210218 _stores [ journal ] = info ;
211219
220+ Debug . Assert ( info . IsConnected ) ;
212221 id = NextId ( ) ;
213222 _idIn [ id ] = journal ;
214223 EncodeId ( id ) ;
@@ -246,6 +255,7 @@ public override void Decode(Span<byte> buffer)
246255 info . Seq = seq ;
247256 info . IdOut = welcomeId ;
248257 Debug . Assert ( info . Source ) ;
258+ Debug . Assert ( info . IsConnected ) ;
249259
250260 break ;
251261 }
@@ -300,7 +310,7 @@ public override void Decode(Span<byte> buffer)
300310 }
301311
302312 StoreInfo info = _stores [ value ] ;
303- if ( info . Source && info . IdOut == 0 )
313+ if ( info . Source && info . IsConnected )
304314 {
305315 HelloAgain ( value ) ;
306316 }
@@ -323,7 +333,7 @@ public override void Decode(Span<byte> buffer)
323333 break ;
324334 }
325335
326- if ( info . Source && info . IdOut == 0 )
336+ if ( info . Source && info . IsConnected )
327337 {
328338 HelloAgain ( journal ) ;
329339 }
@@ -349,6 +359,13 @@ public override void Decode(Span<byte> buffer)
349359 /// <returns>True if the store is being synchronized; otherwise, false.</returns>
350360 public bool IsSynchronizing ( StoreJournal journal ) => _stores . ContainsKey ( journal ) ;
351361
362+ /// <summary>
363+ /// Returns if the given store is currently connected over this connection.
364+ /// </summary>
365+ /// <param name="journal"></param>
366+ /// <returns></returns>
367+ public bool IsConnected ( StoreJournal journal ) => _stores . TryGetValue ( journal , out StoreInfo ? info ) && info . IsConnected ;
368+
352369 /// <summary>
353370 /// Resets the connection, drops all non-source stores, and sends Hello messages for sources.
354371 /// </summary>
@@ -361,6 +378,20 @@ public override void Reset()
361378 HelloAgain ( ) ;
362379 }
363380
381+ /// <inheritdoc />
382+ public override void Connected ( )
383+ {
384+ HelloAgain ( ) ;
385+ base . Connected ( ) ;
386+ }
387+
388+ /// <inheritdoc />
389+ public override void Disconnected ( )
390+ {
391+ DropNonSources ( ) ;
392+ base . Disconnected ( ) ;
393+ }
394+
364395 /// <summary>
365396 /// Encode a Bye and drop all stores.
366397 /// </summary>
@@ -372,7 +403,7 @@ protected void SendBye()
372403 }
373404
374405 /// <summary>
375- ///
406+ ///
376407 /// </summary>
377408 /// <param name="hash"></param>
378409 protected void SendBye ( string hash )
@@ -572,6 +603,7 @@ private void HelloAgain(StoreJournal store)
572603 Id id = _idIn . FirstOrDefault ( x => x . Value == store ) . Key ;
573604
574605 Debug . Assert ( id > 0 ) ;
606+ Debug . Assert ( ! info . IsConnected ) ;
575607
576608 EncodeCmd ( SyncConnection . Hello ) ;
577609 store . EncodeHash ( this , false ) ;
0 commit comments