Ejemplo n.º 1
0
        /// <summary>
        /// Called when a subscription packet comes, update the count of a face.
        ///
        /// if count == Int.MaxValue, it means inf.
        /// </summary>
        /// <param name="face">The specified face</param>
        /// <param name="count">The count of that face, Int32.MaxValue for infinitive</param>
        /// <param name="CountInQueue">A count function that will calculate the packets with the same CD in the queue</param>
        /// <returns>-1 if no change on that CD, </returns>
        public int UpdateFace(IPEndPoint face, int count, Func <int> CountInQueue, out int realFaceCount)
        {
            // To obsolate count = -1
            Trace.Assert(count >= 0);

            FaceCounter counter;

            if (!Faces.TryGetValue(face, out counter))
            {
                Faces [face] = counter = new FaceCounter();
            }

            if (counter.Count == Int32.MaxValue)
            {
                if (count != Int32.MaxValue)
                {
                    // Modified here, if the subscription changed from inf. to a certain value, use count - N as new count
                    counter.Count = count - CountInQueue();
                }
                //else do nothing
            }
            else
            {
                if (count == Int32.MaxValue)
                {
                    counter.Count = Int32.MaxValue;
                }
                else
                {
                    counter.Count += count;
                }
            }
            realFaceCount = counter.Count;

            int newMax = CalculateNewMax();

            if (newMax == Int32.MaxValue)
            {
                if (Max == Int32.MaxValue)
                {
                    return(-1);
                }
                else
                {
                    return(Max = Int32.MaxValue);
                }
            }
            else
            {
                if (Max == Int32.MaxValue)
                {
                    return(Max = newMax);
                }
                else
                {
                    int diff = newMax - Max;
                    Max = newMax;
                    return(diff);
                }
            }
        }