Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* 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 | |
| 29 | namespace 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; |
Steve Kondik | a642462 | 2017-07-08 01:49:14 -0700 | [diff] [blame^] | 52 | |
| 53 | virtual ~Callback() {} |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // checkTimeouts() |
| 57 | // Dispatches any elapsed Timers, and returns the number of milliseconds until the |
| 58 | // next Timer will timeout. |
| 59 | static int checkTimeouts(); |
| 60 | |
| 61 | // getNextTimeout() |
| 62 | // Returns the number of milliseconds until the next timeout, without dispatching |
| 63 | // any elapsed Timers. |
| 64 | static int getNextTimeout(); |
| 65 | |
| 66 | // Create a Timer with the specified callback handler |
| 67 | Timer(Callback* cb_) {cb = cb_;} |
| 68 | ~Timer() {stop();} |
| 69 | |
| 70 | // startTimer |
| 71 | // Starts the timer, causing a timeout after the specified number of milliseconds. |
| 72 | // If the timer is already active then it will be implicitly cancelled and re-started. |
| 73 | void start(int timeoutMs_); |
| 74 | |
| 75 | // stopTimer |
| 76 | // Cancels the timer. |
| 77 | void stop(); |
| 78 | |
| 79 | // isStarted |
| 80 | // Determines whether the timer is started. |
| 81 | bool isStarted(); |
| 82 | |
| 83 | // getTimeoutMs |
| 84 | // Determines the previously used timeout value, if any. |
| 85 | // Usually used with isStarted() to get the _current_ timeout. |
| 86 | int getTimeoutMs(); |
| 87 | |
| 88 | // isBefore |
| 89 | // Determine whether the Timer will timeout before the specified time. |
| 90 | bool isBefore(timeval other); |
| 91 | |
| 92 | protected: |
| 93 | timeval dueTime; |
| 94 | int timeoutMs; |
| 95 | Callback* cb; |
| 96 | |
| 97 | static void insertTimer(Timer* t); |
| 98 | // The list of currently active Timers, ordered by time left until timeout. |
| 99 | static std::list<Timer*> pending; |
| 100 | }; |
| 101 | |
| 102 | }; |
| 103 | |
| 104 | #endif |