Ejemplo n.º 1
0
        /// <summary>
        /// Loop to receive packets. Use e
        /// The method exits when readLimit is reached or when an
        /// outbound flow completes. This
        ///
        /// Throws MqttTimeoutException if keepalive period expires.
        /// </summary>
        /// <param name="readLimit">Read limit in milliseconds.</param>
        /// <returns>Returns true if is connected, false otherwise.</returns>
        public bool Loop(int readLimit)
        {
            if (readLimit < 0)
            {
                throw new ArgumentException("Poll limit should be positive");
            }

            // Loop shouldn't be called concurrently or recursivelly
            if (Interlocked.CompareExchange(ref inLoop, 1, 0) == 1)
            {
                throw new InvalidProgramException("Loop is already running");
            }

            try {
                int readThreshold = Environment.TickCount + readLimit;
                int pollTime;

                InterruptLoop = false;

                while ((pollTime = readThreshold - Environment.TickCount) > 0 && !InterruptLoop)
                {
                    if (Transport.IsClosed)
                    {
                        return(false);
                    }
                    else if (Transport.Poll(pollTime))
                    {
                        ReceivePacket();
                    }
                    else if (WriteWaitExpired)
                    {
                        Send(new PingreqPacket());
                    }
                    else if (ReadWaitExpired)
                    {
                        throw new MqttTimeoutException();
                    }
                }

                return(!Transport.IsClosed);
            } finally {
                inLoop = 0;
            }
        }
Ejemplo n.º 2
0
 bool Poll(int pollTime)
 {
     return(Transport.Poll(Math.Min(pollTime, (int)MaxPollTime.TotalMilliseconds)));
 }