static void OnHeartBeatTimer(object state) { TimedHeartBeat thisPtr = (TimedHeartBeat)state; if (thisPtr.connection.IsClosing()) { return; } bool wasActive = false; DateTime capturedLastActive = thisPtr.lastSendTime; DateTime now = DateTime.UtcNow; DateTime scheduleAfterTime = now; if (now.Subtract(capturedLastActive) < TimeSpan.FromMilliseconds(thisPtr.heartBeatInterval)) { wasActive = true; scheduleAfterTime = capturedLastActive; } try { if (!wasActive) { thisPtr.connection.SendCommand(null, 0, null); } thisPtr.heartBeatTimer.Change(scheduleAfterTime.AddMilliseconds(thisPtr.heartBeatInterval).Subtract(now), Timeout.InfiniteTimeSpan); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } AmqpTrace.Provider.AmqpLogError(thisPtr.connection, "OnHeartBeatTimer", exception.Message); } // idle timeout can be different for the peers. but instead of creating another timer, // we use the sending timer to also check if we have received anything just in case uint thisIdletimeout = thisPtr.connection.Settings.IdleTimeOut(); if (thisIdletimeout < uint.MaxValue && now > thisPtr.lastReceiveTime.AddMilliseconds((int)thisIdletimeout)) { AmqpTrace.Provider.AmqpLogError(thisPtr.connection, "OnHeartBeatTimer", AmqpResources.GetString(AmqpResources.AmqpTimeout, thisIdletimeout, thisPtr.connection)); thisPtr.connection.SafeClose(new AmqpException(AmqpErrorCode.ConnectionForced, AmqpResources.AmqpConnectionInactive)); } }
static void OnHeartBeatTimer(object state) { TimedHeartBeat thisPtr = (TimedHeartBeat)state; if (thisPtr.connection.IsClosing()) { return; } DateTime now = DateTime.UtcNow; try { if (thisPtr.localInterval < uint.MaxValue && now.Subtract(thisPtr.lastReceiveTime).TotalMilliseconds > thisPtr.localInterval) { string message = AmqpResources.GetString(AmqpResources.AmqpConnectionInactive, thisPtr.localInterval, thisPtr.connection.Settings.ContainerId); AmqpTrace.Provider.AmqpLogError(thisPtr.connection, "OnHeartBeatTimer", message); thisPtr.connection.SafeClose(new AmqpException(AmqpErrorCode.ConnectionForced, message)); return; } if (thisPtr.remoteInterval < uint.MaxValue && now.Subtract(thisPtr.lastSendTime).TotalMilliseconds >= thisPtr.remoteInterval) { thisPtr.connection.SendCommand(null, 0, null); } thisPtr.SetTimer(now); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } if (!thisPtr.connection.IsClosing()) { AmqpTrace.Provider.AmqpLogError(thisPtr.connection, "OnHeartBeatTimer", exception.Message); thisPtr.connection.SafeClose(exception); } } }