private void AllIdleTimeoutTask(object state) { var ctx = state as ChannelHandlerContext; // .NET-specific: don't fire if session is already set to timed out if (ctx == null || !ctx.Channel.IsOpen || ctx.IsTimedOut) { return; } long currentTime = Convenient.CurrentTimeMillis(); long lastIoTime = Math.Max(_lastReadTime.Get(), _lastWriteTime.Get()); long nextDelay = (AllIdleTimeMillis - (currentTime - lastIoTime)); if (nextDelay <= 0) { // both reader and writer are idle // --> set a new timeout and notify the callback //Logger.Debug("Both reader and writer are idle..."); _cts = _executor.Schedule(AllIdleTimeoutTask, ctx, AllIdleTimeMillis); try { ctx.FireUserEventTriggered(this); } catch (Exception ex) { ctx.FireExceptionCaught(ex); } } else { // either read or write occurred before the timeout // --> set a new timeout with shorter delayMillis _cts = _executor.Schedule(AllIdleTimeoutTask, ctx, nextDelay); } }
private void Heartbeating(object state) { var ctx = (ChannelHandlerContext)state; if (!ctx.Channel.IsOpen) { return; } var currentTime = Convenient.CurrentTimeMillis(); var lastIoTime = Math.Max(_lastReadTime.Get(), _lastWriteTime.Get()); var nextDelay = TimeToHeartBeatMillis - (currentTime - lastIoTime); if (_peerConnection != null && nextDelay <= 0) { Logger.Debug("Sending heart beat to {0}. Channel: {1}.", _peerConnection.RemotePeer, _peerConnection.Channel); var builder = _pingBuilderFactory.Create(); var taskPing = builder.SetPeerConnection(_peerConnection).Start(); builder.NotifyAutomaticFutures(taskPing); } else { // TODO fix possible NPE Logger.Debug("Not sending heart beat to {0}. Channel: {1}.", _peerConnection.RemotePeer, _peerConnection.Channel); } }