WvStreams
wvtimestream.cc
1/*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * See wvtimestream.h.
6 */
7#include "wvtimestream.h"
8
9WvTimeStream::WvTimeStream():
10 last(wvtime_zero), next(wvtime_zero), ms_per_tick(0)
11{
12}
13
14
15void WvTimeStream::set_timer(time_t msec)
16{
17 WvTime now = wvstime();
18
19 ms_per_tick = msec > 0 ? msec : 0;
20 next = msecadd(now, ms_per_tick);
21 last = now;
22}
23
24
26{
27 return true;
28}
29
30
32{
33 WvTime now;
34 time_t diff;
36
37 //fprintf(stderr, "%p: timestream pre_select mspt=%ld msto=%ld\n",
38 // this, ms_per_tick, si.msec_timeout);
39
40 if (ms_per_tick)
41 {
42 now = wvstime();
43
44 /* Are we going back in time? If so, adjust the due time. */
45 if (now < last)
46 next = tvdiff(next, tvdiff(last, now));
47
48 last = now;
49
50 if (next <= now)
51 {
52 si.msec_timeout = 0;
53 return;
54 }
55
56 diff = msecdiff(next, now);
57 diff = diff < 0 ? 0 : diff;
58 if (diff < si.msec_timeout || si.msec_timeout < 0)
59 si.msec_timeout = diff;
60 }
61}
62
63
65{
66 WvTime now = wvstime();
67
68 return WvStream::post_select(si) || (ms_per_tick && next <= now);
69}
70
71
73{
75
76 /* Schedule our next timer event, unless alarm_is_ticking, which
77 * would mean that we're here because someone used alarm() rather
78 * than because our timer expired. */
80 {
81 WvTime now = wvstime();
82
83 next = msecadd(next, ms_per_tick);
84
85 if (msecdiff(next, now) > ms_per_tick * 100 ||
86 msecdiff(now, next) > ms_per_tick * 100)
87 {
88 // reset if we fall forward or behind WAY too excessively
89 // This is usually due to a change in system time
90 last = now;
91 next = msecadd(last, ms_per_tick);
92 }
93 else if (msecdiff(next, now) > ms_per_tick * 10)
94 // compensate if we fall behind too excessively
95 next = msecadd(now, ms_per_tick);
96 }
97}
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
Definition wvstream.h:652
virtual bool post_select(SelectInfo &si)
post_select() is called after select(), and returns true if this object is now ready.
Definition wvstream.cc:875
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling select().
Definition wvstream.cc:844
bool alarm_was_ticking
This will be true during callback execution if the callback was triggered by the alarm going off.
Definition wvstream.h:54
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
void set_timer(time_t msec)
Every 'msec' milliseconds, select() will return true on this stream.
virtual bool post_select(SelectInfo &si)
post_select() is called after select(), and returns true if this object is now ready.
virtual bool isok() const
return true if the stream is actually usable right now
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling select().
Based on (and interchangeable with) struct timeval.
Definition wvtimeutils.h:18
the data structure used by pre_select()/post_select() and internally by select().
Definition iwvstream.h:50