commit 832f6bcf7fda65054a553aeeaf569d493030bf85 Author: David Fifield david@bamsoftware.com Date: Sat Jun 11 14:03:08 2011 -0700
Move the rate limit back into swfcat from ProxyPair.
It should be global, not per-ProxyPair. Also uniformity with master. --- ProxyPair.as | 117 ++-------------------------------------------------------- swfcat.as | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 113 deletions(-)
diff --git a/ProxyPair.as b/ProxyPair.as index 6ca60ef..fa51933 100644 --- a/ProxyPair.as +++ b/ProxyPair.as @@ -30,13 +30,6 @@ package private var relay_socket:Socket; private var r2c_schedule:Array;
- // Bytes per second. Set to undefined to disable limit. - private const RATE_LIMIT:Number = undefined; //10000; - // Seconds. - private const RATE_LIMIT_HISTORY:Number = 5.0; - - private var rate_limit:RateLimit; - // Callback id. private var flush_id:uint;
@@ -51,11 +44,6 @@ package this.c2r_schedule = new Array(); this.r2c_schedule = new Array();
- if (RATE_LIMIT) - rate_limit = new BucketRateLimit(RATE_LIMIT * RATE_LIMIT_HISTORY, RATE_LIMIT_HISTORY); - else - rate_limit = new RateUnlimit(); - setup_relay_socket();
/* client_socket setup should be taken */ @@ -147,25 +135,25 @@ package /* Can't do anything until connected. */ return;
- while (!rate_limit.is_limited() && (c2r_schedule.length > 0 || r2c_schedule.length > 0)) { + while (!ui.rate_limit.is_limited() && (c2r_schedule.length > 0 || r2c_schedule.length > 0)) { var num_bytes:uint;
if (c2r_schedule.length > 0) { num_bytes = c2r_schedule.shift(); transfer_bytes(null, relay_socket, num_bytes); - rate_limit.update(num_bytes); + ui.rate_limit.update(num_bytes); }
if (r2c_schedule.length > 0) { num_bytes = r2c_schedule.shift(); transfer_bytes(relay_socket, null, num_bytes); - rate_limit.update(num_bytes); + ui.rate_limit.update(num_bytes); } }
/* Call again when safe, if necessary. */ if (c2r_schedule.length > 0 || r2c_schedule.length > 0) - flush_id = setTimeout(flush, rate_limit.when() * 1000); + flush_id = setTimeout(flush, ui.rate_limit.when() * 1000); }
/* Helper function to write output to the @@ -177,100 +165,3 @@ package } } } - -import flash.utils.getTimer; - -class RateLimit -{ - public function RateLimit() - { - } - - public function update(n:Number):Boolean - { - return true; - } - - public function when():Number - { - return 0.0; - } - - public function is_limited():Boolean - { - return false; - } -} - -class RateUnlimit extends RateLimit -{ - public function RateUnlimit() - { - } - - public override function update(n:Number):Boolean - { - return true; - } - - public override function when():Number - { - return 0.0; - } - - public override function is_limited():Boolean - { - return false; - } -} - -class BucketRateLimit extends RateLimit -{ - private var amount:Number; - private var capacity:Number; - private var time:Number; - private var last_update:uint; - - public function BucketRateLimit(capacity:Number, time:Number) - { - this.amount = 0.0; - /* capacity / time is the rate we are aiming for. */ - this.capacity = capacity; - this.time = time; - this.last_update = getTimer(); - } - - private function age():void - { - var now:uint; - var delta:Number; - - now = getTimer(); - delta = (now - last_update) / 1000.0; - last_update = now; - - amount -= delta * capacity / time; - if (amount < 0.0) - amount = 0.0; - } - - public override function update(n:Number):Boolean - { - age(); - amount += n; - - return amount <= capacity; - } - - public override function when():Number - { - age(); - return (amount - capacity) / (capacity / time); - } - - public override function is_limited():Boolean - { - age(); - return amount > capacity; - } -} diff --git a/swfcat.as b/swfcat.as index e75ffac..42bcfea 100644 --- a/swfcat.as +++ b/swfcat.as @@ -41,6 +41,11 @@ package // Milliseconds. private const FACILITATOR_POLL_INTERVAL:int = 10000;
+ // Bytes per second. Set to undefined to disable limit. + public const RATE_LIMIT:Number = undefined; + // Seconds. + private const RATE_LIMIT_HISTORY:Number = 5.0; + // Socket to Cirrus server private var s_c:CirrusSocket; // Socket to facilitator. @@ -65,6 +70,8 @@ package private var fac_addr:Object; private var relay_addr:Object;
+ public var rate_limit:RateLimit; + public function swfcat() { proxy_mode = false; @@ -73,6 +80,11 @@ package // Absolute positioning. stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; + + if (RATE_LIMIT) + rate_limit = new BucketRateLimit(RATE_LIMIT * RATE_LIMIT_HISTORY, RATE_LIMIT_HISTORY); + else + rate_limit = new RateUnlimit();
// Wait until the query string parameters are loaded. this.loaderInfo.addEventListener(Event.COMPLETE, loaderinfo_complete); @@ -279,6 +291,7 @@ package
import flash.text.TextFormat; import flash.text.TextField; +import flash.utils.getTimer;
class Badge extends flash.display.Sprite { @@ -360,3 +373,98 @@ class Badge extends flash.display.Sprite cur_client_count_tf.appendText("."); } } + +class RateLimit +{ + public function RateLimit() + { + } + + public function update(n:Number):Boolean + { + return true; + } + + public function when():Number + { + return 0.0; + } + + public function is_limited():Boolean + { + return false; + } +} + +class RateUnlimit extends RateLimit +{ + public function RateUnlimit() + { + } + + public override function update(n:Number):Boolean + { + return true; + } + + public override function when():Number + { + return 0.0; + } + + public override function is_limited():Boolean + { + return false; + } +} + +class BucketRateLimit extends RateLimit +{ + private var amount:Number; + private var capacity:Number; + private var time:Number; + private var last_update:uint; + + public function BucketRateLimit(capacity:Number, time:Number) + { + this.amount = 0.0; + /* capacity / time is the rate we are aiming for. */ + this.capacity = capacity; + this.time = time; + this.last_update = getTimer(); + } + + private function age():void + { + var now:uint; + var delta:Number; + + now = getTimer(); + delta = (now - last_update) / 1000.0; + last_update = now; + + amount -= delta * capacity / time; + if (amount < 0.0) + amount = 0.0; + } + + public override function update(n:Number):Boolean + { + age(); + amount += n; + + return amount <= capacity; + } + + public override function when():Number + { + age(); + return (amount - capacity) / (capacity / time); + } + + public override function is_limited():Boolean + { + age(); + return amount > capacity; + } +}
tor-commits@lists.torproject.org