blob: e295b8261e6a0659701f31dba3fcd028a6f0db00 [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#ifndef __RFB_TIMER_H__
20#define __RFB_TIMER_H__
21
22#include <list>
23#ifdef WIN32
24#include <winsock2.h>
25#else
26#include <sys/time.h>
27#endif
28
29namespace rfb {
30
31 /* Timer
32
33 Cross-platform timeout handling. The caller creates instances of Timer and passes a
34 Callback implementation to each. The Callback will then be called with a pointer to
35 the Timer instance that timed-out when the timeout occurs.
36
37 The static methods of Timer are used by the main loop of the application both to
38 dispatch elapsed Timer callbacks and to determine how long to wait in select() for
39 the next timeout to occur.
40
41 */
42
43 struct Timer {
44
45 struct Callback {
46 // handleTimeout
47 // Passed a pointer to the Timer that has timed out. If the handler returns true
48 // then the Timer is reset and left running, causing another timeout after the
49 // appropriate interval.
50 // If the handler returns false then the Timer is cancelled.
51 virtual bool handleTimeout(Timer* t) = 0;
52 };
53
54 // checkTimeouts()
55 // Dispatches any elapsed Timers, and returns the number of milliseconds until the
56 // next Timer will timeout.
57 static int checkTimeouts();
58
59 // getNextTimeout()
60 // Returns the number of milliseconds until the next timeout, without dispatching
61 // any elapsed Timers.
62 static int getNextTimeout();
63
64 // Create a Timer with the specified callback handler
65 Timer(Callback* cb_) {cb = cb_;}
66 ~Timer() {stop();}
67
68 // startTimer
69 // Starts the timer, causing a timeout after the specified number of milliseconds.
70 // If the timer is already active then it will be implicitly cancelled and re-started.
71 void start(int timeoutMs_);
72
73 // stopTimer
74 // Cancels the timer.
75 void stop();
76
77 // isStarted
78 // Determines whether the timer is started.
79 bool isStarted();
80
81 // getTimeoutMs
82 // Determines the previously used timeout value, if any.
83 // Usually used with isStarted() to get the _current_ timeout.
84 int getTimeoutMs();
85
86 // isBefore
87 // Determine whether the Timer will timeout before the specified time.
88 bool isBefore(timeval other);
89
90 protected:
91 timeval dueTime;
92 int timeoutMs;
93 Callback* cb;
94
95 static void insertTimer(Timer* t);
96 // The list of currently active Timers, ordered by time left until timeout.
97 static std::list<Timer*> pending;
98 };
99
100};
101
102#endif