/
event_time.h
241 lines (168 loc) · 5.57 KB
/
event_time.h
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// ============================================================================
//
// = LIBRARY
// ULib - c++ library
//
// = FILENAME
// event_time.h
//
// = AUTHOR
// Stefano Casazza
//
// ============================================================================
#ifndef ULIB_EVENT_TIME_H
#define ULIB_EVENT_TIME_H 1
#include <ulib/timeval.h>
class UTimer;
class UNotifier;
#ifdef USE_LIBEVENT
template <class T> class UTimerEv;
#endif
class U_EXPORT UEventTime : public UTimeVal {
public:
struct timeval ctime;
UEventTime(long sec = 0L, long micro_sec = 1L);
virtual ~UEventTime();
bool operator<(const UEventTime& t) const __pure;
// SERVICES
bool isExpired() const __pure
{
U_TRACE_NO_PARAM(0, "UEventTime::isExpired()")
U_CHECK_MEMORY
long diff1 = ctime.tv_sec + tv_sec - timeout1.tv_sec,
diff2 = ctime.tv_usec + tv_usec - timeout1.tv_usec;
U_INTERNAL_DUMP("this = { %ld %6ld }, diff1 = %ld diff2 = %ld", ctime.tv_sec + tv_sec,
ctime.tv_usec + tv_usec, diff1, diff2)
if ( diff1 < 0 ||
(diff1 == 0 &&
diff2 <= 0))
{
U_RETURN(true);
}
ms = (diff1 * 1000L) +
(diff2 / 1000L);
U_ASSERT(checkMilliSecond())
U_RETURN(false);
}
bool isExpiredWithTolerance() const __pure
{
U_TRACE_NO_PARAM(0, "UEventTime::isExpiredWithTolerance()")
if (isExpired() == false)
{
U_ASSERT(checkTolerance())
if (ms > tolerance) U_RETURN(false);
}
U_RETURN(true);
}
time_t expire() const { return (ctime.tv_sec + tv_sec); }
// -------------------------------------------
// method VIRTUAL to define
// -------------------------------------------
// return value: -1 -> normal, 0 -> monitoring
// -------------------------------------------
virtual int handlerTime() { return -1; }
#ifdef USE_LIBEVENT
UTimerEv<UEventTime>* pevent;
void operator()(int fd, short event);
#endif
#ifdef U_STDCPP_ENABLE
friend U_EXPORT ostream& operator<<(ostream& os, const UEventTime& t);
# ifdef DEBUG
const char* dump(bool reset) const;
# endif
#endif
protected:
long tolerance;
static long ms;
static struct timeval timeout1;
static struct timespec timeout2;
void setCurrentTime()
{
U_TRACE_NO_PARAM(1, "UEventTime::setCurrentTime()")
U_CHECK_MEMORY
(void) U_SYSCALL(gettimeofday, "%p,%p", &ctime, 0);
U_INTERNAL_DUMP("ctime = { %ld %6ld }", ctime.tv_sec, ctime.tv_usec)
}
void setTime(long timeoutMS)
{
U_TRACE(0, "UEventTime::setTime(%ld)", timeoutMS)
setCurrentTime();
UTimeVal::setMilliSecond(ms = timeoutMS);
}
bool checkMilliSecond() const
{
U_TRACE_NO_PARAM(0, "UEventTime::checkMilliSecond()")
long ms_calculated = ((ctime.tv_sec + tv_sec - timeout1.tv_sec) * 1000L) +
((ctime.tv_usec + tv_usec - timeout1.tv_usec) / 1000L);
if ((ms - ms_calculated) <= 1) U_RETURN(true);
U_DEBUG("ms = %ld ms_calculated = %ld", ms, ms_calculated);
U_RETURN(false);
}
void setTolerance()
{
U_TRACE_NO_PARAM(0, "UEventTime::setTolerance()")
tolerance = ((tv_sec * 1000L) +
(tv_usec / 1000L)) / 128;
U_INTERNAL_DUMP("tolerance = %ld", tolerance)
}
bool checkTolerance() const
{
U_TRACE_NO_PARAM(0, "UEventTime::checkTolerance()")
U_ASSERT(checkMilliSecond())
long tolerance_calculated = ((tv_sec * 1000L) +
(tv_usec / 1000L)) / 128;
if ((tolerance - tolerance_calculated) <= 1) U_RETURN(true);
U_DEBUG("tolerance = %ld tolerance_calculated = %ld", tolerance, tolerance_calculated);
U_RETURN(false);
}
static long getMilliSecond(UEventTime* ptimeout)
{
U_TRACE(0, "UEventTime::getMilliSecond(%p)", ptimeout)
if (ptimeout == 0) U_RETURN(-1);
U_ASSERT(ptimeout->checkMilliSecond())
U_RETURN(ms);
}
static struct timeval* getTimeVal(UEventTime* ptimeout)
{
U_TRACE(0, "UEventTime::getTimeVal(%p)", ptimeout)
if (ptimeout == 0) U_RETURN_POINTER(0, struct timeval);
U_ASSERT(ptimeout->checkMilliSecond())
/**
* struct timeval {
* long tv_sec; // seconds
* long tv_usec; // microseconds
* };
*/
long us = ms * 1000L;
timeout1.tv_sec = us / 1000000L;
timeout1.tv_usec = us % 1000000L;
U_INTERNAL_DUMP("timeout1 = { %ld %9ld }", timeout1.tv_sec, timeout1.tv_usec)
U_RETURN_POINTER(&timeout1, struct timeval);
}
static struct timespec* getTimeSpec(UEventTime* ptimeout)
{
U_TRACE(0, "UEventTime::getTimeSpec(%p)", ptimeout)
if (ptimeout == 0) U_RETURN_POINTER(0, struct timespec);
U_ASSERT(ptimeout->checkMilliSecond())
/**
* struct timespec {
* time_t tv_sec; // seconds
* long tv_nsec; // nanoseconds
* };
*/
long ns = ms * 1000000L;
timeout2.tv_sec = ns / 1000000000L;
timeout2.tv_nsec = ns % 1000000000L;
U_INTERNAL_DUMP("timeout2 = { %ld %9ld }", timeout2.tv_sec, timeout2.tv_nsec)
U_RETURN_POINTER(&timeout2, struct timespec);
}
private:
#ifdef U_COMPILER_DELETE_MEMBERS
UEventTime& operator=(const UEventTime&) = delete;
#else
UEventTime& operator=(const UEventTime&) { return *this; }
#endif
friend class UTimer;
friend class UNotifier;
};
#endif