Esempio n. 1
0
 public IotHubConnection(IotHubConnectionString connectionString, AccessRights accessRights)
 {
     this.connectionString     = connectionString;
     this.accessRights         = accessRights;
     this.faultTolerantSession = new FaultTolerantAmqpObject <AmqpSession>(this.CreateSessionAsync, this.CloseConnection);
     this.refreshTokenTimer    = new IOThreadTimer(s => ((IotHubConnection)s).OnRefreshToken(), this, false);
 }
Esempio n. 2
0
 public IotHubConnection(IotHubConnectionString connectionString, AccessRights accessRights)
 {
     this.connectionString = connectionString;
     this.accessRights = accessRights;
     this.faultTolerantSession = new FaultTolerantAmqpObject<AmqpSession>(this.CreateSessionAsync, this.CloseConnection);
     this.refreshTokenTimer = new IOThreadTimer(s => ((IotHubConnection)s).OnRefreshToken(), this, false);
 }
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer.index;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= this.count, "");

                IOThreadTimer[] tempTimers = this.timers;

                for (; ;)
                {
                    int parentIndex = index / 2;

                    if (parentIndex >= 1)
                    {
                        IOThreadTimer parentTimer = tempTimers[parentIndex];
                        tempTimers[index] = parentTimer;
                        parentTimer.index = index;
                    }
                    else
                    {
                        break;
                    }

                    index = parentIndex;
                }

                timer.index   = 0;
                timer.dueTime = 0;
                tempTimers[1] = null;
                DeleteMinTimerCore();
            }
            public bool UpdateTimer(IOThreadTimer timer, long newDueTime)
            {
                int index = timer.index;

                IOThreadTimer[] tempTimers = this.timers;
                int             tempCount  = this.count;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= tempCount, "");

                int parentIndex = index / 2;

                if (parentIndex == 0 ||
                    tempTimers[parentIndex].dueTime <= newDueTime)
                {
                    int leftChildIndex = index * 2;
                    if (leftChildIndex > tempCount ||
                        tempTimers[leftChildIndex].dueTime >= newDueTime)
                    {
                        int rightChildIndex = leftChildIndex + 1;
                        if (rightChildIndex > tempCount ||
                            tempTimers[rightChildIndex].dueTime >= newDueTime)
                        {
                            timer.dueTime = newDueTime;
                            return(index == 1);
                        }
                    }
                }

                DeleteTimer(timer);
                InsertTimer(timer, newDueTime);
                return(true);
            }
            public void DeleteMinTimer()
            {
                IOThreadTimer minTimer = this.MinTimer;

                DeleteMinTimerCore();
                minTimer.index   = 0;
                minTimer.dueTime = 0;
            }
Esempio n. 6
0
            public void DeleteMinTimer()
            {
                IOThreadTimer minTimer = MinTimer;

                DeleteMinTimerCore();
                minTimer._index   = 0;
                minTimer._dueTime = 0;
            }
