Example #1
0
        public static void EndOfStreamException_Ctor_String_Exception()
        {
            Exception ex = new Exception(innerExceptionMessage);
            EndOfStreamException i = new EndOfStreamException(exceptionMessage, ex);

            Assert.Equal(exceptionMessage, i.Message);
            Assert.Equal(i.InnerException.Message, innerExceptionMessage);
            Assert.Equal(i.InnerException.HResult, ex.HResult);
            Assert.Equal(COR_E_ENDOFSTREAM, (uint)i.HResult);
        }
        public void HeartbeatReadLoop()
        {
            while (!m_closed)
            {
                if (!m_heartbeatRead.WaitOne(Heartbeat * 1000, false))
                    m_missedHeartbeats++;
                else
                    m_missedHeartbeats = 0;

                // Has to miss two full heartbeats to force socket close
                if (m_missedHeartbeats > 1)
                {
                    String description = "Heartbeat missing with heartbeat == " +
                                          m_heartbeat + " seconds";
                    EndOfStreamException eose = new EndOfStreamException(description);
                    m_shutdownReport.Add(new ShutdownReportEntry(description, eose));
                    HandleMainLoopException(new ShutdownEventArgs(
                                                          ShutdownInitiator.Library,
                                                          0,
                                                          "End of stream",
                                                          eose));
                    break;
                }
            }

            TerminateMainloop();
            FinishClose();
        }
        public void HeartbeatReadTimerCallback(object state)
        {
            bool shouldTerminate = false;
            try
            {
                if (!m_closed)
                {
                    if (!m_heartbeatRead.WaitOne(0))
                    {
                        m_missedHeartbeats++;
                    }
                    else
                    {
                        m_missedHeartbeats = 0;
                    }

                    // We check against 8 = 2 * 4 because we need to wait for at
                    // least two complete heartbeat setting intervals before
                    // complaining, and we've set the socket timeout to a quarter
                    // of the heartbeat setting in setHeartbeat above.
                    if (m_missedHeartbeats > 2 * 4)
                    {
                        String description = String.Format("Heartbeat missing with heartbeat == {0} seconds", m_heartbeat);
                        var eose = new EndOfStreamException(description);
                        ESLog.Error(description, eose);
                        m_shutdownReport.Add(new ShutdownReportEntry(description, eose));
                        HandleMainLoopException(
                            new ShutdownEventArgs(ShutdownInitiator.Library, 0, "End of stream", eose));
                        shouldTerminate = true;
                    }
                }

                if (shouldTerminate)
                {
                    TerminateMainloop();
                    FinishClose();
                }
                else if(_heartbeatReadTimer != null)
                {
                    _heartbeatReadTimer.Change(Heartbeat * 1000, Timeout.Infinite);
                }
            }
            catch (ObjectDisposedException)
            {
                // timer is already disposed,
                // e.g. due to shutdown
            }
            catch (NullReferenceException)
            {
                // timer has already been disposed from a different thread after null check
                // this event should be rare
            }
        }
        ///<summary>Closes this Subscription, cancelling the consumer
        ///record in the server.</summary>
        public void Close()
        {
            try
            {
                bool shouldCancelConsumer = false;
                if (m_consumer != null)
                {
                    shouldCancelConsumer = m_consumer.IsRunning;
                    m_consumer = null;
                }

                if (shouldCancelConsumer)
                {
                    if (Model.IsOpen)
                    {
                        Model.BasicCancel(ConsumerTag);
                    }

                    ConsumerTag = null;
                }

                m_queueCts.Cancel(true);
                if(m_queue != null)
                {
                    m_queue.Dispose();
                    m_queue = null;
                }
            #if NETFX_CORE || NET4
                var exn = new EndOfStreamException("Subscription closed");
                foreach (var tsc in m_waiting)
                {
                    tsc.TrySetException(exn);
                }
            #endif
            }
            catch (OperationInterruptedException)
            {
                // We don't mind, here.
            }
        }
Example #5
0
 private static EmberException CreateEmberException(EndOfStreamException ex) =>
     new EmberException("Unexpected end of stream.", ex);
        public void HeartbeatReadTimerCallback(object state)
        {
            bool shouldTerminate = false;
            if (!m_closed)
            {
                if (!m_heartbeatRead.WaitOne(0))
                {
                    m_missedHeartbeats++;
                }
                else
                {
                    m_missedHeartbeats = 0;
                }

                // We check against 8 = 2 * 4 because we need to wait for at
                // least two complete heartbeat setting intervals before
                // complaining, and we've set the socket timeout to a quarter
                // of the heartbeat setting in setHeartbeat above.
                if (m_missedHeartbeats > 2 * 4)
                {
                    String description = String.Format("Heartbeat missing with heartbeat == {0} seconds", m_heartbeat);
                    var eose = new EndOfStreamException(description);
                    m_shutdownReport.Add(new ShutdownReportEntry(description, eose));
                    HandleMainLoopException(
                        new ShutdownEventArgs(ShutdownInitiator.Library, 0, "End of stream", eose));
                    shouldTerminate = true;
                }
             }

            if (shouldTerminate)
            {
                TerminateMainloop();
                FinishClose();
            }
            else
            {
                _heartbeatReadTimer.Change(Heartbeat * 1000, Timeout.Infinite);
            }
        }
Example #7
0
 public static void EndOfStreamException_Ctor_String()
 {
     EndOfStreamException i = new EndOfStreamException(exceptionMessage);
     Assert.Equal(exceptionMessage, i.Message);
     Assert.Equal(COR_E_ENDOFSTREAM, (uint)i.HResult);
 }
Example #8
0
 public static void EndOfStreamException_Ctor_Empty()
 {
     EndOfStreamException i = new EndOfStreamException();
     Assert.Equal(COR_E_ENDOFSTREAM, (uint)i.HResult);
 }
Example #9
0
        /// <summary>
        /// Objects of this class spend most of their execution time in this method.  It simply waits asynchronously (control
        /// is returned to the constructor and then returned here on an interrupt) for data.  When data is read it is packaged
        /// and sent to the event dispatcher.  
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ReceiverExceptions">Thrown when the EOF is reached on the read stream (contains an 
        /// EndOfStreamException</exception>
        public async Task run()
        {
            while (goState > 0)
            {
                var ret = string.Empty;
                var buffer = new char[1]; // Not the most efficient...
                while (!ret.Contains("\n") && (!ret.Contains("\r")))
                {
                    var charsRead = await textReader.ReadAsync(buffer, 0, 1);
                    if (charsRead == 0)
                    {
                        EndOfStreamException eose = new EndOfStreamException();
                        ReceiverExceptions re = new ReceiverExceptions(this,"End of stream reached on serial port.",true,eose);
                        dispatcher.enqueueEvent(new RealTimeEvents.ExcepReceiver(re, re.fatal,
                            this, this.portName, this.VEMCO_SerialNumber, this.VEMCO_Model, this.encoder.encoderConfig));
                        throw re;
                    }
                    ret += buffer[0];
                }

                if (ret.Length > 1)
                {
                    dispatcher.enqueueEvent(new RealTimeEvents.UnparsedMessage(ret,
                        this, this.portName, this.VEMCO_SerialNumber, this.VEMCO_Model, this.encoder.encoderConfig));
                }
            }
            goState = -1;
        }
 public void FinishSend(Exception exception)
 {
     if (exception == null && !_clientClosedConnection)
     {
         var tcs = _tcsSend;
         _tcsSend = null;
         if (tcs != null)
         {
             tcs.SetResult(true);
         }
         else if (_isWebSocket)
         {
             _isWebSocket = false;
             _next.UpgradedToWebSocket(true);
         }
     }
     else
     {
         var tcs = _tcsSend;
         _tcsSend = null;
         _isKeepAlive = false;
         if (tcs != null)
         {
             if (exception == null) exception = new EndOfStreamException("Client closed connection");
             tcs.SetException(exception);
         }
         else if (_isWebSocket)
         {
             _isWebSocket = false;
             _next.UpgradedToWebSocket(false);
         }
     }
     if (_lastPacket)
     {
         _lastPacket = false;
         if (_isKeepAlive && !_clientClosedConnection)
         {
             ResetForNextRequest();
             StartNextReceive();
         }
         else
         {
             CloseConnection();
         }
     }
 }
        protected void HandleIOException(Exception e)
        {
            // socket error when in negotiation, throw BrokerUnreachableException
            // immediately
            if (m_inConnectionNegotiation)
            {
                var cfe = new ConnectFailureException("I/O error before connection negotiation was completed", e);
                throw new BrokerUnreachableException(cfe);
            }

            if (++m_missedHeartbeats >= SOCKET_TIMEOUTS_TO_CONSIDER_PEER_UNRESPONSIVE)
            {
                var description =
                    String.Format("Peer missed 2 heartbeats with heartbeat timeout set to {0} seconds",
                                  m_heartbeat);
                var eose = new EndOfStreamException(description);
                m_shutdownReport.Add(new ShutdownReportEntry(description, eose));
                HandleMainLoopException(new ShutdownEventArgs(ShutdownInitiator.Library,
                                                              0,
                                                              "End of stream",
                                                              eose));
                TerminateMainloop();
                FinishClose();
            }
        }