aboutsummaryrefslogtreecommitdiff
path: root/src/Timer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/Timer.cc')
-rw-r--r--src/Timer.cc198
1 files changed, 99 insertions, 99 deletions
diff --git a/src/Timer.cc b/src/Timer.cc
index 23849e1..ebc1cd8 100644
--- a/src/Timer.cc
+++ b/src/Timer.cc
@@ -39,139 +39,139 @@
39BTimer::TimerList BTimer::m_timerlist; 39BTimer::TimerList BTimer::m_timerlist;
40 40
41BTimer::BTimer(TimeoutHandler *h): 41BTimer::BTimer(TimeoutHandler *h):
42m_handler(h), 42 m_handler(h),
43m_timing(false), 43 m_timing(false),
44m_once(false) { 44 m_once(false) {
45} 45}
46 46
47 47
48BTimer::~BTimer() { 48BTimer::~BTimer() {
49 if (isTiming()) stop(); 49 if (isTiming()) stop();
50} 50}
51 51
52 52
53void BTimer::setTimeout(long t) { 53void BTimer::setTimeout(long t) {
54 m_timeout.tv_sec = t / 1000; 54 m_timeout.tv_sec = t / 1000;
55 m_timeout.tv_usec = t; 55 m_timeout.tv_usec = t;
56 m_timeout.tv_usec -= (m_timeout.tv_sec * 1000); 56 m_timeout.tv_usec -= (m_timeout.tv_sec * 1000);
57 m_timeout.tv_usec *= 1000; 57 m_timeout.tv_usec *= 1000;
58} 58}
59 59
60 60
61void BTimer::setTimeout(timeval t) { 61void BTimer::setTimeout(timeval t) {
62 m_timeout.tv_sec = t.tv_sec; 62 m_timeout.tv_sec = t.tv_sec;
63 m_timeout.tv_usec = t.tv_usec; 63 m_timeout.tv_usec = t.tv_usec;
64} 64}
65 65
66 66
67void BTimer::start() { 67void BTimer::start() {
68 gettimeofday(&m_start, 0); 68 gettimeofday(&m_start, 0);
69 69
70 if (! m_timing) { 70 if (! m_timing) {
71 m_timing = true; 71 m_timing = true;
72 addTimer(this); //add us to the list 72 addTimer(this); //add us to the list
73 } 73 }
74} 74}
75 75
76 76
77void BTimer::stop() { 77void BTimer::stop() {
78 m_timing = false; 78 m_timing = false;
79 removeTimer(this); //remove us from the list 79 removeTimer(this); //remove us from the list
80} 80}
81 81
82 82
83void BTimer::fireTimeout() { 83void BTimer::fireTimeout() {
84 if (m_handler) m_handler->timeout(); 84 if (m_handler) m_handler->timeout();
85} 85}
86 86
87void BTimer::updateTimers(int fd) { 87void BTimer::updateTimers(int fd) {
88 fd_set rfds; 88 fd_set rfds;
89 timeval now, tm, *timeout = 0; 89 timeval now, tm, *timeout = 0;
90 90
91 FD_ZERO(&rfds); 91 FD_ZERO(&rfds);
92 FD_SET(fd, &rfds); 92 FD_SET(fd, &rfds);
93 93
94 if (m_timerlist.size() > 0) { 94 if (m_timerlist.size() > 0) {
95 gettimeofday(&now, 0); 95 gettimeofday(&now, 0);
96 96
97 tm.tv_sec = tm.tv_usec = 0l; 97 tm.tv_sec = tm.tv_usec = 0l;
98 98
99 BTimer *timer = m_timerlist.front(); 99 BTimer *timer = m_timerlist.front();
100 100
101 tm.tv_sec = timer->getStartTime().tv_sec + 101 tm.tv_sec = timer->getStartTime().tv_sec +
102 timer->getTimeout().tv_sec - now.tv_sec; 102 timer->getTimeout().tv_sec - now.tv_sec;
103 tm.tv_usec = timer->getStartTime().tv_usec + 103 tm.tv_usec = timer->getStartTime().tv_usec +
104 timer->getTimeout().tv_usec - now.tv_usec; 104 timer->getTimeout().tv_usec - now.tv_usec;
105 105
106 while (tm.tv_usec >= 1000000) { 106 while (tm.tv_usec >= 1000000) {
107 tm.tv_sec++; 107 tm.tv_sec++;
108 tm.tv_usec -= 1000000; 108 tm.tv_usec -= 1000000;
109 } 109 }
110 110
111 while (tm.tv_usec < 0) { 111 while (tm.tv_usec < 0) {
112 if (tm.tv_sec > 0) { 112 if (tm.tv_sec > 0) {
113 tm.tv_sec--; 113 tm.tv_sec--;
114 tm.tv_usec += 1000000; 114 tm.tv_usec += 1000000;
115 } else { 115 } else {
116 tm.tv_usec = 0; 116 tm.tv_usec = 0;
117 break; 117 break;
118 } 118 }
119 } 119 }
120 120
121 timeout = &tm; 121 timeout = &tm;
122 } 122 }
123 123
124 select(fd + 1, &rfds, 0, 0, timeout); 124 select(fd + 1, &rfds, 0, 0, timeout);
125 125
126 // check for timer timeout 126 // check for timer timeout
127 gettimeofday(&now, 0); 127 gettimeofday(&now, 0);
128 128
129 TimerList::iterator it = m_timerlist.begin(); 129 TimerList::iterator it = m_timerlist.begin();
130 //must check end ...the timer might remove 130 //must check end ...the timer might remove
131 //it self from the list (should be fixed in the future) 131 //it self from the list (should be fixed in the future)
132 for(; it != m_timerlist.end(); ++it) { 132 for(; it != m_timerlist.end(); ++it) {
133 //This is to make sure we don't get an invalid iterator 133 //This is to make sure we don't get an invalid iterator
134 //when we do fireTimeout 134 //when we do fireTimeout
135 BTimer &t = *(*it); 135 BTimer &t = *(*it);
136 tm.tv_sec = t.getStartTime().tv_sec + 136 tm.tv_sec = t.getStartTime().tv_sec +
137 t.getTimeout().tv_sec; 137 t.getTimeout().tv_sec;
138 tm.tv_usec = t.getStartTime().tv_usec + 138 tm.tv_usec = t.getStartTime().tv_usec +
139 t.getTimeout().tv_usec; 139 t.getTimeout().tv_usec;
140 140
141 if ((now.tv_sec < tm.tv_sec) || 141 if ((now.tv_sec < tm.tv_sec) ||
142 (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec)) 142 (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))
143 break; 143 break;
144 144
145 t.fireTimeout(); 145 t.fireTimeout();
146 // restart the current timer so that the start time is updated 146 // restart the current timer so that the start time is updated
147 if (! t.doOnce()) 147 if (! t.doOnce())
148 t.start(); 148 t.start();
149 else { 149 else {
150 t.stop(); 150 t.stop();
151 it--; 151 it--;
152 } 152 }
153 } 153 }
154} 154}
155 155
156void BTimer::addTimer(BTimer *timer) { 156void BTimer::addTimer(BTimer *timer) {
157 assert(timer); 157 assert(timer);
158 158
159 TimerList::iterator it = m_timerlist.begin(); 159 TimerList::iterator it = m_timerlist.begin();
160 TimerList::iterator it_end = m_timerlist.end(); 160 TimerList::iterator it_end = m_timerlist.end();
161 int index = 0; 161 int index = 0;
162 for (; it != it_end; ++it, ++index) { 162 for (; it != it_end; ++it, ++index) {
163 if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) || 163 if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
164 (((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) && 164 (((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
165 ((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) { 165 ((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) {
166 break; 166 break;
167 } 167 }
168 } 168 }
169 m_timerlist.insert(it, timer); 169 m_timerlist.insert(it, timer);
170 170
171} 171}
172 172
173void BTimer::removeTimer(BTimer *timer) { 173void BTimer::removeTimer(BTimer *timer) {
174 assert(timer); 174 assert(timer);
175 m_timerlist.remove(timer); 175 m_timerlist.remove(timer);
176} 176}
177 177