blob: 66fd2b1215cb6345de9a77417ade20c6823001fe [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +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
32// XXX Lynx/OS 2.3: proto for gettimeofday()
33#ifdef Lynx
34#include <sys/proto.h>
35#endif
36
37using namespace rfb;
38
39#ifndef __NO_DEFINE_VLOG__
40static LogWriter vlog("Timer");
41#endif
42
43
44// Win32 does not provide gettimeofday, so we emulate it to simplify the
45// Timer code.
46
47#ifdef _WIN32
48static void gettimeofday(struct timeval* tv, void*)
49{
50 LARGE_INTEGER counts, countsPerSec;
51 static double usecPerCount = 0.0;
52
53 if (QueryPerformanceCounter(&counts)) {
54 if (usecPerCount == 0.0) {
55 QueryPerformanceFrequency(&countsPerSec);
56 usecPerCount = 1000000.0 / countsPerSec.QuadPart;
57 }
58
59 LONGLONG usecs = (LONGLONG)(counts.QuadPart * usecPerCount);
60 tv->tv_usec = (long)(usecs % 1000000);
61 tv->tv_sec = (long)(usecs / 1000000);
62
63 } else {
64#ifndef _WIN32_WCE
65 struct timeb tb;
66 ftime(&tb);
67 tv->tv_sec = tb.time;
68 tv->tv_usec = tb.millitm * 1000;
69#else
70 throw SystemException("QueryPerformanceCounter", GetLastError());
71#endif
72 }
73}
74#endif
75
76
77// Millisecond timeout processing helper functions
78
79inline static timeval addMillis(timeval inTime, int millis) {
80 int secs = millis / 1000;
81 millis = millis % 1000;
82 inTime.tv_sec += secs;
83 inTime.tv_usec += millis * 1000;
84 if (inTime.tv_usec >= 1000000) {
85 inTime.tv_sec++;
86 inTime.tv_usec -= 1000000;
87 }
88 return inTime;
89}
90
91inline static int diffTimeMillis(timeval later, timeval earlier) {
92 return ((later.tv_sec - earlier.tv_sec) * 1000) + ((later.tv_usec - earlier.tv_usec) / 1000);
93}
94
95std::list<Timer*> Timer::pending;
96
97int Timer::checkTimeouts() {
98 if (pending.empty())
99 return 0;
100 timeval now;
101 gettimeofday(&now, 0);
102 while (pending.front()->isBefore(now)) {
103 Timer* timer = pending.front();
104 pending.pop_front();
105 vlog.debug("handleTimeout(%p)", timer);
106 if (timer->cb->handleTimeout(timer)) {
107 timer->dueTime = addMillis(timer->dueTime, timer->timeoutMs);
108 if (timer->isBefore(now)) {
109 // Time has jumped forwards!
110 vlog.info("time has moved forwards!");
111 timer->dueTime = addMillis(now, timer->timeoutMs);
112 }
113 insertTimer(timer);
114 } else if (pending.empty()) {
115 return 0;
116 }
117 }
118 return getNextTimeout();
119}
120
121int Timer::getNextTimeout() {
122 timeval now;
123 gettimeofday(&now, 0);
124 int toWait = __rfbmax(1, diffTimeMillis(pending.front()->dueTime, now));
125 if (toWait > pending.front()->timeoutMs) {
126 if (toWait - pending.front()->timeoutMs < 1000) {
127 vlog.info("gettimeofday is broken...");
128 return toWait;
129 }
130 // Time has jumped backwards!
131 vlog.info("time has moved backwards!");
132 pending.front()->dueTime = now;
133 toWait = 1;
134 }
135 return toWait;
136}
137
138void Timer::insertTimer(Timer* t) {
139 std::list<Timer*>::iterator i;
140 for (i=pending.begin(); i!=pending.end(); i++) {
141 if (t->isBefore((*i)->dueTime)) {
142 pending.insert(i, t);
143 return;
144 }
145 }
146 pending.push_back(t);
147}
148
149void Timer::start(int timeoutMs_) {
150 timeval now;
151 gettimeofday(&now, 0);
152 stop();
153 timeoutMs = timeoutMs_;
154 dueTime = addMillis(now, timeoutMs);
155 insertTimer(this);
156}
157
158void Timer::stop() {
159 pending.remove(this);
160}
161
162bool Timer::isStarted() {
163 std::list<Timer*>::iterator i;
164 for (i=pending.begin(); i!=pending.end(); i++) {
165 if (*i == this)
166 return true;
167 }
168 return false;
169}
170
171int Timer::getTimeoutMs() {
172 return timeoutMs;
173}
174
175bool Timer::isBefore(timeval other) {
176 return (dueTime.tv_sec < other.tv_sec) ||
177 ((dueTime.tv_sec == other.tv_sec) &&
178 (dueTime.tv_usec < other.tv_usec));
179}