blob: 59d250ed65d77f3d58f188059c87bd320448d1e1 [file] [log] [blame]
Constantin Kaplinskybdf56512006-04-17 01:32:35 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Timer.cxx
20
21#include <stdio.h>
22#ifdef WIN32
23#include <windows.h>
24#ifndef _WIN32_WCE
25#include <sys/timeb.h>
26#endif
27#endif
28#include <rfb/Timer.h>
29#include <rfb/util.h>
30#include <rfb/LogWriter.h>
31
32using namespace rfb;
33
34#ifndef __NO_DEFINE_VLOG__
35static LogWriter vlog("Timer");
36#endif
37
38
39// Win32 does not provide gettimeofday, so we emulate it to simplify the
40// Timer code.
41
42#ifdef _WIN32
43static void gettimeofday(struct timeval* tv, void*)
44{
45 LARGE_INTEGER counts, countsPerSec;
46 static double usecPerCount = 0.0;
47
48 if (QueryPerformanceCounter(&counts)) {
49 if (usecPerCount == 0.0) {
50 QueryPerformanceFrequency(&countsPerSec);
51 usecPerCount = 1000000.0 / countsPerSec.QuadPart;
52 }
53
54 LONGLONG usecs = (LONGLONG)(counts.QuadPart * usecPerCount);
55 tv->tv_usec = (long)(usecs % 1000000);
56 tv->tv_sec = (long)(usecs / 1000000);
57
58 } else {
59#ifndef _WIN32_WCE
60 struct timeb tb;
61 ftime(&tb);
62 tv->tv_sec = tb.time;
63 tv->tv_usec = tb.millitm * 1000;
64#else
65 throw SystemException("QueryPerformanceCounter", GetLastError());
66#endif
67 }
68}
69#endif
70
71
72// Millisecond timeout processing helper functions
73
74inline static timeval addMillis(timeval inTime, int millis) {
75 int secs = millis / 1000;
76 millis = millis % 1000;
77 inTime.tv_sec += secs;
78 inTime.tv_usec += millis * 1000;
79 if (inTime.tv_usec >= 1000000) {
80 inTime.tv_sec++;
81 inTime.tv_usec -= 1000000;
82 }
83 return inTime;
84}
85
86inline static int diffTimeMillis(timeval later, timeval earlier) {
87 return ((later.tv_sec - earlier.tv_sec) * 1000) + ((later.tv_usec - earlier.tv_usec) / 1000);
88}
89
90std::list<Timer*> Timer::pending;
91
92int Timer::checkTimeouts() {
93 if (pending.empty())
94 return 0;
95 timeval now;
96 gettimeofday(&now, 0);
97 while (pending.front()->isBefore(now)) {
98 Timer* timer = pending.front();
99 pending.pop_front();
100 vlog.debug("handleTimeout(%p)", timer);
101 if (timer->cb->handleTimeout(timer)) {
102 timer->dueTime = addMillis(timer->dueTime, timer->timeoutMs);
103 if (timer->isBefore(now)) {
104 // Time has jumped forwards!
105 vlog.info("time has moved forwards!");
106 timer->dueTime = addMillis(now, timer->timeoutMs);
107 }
108 insertTimer(timer);
109 } else if (pending.empty()) {
110 return 0;
111 }
112 }
113 return getNextTimeout();
114}
115
116int Timer::getNextTimeout() {
117 timeval now;
118 gettimeofday(&now, 0);
119 int toWait = __rfbmax(1, diffTimeMillis(pending.front()->dueTime, now));
120 if (toWait > pending.front()->timeoutMs) {
121 if (toWait - pending.front()->timeoutMs < 1000) {
122 vlog.info("gettimeofday is broken...");
123 return toWait;
124 }
125 // Time has jumped backwards!
126 vlog.info("time has moved backwards!");
127 pending.front()->dueTime = now;
128 toWait = 1;
129 }
130 return toWait;
131}
132
133void Timer::insertTimer(Timer* t) {
134 std::list<Timer*>::iterator i;
135 for (i=pending.begin(); i!=pending.end(); i++) {
136 if (t->isBefore((*i)->dueTime)) {
137 pending.insert(i, t);
138 return;
139 }
140 }
141 pending.push_back(t);
142}
143
144void Timer::start(int timeoutMs_) {
145 timeval now;
146 gettimeofday(&now, 0);
147 stop();
148 timeoutMs = timeoutMs_;
149 dueTime = addMillis(now, timeoutMs);
150 insertTimer(this);
151}
152
153void Timer::stop() {
154 pending.remove(this);
155}
156
157bool Timer::isStarted() {
158 std::list<Timer*>::iterator i;
159 for (i=pending.begin(); i!=pending.end(); i++) {
160 if (*i == this)
161 return true;
162 }
163 return false;
164}
165
166int Timer::getTimeoutMs() {
167 return timeoutMs;
168}
169
170bool Timer::isBefore(timeval other) {
171 return (dueTime.tv_sec < other.tv_sec) ||
172 ((dueTime.tv_sec == other.tv_sec) &&
173 (dueTime.tv_usec < other.tv_usec));
174}