1
/**************************************************************************/
3
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/. */
5
/* NXCOMP, NX protocol compression and NX extensions to this software */
6
/* are copyright of NoMachine. Redistribution and use of the present */
7
/* software is allowed according to terms specified in the file LICENSE */
8
/* which comes in the source distribution. */
10
/* Check http://www.nomachine.com/licensing.html for applicability. */
12
/* NX and NoMachine are trademarks of NoMachine S.r.l. */
14
/* All rights reserved. */
16
/**************************************************************************/
40
// If not defined, always query the system time.
43
#undef CACHE_TIMESTAMP
46
// Log a warning if the time difference since
47
// the last update exceeds the given number
51
#define DRIFT_TIMESTAMP 1
54
// Type used for timeout manipulation.
57
typedef struct timeval T_timestamp;
60
// Last timestamp taken from the system. If the
61
// timestamp is cached, we need to explicitly
62
// get a new timestamp after any operation that
63
// may have required a relevant amount of time.
66
extern T_timestamp timestamp;
69
// Get a timestamp instance with values set
70
// at the given amount of milliseconds.
73
inline T_timestamp getTimestamp(long ms)
77
ts.tv_sec = ms / 1000;
78
ts.tv_usec = (ms % 1000) * 1000;
84
// Return the difference in milliseconds
85
// between the two timestamps.
88
inline long diffTimestamp(const T_timestamp &ts1, const T_timestamp &ts2)
91
// Add 500 microseconds to round up
92
// to the nearest millisecond.
95
return ((ts2.tv_sec * 1000 + (ts2.tv_usec + 500) / 1000) -
96
(ts1.tv_sec * 1000 + (ts1.tv_usec + 500) / 1000));
100
// The same in microseconds. It doesn't
104
inline long diffUsTimestamp(const T_timestamp &ts1, const T_timestamp &ts2)
106
return ((ts2.tv_sec * 1000000 + ts2.tv_usec) -
107
(ts1.tv_sec * 1000000 + ts1.tv_usec));
111
// Return the last timestamp taken from the
112
// system. It doesn't update the timestamp.
115
inline T_timestamp getTimestamp()
117
#ifdef CACHE_TIMESTAMP
123
gettimeofday(&ts, NULL);
125
long diffTs = diffTimestamp(timestamp, ts);
127
if (diffTs > DRIFT_TIMESTAMP)
129
*logofs << "Timestamp: WARNING! Time difference since the "
130
<< "current timestamp is " << diffTs << " Ms.\n"
140
gettimeofday(×tamp, NULL);
147
inline T_timestamp &setTimestamp(T_timestamp &ts, long ms)
149
ts.tv_sec = ms / 1000;
150
ts.tv_usec = (ms % 1000) * 1000;
156
// Return the smaller between two timestamps.
159
inline T_timestamp &setMinTimestamp(T_timestamp &ts, long ms)
161
if ((ts.tv_sec * 1000 + ts.tv_usec / 1000) > ms)
163
ts.tv_sec = ms / 1000;
164
ts.tv_usec = (ms % 1000) * 1000;
170
inline T_timestamp &setMinTimestamp(T_timestamp &ts1, T_timestamp &ts2)
172
if ((ts1.tv_sec * 1000000 + ts1.tv_usec) >
173
(ts2.tv_sec * 1000000 + ts2.tv_usec))
175
ts1.tv_sec = ts2.tv_sec;
176
ts1.tv_usec = ts2.tv_usec;
183
// Convert a timestamp in the total number
187
inline long getMsTimestamp(const T_timestamp &ts)
189
return ts.tv_sec * 1000 + ts.tv_usec / 1000;
193
// A 0 value on both seconds and microseconds
194
// fields means that timestamp is invalid or
198
inline T_timestamp nullTimestamp()
208
inline bool isTimestamp(const T_timestamp &ts)
210
if (ts.tv_sec == 0 && ts.tv_usec == 0)
218
inline void subMsTimestamp(T_timestamp &ts, long ms)
220
ts.tv_sec -= ms / 1000;
221
ts.tv_usec -= (ms % 1000) * 1000;
224
inline void addMsTimestamp(T_timestamp &ts, long ms)
226
ts.tv_sec += ms / 1000;
227
ts.tv_usec += (ms % 1000) * 1000;
231
// Check the difference between timestamps.
232
// Return 0 if the system time went backward
233
// compared to the second timestamp, or the
234
// difference between the timestamps exceeds
235
// the given number of milliseconds.
238
inline int checkDiffTimestamp(const T_timestamp &ts1, const T_timestamp &ts2,
241
long diffTs = diffTimestamp(ts1, ts2);
243
if (diffTs < 0 || diffTs > ms)
252
// Return a string representing the timestamp.
255
char *strTimestamp(const T_timestamp &ts);
256
char *strMsTimestamp(const T_timestamp &ts);
258
inline char *strTimestamp()
260
return strTimestamp(getTimestamp());
263
inline char *strMsTimestamp()
265
return strMsTimestamp(getTimestamp());
269
// Update the current timestamp.
272
inline T_timestamp getNewTimestamp()
278
gettimeofday(&ts, NULL);
280
*logofs << "Timestamp: Updating the current timestamp at "
281
<< strMsTimestamp(ts) << ".\n" << logofs_flush;
283
long diffTs = diffTimestamp(timestamp, ts);
285
if (diffTs > DRIFT_TIMESTAMP)
287
*logofs << "Timestamp: WARNING! Time difference since the "
288
<< "old timestamp is " << diffTs << " Ms.\n"
294
gettimeofday(×tamp, NULL);
299
#endif /* Timestamp_H */