+static struct {
+ long bytes;
+ long dltime;
+} limit_data;
+
+static void
+limit_bandwidth_reset (void)
+{
+ limit_data.bytes = 0;
+ limit_data.dltime = 0;
+}
+
+/* Limit the bandwidth by pausing the download for an amount of time.
+ BYTES is the number of bytes received from the network, DELTA is
+ how long it took to receive them, DLTIME the current download time,
+ TIMER the timer, and ADJUSTMENT the previous. */
+
+static void
+limit_bandwidth (long bytes, long delta)
+{
+ long expected;
+
+ limit_data.bytes += bytes;
+ limit_data.dltime += delta;
+
+ expected = (long)(1000.0 * limit_data.bytes / opt.limit_rate);
+
+ if (expected > limit_data.dltime)
+ {
+ long slp = expected - limit_data.dltime;
+ if (slp < 200)
+ {
+ DEBUGP (("deferring a %ld ms sleep (%ld/%ld) until later.\n",
+ slp, limit_data.bytes, limit_data.dltime));
+ return;
+ }
+ DEBUGP (("sleeping %ld ms\n", slp));
+ usleep (1000 * slp);
+ }
+
+ limit_data.bytes = 0;
+ limit_data.dltime = 0;
+}
+