Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Timer.h 3.8KB

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