123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
-
- using System;
- namespace OpenSim.Region.ClientStack.LindenUDP
- {
-
-
-
-
- public class TokenBucket
- {
-
-
- TokenBucket parent;
-
-
- int maxBurst;
-
-
- int tokensPerMS;
-
- int content;
-
- int lastDrip;
- #region Properties
-
-
-
-
-
- public TokenBucket Parent
- {
- get { return parent; }
- }
-
-
-
-
- public int MaxBurst
- {
- get { return maxBurst; }
- set { maxBurst = (value >= 0 ? value : 0); }
- }
-
-
-
-
-
-
-
- public int DripRate
- {
- get { return tokensPerMS * 1000; }
- set
- {
- if (value == 0)
- tokensPerMS = 0;
- else
- {
- int bpms = (int)((float)value / 1000.0f);
- if (bpms <= 0)
- tokensPerMS = 1;
- else
- tokensPerMS = bpms;
- }
- }
- }
-
-
-
- public int DripPerMS
- {
- get { return tokensPerMS; }
- }
-
-
-
-
-
-
-
- public int Content
- {
- get { return content; }
- }
- #endregion Properties
-
-
-
-
-
-
-
-
-
- public TokenBucket(TokenBucket parent, int maxBurst, int dripRate)
- {
- this.parent = parent;
- MaxBurst = maxBurst;
- DripRate = dripRate;
- lastDrip = Environment.TickCount & Int32.MaxValue;
- }
-
-
-
-
-
-
- public bool RemoveTokens(int amount)
- {
- bool dummy;
- return RemoveTokens(amount, out dummy);
- }
-
-
-
-
-
-
-
-
- public bool RemoveTokens(int amount, out bool dripSucceeded)
- {
- if (maxBurst == 0)
- {
- dripSucceeded = true;
- return true;
- }
- dripSucceeded = Drip();
- if (content - amount >= 0)
- {
- if (parent != null && !parent.RemoveTokens(amount))
- return false;
- content -= amount;
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
-
- public bool Drip()
- {
- if (tokensPerMS == 0)
- {
- content = maxBurst;
- return true;
- }
- else
- {
- int now = Environment.TickCount & Int32.MaxValue;
- int deltaMS = now - lastDrip;
- if (deltaMS <= 0)
- {
- if (deltaMS < 0)
- lastDrip = now;
- return false;
- }
- int dripAmount = deltaMS * tokensPerMS;
- content = Math.Min(content + dripAmount, maxBurst);
- lastDrip = now;
- return true;
- }
- }
- }
- }
|