Table of Contents

Class ReconnectionTimer

Namespace
Utils.Common.Network
Assembly
Utils.Common.dll

Repeats connect attempts on a timer after a connection loss, with an interval that grows with the number of attempts (Periods). Each Start() call allows one attempt: the owner calls Start() again after a failed attempt, and a tick that finds the connection established calls Stop(). Start() and Stop() are thread-safe; the timer callbacks, including the connect delegate, run on thread-pool threads.

public class ReconnectionTimer
Inheritance
object
ReconnectionTimer
Extension Methods

Constructors

ReconnectionTimer(ILoggerSource, Func<bool>, Action)

Creates a stopped timer with the default Periods and IsEnabled set.

public ReconnectionTimer(ILoggerSource loggerSource, Func<bool> isConnected, Action connect)

Parameters

loggerSource ILoggerSource

The source the timer writes its log records to.

isConnected Func<bool>

Returns whether the connection is established; checked on every tick.

connect Action

Starts one connect attempt; its exceptions are logged and swallowed.

Exceptions

ArgumentNullException

One of the arguments is null.

Properties

From

The start of the UTC time-of-day window in which attempts are made; ticks outside the window are skipped. When From equals To (both zero by default) there is no window; when From is later than To, the window spans midnight. Both bounds are inclusive.

public TimeSpan From { get; set; }

Property Value

TimeSpan

IsEnabled

Whether Start() starts the reconnection; true by default. When false, Start() only logs. Clearing it does not stop a reconnection that is already running.

public bool IsEnabled { get; set; }

Property Value

bool

IsReconnecting

True from Start() until Stop(), including the waits between attempts.

public bool IsReconnecting { get; }

Property Value

bool

Period

The current interval between ticks; System.TimeSpan.Zero while not reconnecting.

public TimeSpan Period { get; }

Property Value

TimeSpan

Periods

The interval between attempts by the number of attempts made since the last Stop(): an entry applies from its key on. The defaults are 1 second from 0 attempts, 5 seconds from 5 and 30 seconds from 10. The entry used is the last one in enumeration order whose key does not exceed the attempt count, so add the keys in ascending order and keep a key 0; without a matching entry the lookup throws System.InvalidOperationException. Not synchronized: change it before Start().

public IDictionary<int, TimeSpan> Periods { get; }

Property Value

IDictionary<int, TimeSpan>

To

The end of the UTC time-of-day window in which attempts are made; see From.

public TimeSpan To { get; set; }

Property Value

TimeSpan

Methods

Start()

Starts the reconnection or, when it is already running, allows the next attempt. The first call arms the timer with the period for the current attempt count, so the first attempt is made one period later. On each tick: an established connection stops the timer; a tick outside the From-To window is skipped; a tick that changes Period re-arms the timer and is skipped; otherwise the connect delegate is called once and no further attempt is made until the next Start(). Does nothing but log when IsEnabled is false.

public void Start()

Stop()

Stops the reconnection: disposes the timer and resets the attempt count and Period. Does nothing when not reconnecting; does not wait for a connect attempt that is already running.

public void Stop()