diff options
author | Melanie | 2009-10-22 07:12:10 +0100 |
---|---|---|
committer | Melanie | 2009-10-22 07:12:10 +0100 |
commit | c4969d47d9bbc22b37054451cd31451ca8d8c78a (patch) | |
tree | 788e3b034254bcf068ca950ee97a78b6aa07b386 /OpenSim/Region/ClientStack/LindenUDP/TokenBucket.cs | |
parent | Merge branch 'master' into vehicles (diff) | |
parent | Remove the "mel_t" from version string (diff) | |
download | opensim-SC_OLD-c4969d47d9bbc22b37054451cd31451ca8d8c78a.zip opensim-SC_OLD-c4969d47d9bbc22b37054451cd31451ca8d8c78a.tar.gz opensim-SC_OLD-c4969d47d9bbc22b37054451cd31451ca8d8c78a.tar.bz2 opensim-SC_OLD-c4969d47d9bbc22b37054451cd31451ca8d8c78a.tar.xz |
Merge branch 'master' into vehicles
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/TokenBucket.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/LindenUDP/TokenBucket.cs | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/TokenBucket.cs b/OpenSim/Region/ClientStack/LindenUDP/TokenBucket.cs new file mode 100644 index 0000000..bdbd284 --- /dev/null +++ b/OpenSim/Region/ClientStack/LindenUDP/TokenBucket.cs | |||
@@ -0,0 +1,217 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | |||
30 | namespace OpenSim.Region.ClientStack.LindenUDP | ||
31 | { | ||
32 | /// <summary> | ||
33 | /// A hierarchical token bucket for bandwidth throttling. See | ||
34 | /// http://en.wikipedia.org/wiki/Token_bucket for more information | ||
35 | /// </summary> | ||
36 | public class TokenBucket | ||
37 | { | ||
38 | /// <summary>Parent bucket to this bucket, or null if this is a root | ||
39 | /// bucket</summary> | ||
40 | TokenBucket parent; | ||
41 | /// <summary>Size of the bucket in bytes. If zero, the bucket has | ||
42 | /// infinite capacity</summary> | ||
43 | int maxBurst; | ||
44 | /// <summary>Rate that the bucket fills, in bytes per millisecond. If | ||
45 | /// zero, the bucket always remains full</summary> | ||
46 | int tokensPerMS; | ||
47 | /// <summary>Number of tokens currently in the bucket</summary> | ||
48 | int content; | ||
49 | /// <summary>Time of the last drip, in system ticks</summary> | ||
50 | int lastDrip; | ||
51 | |||
52 | #region Properties | ||
53 | |||
54 | /// <summary> | ||
55 | /// The parent bucket of this bucket, or null if this bucket has no | ||
56 | /// parent. The parent bucket will limit the aggregate bandwidth of all | ||
57 | /// of its children buckets | ||
58 | /// </summary> | ||
59 | public TokenBucket Parent | ||
60 | { | ||
61 | get { return parent; } | ||
62 | } | ||
63 | |||
64 | /// <summary> | ||
65 | /// Maximum burst rate in bytes per second. This is the maximum number | ||
66 | /// of tokens that can accumulate in the bucket at any one time | ||
67 | /// </summary> | ||
68 | public int MaxBurst | ||
69 | { | ||
70 | get { return maxBurst; } | ||
71 | set { maxBurst = (value >= 0 ? value : 0); } | ||
72 | } | ||
73 | |||
74 | /// <summary> | ||
75 | /// The speed limit of this bucket in bytes per second. This is the | ||
76 | /// number of tokens that are added to the bucket per second | ||
77 | /// </summary> | ||
78 | /// <remarks>Tokens are added to the bucket any time | ||
79 | /// <seealso cref="RemoveTokens"/> is called, at the granularity of | ||
80 | /// the system tick interval (typically around 15-22ms)</remarks> | ||
81 | public int DripRate | ||
82 | { | ||
83 | get { return tokensPerMS * 1000; } | ||
84 | set | ||
85 | { | ||
86 | if (value == 0) | ||
87 | tokensPerMS = 0; | ||
88 | else | ||
89 | { | ||
90 | int bpms = (int)((float)value / 1000.0f); | ||
91 | |||
92 | if (bpms <= 0) | ||
93 | tokensPerMS = 1; // 1 byte/ms is the minimum granularity | ||
94 | else | ||
95 | tokensPerMS = bpms; | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | |||
100 | /// <summary> | ||
101 | /// The speed limit of this bucket in bytes per millisecond | ||
102 | /// </summary> | ||
103 | public int DripPerMS | ||
104 | { | ||
105 | get { return tokensPerMS; } | ||
106 | } | ||
107 | |||
108 | /// <summary> | ||
109 | /// The number of bytes that can be sent at this moment. This is the | ||
110 | /// current number of tokens in the bucket | ||
111 | /// <remarks>If this bucket has a parent bucket that does not have | ||
112 | /// enough tokens for a request, <seealso cref="RemoveTokens"/> will | ||
113 | /// return false regardless of the content of this bucket</remarks> | ||
114 | /// </summary> | ||
115 | public int Content | ||
116 | { | ||
117 | get { return content; } | ||
118 | } | ||
119 | |||
120 | #endregion Properties | ||
121 | |||
122 | /// <summary> | ||
123 | /// Default constructor | ||
124 | /// </summary> | ||
125 | /// <param name="parent">Parent bucket if this is a child bucket, or | ||
126 | /// null if this is a root bucket</param> | ||
127 | /// <param name="maxBurst">Maximum size of the bucket in bytes, or | ||
128 | /// zero if this bucket has no maximum capacity</param> | ||
129 | /// <param name="dripRate">Rate that the bucket fills, in bytes per | ||
130 | /// second. If zero, the bucket always remains full</param> | ||
131 | public TokenBucket(TokenBucket parent, int maxBurst, int dripRate) | ||
132 | { | ||
133 | this.parent = parent; | ||
134 | MaxBurst = maxBurst; | ||
135 | DripRate = dripRate; | ||
136 | lastDrip = Environment.TickCount & Int32.MaxValue; | ||
137 | } | ||
138 | |||
139 | /// <summary> | ||
140 | /// Remove a given number of tokens from the bucket | ||
141 | /// </summary> | ||
142 | /// <param name="amount">Number of tokens to remove from the bucket</param> | ||
143 | /// <returns>True if the requested number of tokens were removed from | ||
144 | /// the bucket, otherwise false</returns> | ||
145 | public bool RemoveTokens(int amount) | ||
146 | { | ||
147 | bool dummy; | ||
148 | return RemoveTokens(amount, out dummy); | ||
149 | } | ||
150 | |||
151 | /// <summary> | ||
152 | /// Remove a given number of tokens from the bucket | ||
153 | /// </summary> | ||
154 | /// <param name="amount">Number of tokens to remove from the bucket</param> | ||
155 | /// <param name="dripSucceeded">True if tokens were added to the bucket | ||
156 | /// during this call, otherwise false</param> | ||
157 | /// <returns>True if the requested number of tokens were removed from | ||
158 | /// the bucket, otherwise false</returns> | ||
159 | public bool RemoveTokens(int amount, out bool dripSucceeded) | ||
160 | { | ||
161 | if (maxBurst == 0) | ||
162 | { | ||
163 | dripSucceeded = true; | ||
164 | return true; | ||
165 | } | ||
166 | |||
167 | dripSucceeded = Drip(); | ||
168 | |||
169 | if (content - amount >= 0) | ||
170 | { | ||
171 | if (parent != null && !parent.RemoveTokens(amount)) | ||
172 | return false; | ||
173 | |||
174 | content -= amount; | ||
175 | return true; | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | return false; | ||
180 | } | ||
181 | } | ||
182 | |||
183 | /// <summary> | ||
184 | /// Add tokens to the bucket over time. The number of tokens added each | ||
185 | /// call depends on the length of time that has passed since the last | ||
186 | /// call to Drip | ||
187 | /// </summary> | ||
188 | /// <returns>True if tokens were added to the bucket, otherwise false</returns> | ||
189 | public bool Drip() | ||
190 | { | ||
191 | if (tokensPerMS == 0) | ||
192 | { | ||
193 | content = maxBurst; | ||
194 | return true; | ||
195 | } | ||
196 | else | ||
197 | { | ||
198 | int now = Environment.TickCount & Int32.MaxValue; | ||
199 | int deltaMS = now - lastDrip; | ||
200 | |||
201 | if (deltaMS <= 0) | ||
202 | { | ||
203 | if (deltaMS < 0) | ||
204 | lastDrip = now; | ||
205 | return false; | ||
206 | } | ||
207 | |||
208 | int dripAmount = deltaMS * tokensPerMS; | ||
209 | |||
210 | content = Math.Min(content + dripAmount, maxBurst); | ||
211 | lastDrip = now; | ||
212 | |||
213 | return true; | ||
214 | } | ||
215 | } | ||
216 | } | ||
217 | } | ||