+static struct {
+ long bytes;
+ double 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, double delta)
+{
+ double expected;
+
+ limit_data.bytes += bytes;
+ limit_data.dltime += delta;
+
+ expected = 1000.0 * limit_data.bytes / opt.limit_rate;
+
+ if (expected > limit_data.dltime)
+ {
+ double slp = expected - limit_data.dltime;
+ if (slp < 200)
+ {
+ DEBUGP (("deferring a %.2f ms sleep (%ld/%.2f).\n",
+ slp, limit_data.bytes, limit_data.dltime));
+ return;
+ }
+ DEBUGP (("sleeping %.2f ms\n", slp));
+ usleep ((unsigned long) (1000 * slp));
+ }
+
+ limit_data.bytes = 0;
+ limit_data.dltime = 0;
+}
+