+ bp->total_length = bp->initial_length + bp->count;
+
+ /* This code attempts to determine the current download speed. We
+ measure the speed over the interval of approximately three
+ seconds, in subintervals no smaller than 0.1s. In other words,
+ we maintain and use the history of 30 most recent reads, where a
+ "read" consists of one or more network reads, up until the point
+ where a subinterval is filled. */
+
+ if (hist->times[hist->pos]
+ >= DLSPEED_HISTORY_MAX_INTERVAL / DLSPEED_HISTORY_SIZE)
+ {
+ /* The subinterval at POS has been used up. Move on to the next
+ position. */
+ if (++hist->pos == DLSPEED_HISTORY_SIZE)
+ hist->pos = 0;
+
+ /* Invalidate old data (from the previous cycle) at this
+ position. */
+ hist->summed_times -= hist->times[hist->pos];
+ hist->summed_bytes -= hist->bytes[hist->pos];
+ hist->times[hist->pos] = delta_time;
+ hist->bytes[hist->pos] = howmuch;
+ }
+ else
+ {
+ /* Increment the data at POS. */
+ hist->times[hist->pos] += delta_time;
+ hist->bytes[hist->pos] += howmuch;
+ }
+
+ hist->summed_times += delta_time;
+ hist->summed_bytes += howmuch;
+ hist->previous_time = dltime;
+
+#if 0
+ /* Sledgehammer check that summed_times and summed_bytes are
+ accurate. */
+ {
+ int i;
+ long sumt = 0, sumb = 0;
+ for (i = 0; i < DLSPEED_HISTORY_SIZE; i++)
+ {
+ sumt += hist->times[i];
+ sumb += hist->bytes[i];
+ }
+ assert (sumt == hist->summed_times);
+ assert (sumb == hist->summed_bytes);
+ }
+#endif