You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Timer.h 3.4KB

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