aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/Timer.cxx
blob: fbc9bae95e4043f66bb14a1630ac7fb025246afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright 2016-2024 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- Timer.cxx

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <sys/time.h>

#include <algorithm>

#include <rfb/Timer.h>
#include <rfb/util.h>
#include <rfb/LogWriter.h>

using namespace rfb;

#ifndef __NO_DEFINE_VLOG__
static LogWriter vlog("Timer");
#endif


// Millisecond timeout processing helper functions

inline static timeval addMillis(timeval inTime, int millis) {
  int secs = millis / 1000;
  millis = millis % 1000;
  inTime.tv_sec += secs;
  inTime.tv_usec += millis * 1000;
  if (inTime.tv_usec >= 1000000) {
    inTime.tv_sec++;
    inTime.tv_usec -= 1000000;
  }
  return inTime;
}

inline static int diffTimeMillis(timeval later, timeval earlier) {
  long udiff;
  udiff = ((later.tv_sec - earlier.tv_sec) * 1000000) +
          (later.tv_usec - earlier.tv_usec);
  return (udiff + 999) / 1000;
}

std::list<Timer*> Timer::pending;

int Timer::checkTimeouts() {
  timeval start;

  if (pending.empty())
    return -1;

  gettimeofday(&start, nullptr);
  while (pending.front()->isBefore(start)) {
    Timer* timer;

    timer = pending.front();
    pending.pop_front();

    timer->lastDueTime = timer->dueTime;
    timer->cb->handleTimeout(timer);

    if (pending.empty())
      return -1;
  }
  return getNextTimeout();
}

int Timer::getNextTimeout() {
  timeval now;
  gettimeofday(&now, nullptr);

  if (pending.empty())
    return -1;

  int toWait = pending.front()->getRemainingMs();

  if (toWait > pending.front()->timeoutMs) {
    if (toWait - pending.front()->timeoutMs < 1000) {
      vlog.info("gettimeofday is broken...");
      return toWait;
    }
    // Time has jumped backwards!
    vlog.info("time has moved backwards!");
    pending.front()->dueTime = now;
    toWait = 0;
  }

  return toWait;
}

void Timer::insertTimer(Timer* t) {
  std::list<Timer*>::iterator i;
  for (i=pending.begin(); i!=pending.end(); i++) {
    if (t->isBefore((*i)->dueTime)) {
      pending.insert(i, t);
      return;
    }
  }
  pending.push_back(t);
}

void Timer::start(int timeoutMs_) {
  timeval now;
  gettimeofday(&now, nullptr);
  stop();
  timeoutMs = timeoutMs_;
  dueTime = addMillis(now, timeoutMs);
  insertTimer(this);
}

void Timer::repeat(int timeoutMs_) {
  timeval now;

  gettimeofday(&now, nullptr);

  if (isStarted()) {
    vlog.error("Incorrectly repeating already running timer");
    stop();
  }

  if (msBetween(&lastDueTime, &dueTime) != 0)
    vlog.error("Timer incorrectly modified whilst repeating");

  if (timeoutMs_ != -1)
    timeoutMs = timeoutMs_;

  dueTime = addMillis(lastDueTime, timeoutMs);
  if (isBefore(now)) {
    // Time has jumped forwards, or we're not getting enough
    // CPU time for the timers
    dueTime = now;
  }

  insertTimer(this);
}

void Timer::stop() {
  pending.remove(this);
}

bool Timer::isStarted() {
  return std::find(pending.begin(), pending.end(),
                   this) != pending.end();
}

int Timer::getTimeoutMs() {
  return timeoutMs;
}

int Timer::getRemainingMs() {
  timeval now;
  gettimeofday(&now, nullptr);
  return __rfbmax(0, diffTimeMillis(dueTime, now));
}

bool Timer::isBefore(timeval other) {
  return (dueTime.tv_sec < other.tv_sec) ||
    ((dueTime.tv_sec == other.tv_sec) &&
     (dueTime.tv_usec < other.tv_usec));
}