Esempio n. 7
0
 public CachedConnection(IotHubConnectionCache cache, IotHubConnection connection)
 {
     this.cache      = cache;
     this.Connection = connection;
     this.ThisLock   = new object();
     this.idleTimer  = new IOThreadTimer(s => ((CachedConnection)s).IdleTimerCallback(), this, false);
     this.idleTimer.Set(this.cache.IdleTimeout);
 }
        /*
         * Only called under this.thisLock
         */
        void StartIdleConnectionTimer(IotHubDeviceMuxConnection iotHubDeviceMuxConnection)
        {
#if WINDOWS_UWP
            var idleTimer = new IOThreadTimerSlim(this.IdleConnectionTimerCallback, iotHubDeviceMuxConnection, false);
#else
            var idleTimer = new IOThreadTimer(this.IdleConnectionTimerCallback, iotHubDeviceMuxConnection, false);
#endif
            this.idleTimers.Add(iotHubDeviceMuxConnection, idleTimer);
            idleTimer.Set(this.idleTimeout);
        }
            static void UpdateWaitableTimer(TimerGroup timerGroup)
            {
                WaitableTimer waitableTimer = timerGroup.WaitableTimer;
                IOThreadTimer minTimer      = timerGroup.TimerQueue.MinTimer;
                long          timeDiff      = waitableTimer.DueTime - minTimer.dueTime;

                if (timeDiff < 0)
                {
                    timeDiff = -timeDiff;
                }
                if (timeDiff > minTimer.maxSkew)
                {
                    waitableTimer.Set(minTimer.dueTime);
                }
            }
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                Fx.Assert(timer.index == 0, "Timer should not have an index.");

                IOThreadTimer[] tempTimers = this.timers;

                int index = this.count + 1;

                if (index == tempTimers.Length)
                {
                    tempTimers = new IOThreadTimer[tempTimers.Length * 2];
                    Array.Copy(this.timers, tempTimers, this.timers.Length);
                    this.timers = tempTimers;
                }

                this.count = index;

                if (index > 1)
                {
                    for (; ;)
                    {
                        int parentIndex = index / 2;

                        if (parentIndex == 0)
                        {
                            break;
                        }

                        IOThreadTimer parent = tempTimers[parentIndex];

                        if (parent.dueTime > dueTime)
                        {
                            tempTimers[index] = parent;
                            parent.index      = index;
                            index             = parentIndex;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                tempTimers[index] = timer;
                timer.index       = index;
                timer.dueTime     = dueTime;
                return(index == 1);
            }
            static void ScheduleElapsedTimers(TimerGroup timerGroup, long now)
            {
                TimerQueue timerQueue = timerGroup.TimerQueue;

                while (timerQueue.Count > 0)
                {
                    IOThreadTimer timer    = timerQueue.MinTimer;
                    long          timeDiff = timer.dueTime - now;
                    if (timeDiff <= timer.maxSkew)
                    {
                        timerQueue.DeleteMinTimer();
                        ActionItem.Schedule(timer.callback, timer.callbackState);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            public bool Cancel(IOThreadTimer timer)
            {
                lock (ThisLock)
                {
                    if (timer.index > 0)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        timerQueue.DeleteTimer(timer);

                        if (timerQueue.Count > 0)
                        {
                            UpdateWaitableTimer(timerGroup);
                        }
                        else
                        {
                            TimerGroup otherTimerGroup = GetOtherTimerGroup(timerGroup);
                            if (otherTimerGroup.TimerQueue.Count == 0)
                            {
                                long now = Ticks.Now;
                                long thisGroupRemainingTime  = timerGroup.WaitableTimer.DueTime - now;
                                long otherGroupRemainingTime = otherTimerGroup.WaitableTimer.DueTime - now;
                                if (thisGroupRemainingTime > maxTimeToWaitForMoreTimers &&
                                    otherGroupRemainingTime > maxTimeToWaitForMoreTimers)
                                {
                                    timerGroup.WaitableTimer.Set(Ticks.Add(now, maxTimeToWaitForMoreTimers));
                                }
                            }
                        }

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            public void Set(IOThreadTimer timer, long dueTime)
            {
                long timeDiff = dueTime - timer.dueTime;

                if (timeDiff < 0)
                {
                    timeDiff = -timeDiff;
                }

                if (timeDiff > timer.maxSkew)
                {
                    lock (ThisLock)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        if (timer.index > 0)
                        {
                            if (timerQueue.UpdateTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);
                            }
                        }
                        else
                        {
                            if (timerQueue.InsertTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);

                                if (timerQueue.Count == 1)
                                {
                                    EnsureWaitScheduled();
                                }
                            }
                        }
                    }
                }
            }
Esempio n. 14
0
            public void Set(IOThreadTimer timer, long dueTime)
            {
                long timeDiff = dueTime - timer.dueTime;
                if (timeDiff < 0)
                {
                    timeDiff = -timeDiff;
                }

                if (timeDiff > timer.maxSkew)
                {
                    lock (ThisLock)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        if (timer.index > 0)
                        {
                            if (timerQueue.UpdateTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);
                            }
                        }
                        else
                        {
                            if (timerQueue.InsertTimer(timer, dueTime))
                            {
                                UpdateWaitableTimer(timerGroup);

                                if (timerQueue.Count == 1)
                                {
                                    EnsureWaitScheduled();
                                }
                            }
                        }
                    }
                }
            }
            void DeleteMinTimerCore()
            {
                int currentCount = this.count;

                if (currentCount == 1)
                {
                    this.count     = 0;
                    this.timers[1] = null;
                }
                else
                {
                    IOThreadTimer[] tempTimers = this.timers;
                    IOThreadTimer   lastTimer  = tempTimers[currentCount];
                    this.count = --currentCount;

                    int index = 1;
                    for (; ;)
                    {
                        int leftChildIndex = index * 2;

                        if (leftChildIndex > currentCount)
                        {
                            break;
                        }

                        int           childIndex;
                        IOThreadTimer child;

                        if (leftChildIndex < currentCount)
                        {
                            IOThreadTimer leftChild       = tempTimers[leftChildIndex];
                            int           rightChildIndex = leftChildIndex + 1;
                            IOThreadTimer rightChild      = tempTimers[rightChildIndex];

                            if (rightChild.dueTime < leftChild.dueTime)
                            {
                                child      = rightChild;
                                childIndex = rightChildIndex;
                            }
                            else
                            {
                                child      = leftChild;
                                childIndex = leftChildIndex;
                            }
                        }
                        else
                        {
                            childIndex = leftChildIndex;
                            child      = tempTimers[childIndex];
                        }

                        if (lastTimer.dueTime > child.dueTime)
                        {
                            tempTimers[index] = child;
                            child.index       = index;
                        }
                        else
                        {
                            break;
                        }

                        index = childIndex;

                        if (leftChildIndex >= currentCount)
                        {
                            break;
                        }
                    }

                    tempTimers[index]            = lastTimer;
                    lastTimer.index              = index;
                    tempTimers[currentCount + 1] = null;
                }
            }
Esempio n. 16
0
            public void DeleteTimer(IOThreadTimer timer)
            {
                int index = timer.index;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= this.count, "");

                IOThreadTimer[] tempTimers = this.timers;

                for (; ; )
                {
                    int parentIndex = index / 2;

                    if (parentIndex >= 1)
                    {
                        IOThreadTimer parentTimer = tempTimers[parentIndex];
                        tempTimers[index] = parentTimer;
                        parentTimer.index = index;
                    }
                    else
                    {
                        break;
                    }

                    index = parentIndex;
                }

                timer.index = 0;
                timer.dueTime = 0;
                tempTimers[1] = null;
                DeleteMinTimerCore();
            }
Esempio n. 17
0
            public bool InsertTimer(IOThreadTimer timer, long dueTime)
            {
                Fx.Assert(timer.index == 0, "Timer should not have an index.");

                IOThreadTimer[] tempTimers = this.timers;

                int index = this.count + 1;

                if (index == tempTimers.Length)
                {
                    tempTimers = new IOThreadTimer[tempTimers.Length * 2];
                    Array.Copy(this.timers, tempTimers, this.timers.Length);
                    this.timers = tempTimers;
                }

                this.count = index;

                if (index > 1)
                {
                    for (; ; )
                    {
                        int parentIndex = index / 2;

                        if (parentIndex == 0)
                        {
                            break;
                        }

                        IOThreadTimer parent = tempTimers[parentIndex];

                        if (parent.dueTime > dueTime)
                        {
                            tempTimers[index] = parent;
                            parent.index = index;
                            index = parentIndex;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                tempTimers[index] = timer;
                timer.index = index;
                timer.dueTime = dueTime;
                return index == 1;
            }
Esempio n. 18
0
            public bool UpdateTimer(IOThreadTimer timer, long newDueTime)
            {
                int index = timer.index;

                IOThreadTimer[] tempTimers = this.timers;
                int tempCount = this.count;

                Fx.Assert(index > 0, "");
                Fx.Assert(index <= tempCount, "");

                int parentIndex = index / 2;
                if (parentIndex == 0 ||
                    tempTimers[parentIndex].dueTime <= newDueTime)
                {
                    int leftChildIndex = index * 2;
                    if (leftChildIndex > tempCount ||
                        tempTimers[leftChildIndex].dueTime >= newDueTime)
                    {
                        int rightChildIndex = leftChildIndex + 1;
                        if (rightChildIndex > tempCount ||
                            tempTimers[rightChildIndex].dueTime >= newDueTime)
                        {
                            timer.dueTime = newDueTime;
                            return index == 1;
                        }
                    }
                }

                DeleteTimer(timer);
                InsertTimer(timer, newDueTime);
                return true;
            }
Esempio n. 19
0
            public bool Cancel(IOThreadTimer timer)
            {
                lock (ThisLock)
                {
                    if (timer.index > 0)
                    {
                        TimerGroup timerGroup = timer.timerGroup;
                        TimerQueue timerQueue = timerGroup.TimerQueue;

                        timerQueue.DeleteTimer(timer);

                        if (timerQueue.Count > 0)
                        {
                            UpdateWaitableTimer(timerGroup);
                        }
                        else
                        {
                            TimerGroup otherTimerGroup = GetOtherTimerGroup(timerGroup);
                            if (otherTimerGroup.TimerQueue.Count == 0)
                            {
                                long now = Ticks.Now;
                                long thisGroupRemainingTime = timerGroup.WaitableTimer.DueTime - now;
                                long otherGroupRemainingTime = otherTimerGroup.WaitableTimer.DueTime - now;
                                if (thisGroupRemainingTime > maxTimeToWaitForMoreTimers &&
                                    otherGroupRemainingTime > maxTimeToWaitForMoreTimers)
                                {
                                    timerGroup.WaitableTimer.Set(Ticks.Add(now, maxTimeToWaitForMoreTimers));
                                }
                            }
                        }

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }