blob: 15b5d03de6e9b38408e27eb76d0101de17a2760a [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmana2b80d62018-03-23 09:30:09 +01002 * Copyright 2018 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#ifndef __RFB_TIMER_H__
21#define __RFB_TIMER_H__
22
23#include <list>
24#ifdef WIN32
25#include <winsock2.h>
26#else
27#include <sys/time.h>
28#endif
29
30namespace rfb {
31
32 /* Timer
33
34 Cross-platform timeout handling. The caller creates instances of Timer and passes a
35 Callback implementation to each. The Callback will then be called with a pointer to
36 the Timer instance that timed-out when the timeout occurs.
37
38 The static methods of Timer are used by the main loop of the application both to
39 dispatch elapsed Timer callbacks and to determine how long to wait in select() for
40 the next timeout to occur.
41
42 */
43
44 struct Timer {
45
46 struct Callback {
47 // handleTimeout
48 // Passed a pointer to the Timer that has timed out. If the handler returns true
49 // then the Timer is reset and left running, causing another timeout after the
50 // appropriate interval.
51 // If the handler returns false then the Timer is cancelled.
52 virtual bool handleTimeout(Timer* t) = 0;
Steve Kondika6424622017-07-08 01:49:14 -070053
54 virtual ~Callback() {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055 };
56
57 // checkTimeouts()
58 // Dispatches any elapsed Timers, and returns the number of milliseconds until the
59 // next Timer will timeout.
60 static int checkTimeouts();
61
62 // getNextTimeout()
63 // Returns the number of milliseconds until the next timeout, without dispatching
64 // any elapsed Timers.
65 static int getNextTimeout();
66
67 // Create a Timer with the specified callback handler
68 Timer(Callback* cb_) {cb = cb_;}
69 ~Timer() {stop();}
70
71 // startTimer
72 // Starts the timer, causing a timeout after the specified number of milliseconds.
73 // If the timer is already active then it will be implicitly cancelled and re-started.
74 void start(int timeoutMs_);
75
76 // stopTimer
77 // Cancels the timer.
78 void stop();
79
80 // isStarted
81 // Determines whether the timer is started.
82 bool isStarted();
83
84 // getTimeoutMs
85 // Determines the previously used timeout value, if any.
86 // Usually used with isStarted() to get the _current_ timeout.
87 int getTimeoutMs();
88
Pierre Ossmana2b80d62018-03-23 09:30:09 +010089 // getRemainingMs
90 // Determines how many milliseconds are left before the Timer
91 // will timeout. Only valid for an active timer.
92 int getRemainingMs();
93
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 // isBefore
95 // Determine whether the Timer will timeout before the specified time.
96 bool isBefore(timeval other);
97
98 protected:
99 timeval dueTime;
100 int timeoutMs;
101 Callback* cb;
102
103 static void insertTimer(Timer* t);
104 // The list of currently active Timers, ordered by time left until timeout.
105 static std::list<Timer*> pending;
106 };
107
108};
109
110#endif