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