From 34a3278c85e883e3279b14acd57b02610c1039e3 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Sat, 11 Aug 2007 22:40:38 +0800 Subject: Fix timing calculations for serve_image Signed-off-by: David Woodhouse --- serve_image.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) (limited to 'serve_image.c') diff --git a/serve_image.c b/serve_image.c index d2e3390..dc434f0 100644 --- a/serve_image.c +++ b/serve_image.c @@ -112,6 +112,7 @@ int main(int argc, char **argv) nr_blocks = st.st_size / erasesize; printf("Checking CRC...."); + fflush(stdout); pktbuf.hdr.resend = 0; pktbuf.hdr.totcrc = htonl(crc32(-1, image, st.st_size)); @@ -196,30 +197,18 @@ int main(int argc, char **argv) now.tv_usec < nextpkt.tv_usec)); nextpkt.tv_usec = now.tv_usec + pkt_delay; - if (nextpkt.tv_usec > 1000000) { + if (nextpkt.tv_usec >= 1000000) { nextpkt.tv_sec += nextpkt.tv_usec / 1000000; nextpkt.tv_usec %= 1000000; } -#if 0 - /* Delay, if we are so far ahead of ourselves that we have at - least one tick to wait. */ - - gettimeofday(&now, NULL); - nextpkt.tv_usec = now.tv_usec + - /* Add to pkt_delay if necessary */ - delay_accum += pkt_delay; - delay_accum -= (now.tv_usec - blkthen.tv_usec); - delay_accum -= 1000000 * (now.tv_sec - blkthen.tv_sec); - if (delay_accum < 0) - delay_accum = 0; - blkthen = now; - - if (delay_accum >= usec_per_tick) { - usleep(delay_accum); - delay_accum = 0; - } printf("delay_accum %ld\n", delay_accum); -#endif + /* If the time for the next packet has already + passed, then we've lost time. Adjust our expected + timings accordingly. */ + if (now.tv_usec > (now.tv_usec + + 1000000 * (nextpkt.tv_sec - now.tv_sec))) + nextpkt = now; + } } munmap(image, st.st_size); -- cgit v1.2.3