Coverage Report

Created: 2026-01-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/resiprocate/rutil/Timer.hxx
Line
Count
Source
1
#if !defined(RESIP_TIMER_HXX)
2
#define RESIP_TIMER_HXX 
3
4
#ifdef HAVE_CONFIG_H
5
#include "config.h"
6
#endif
7
     
8
#include "rutil/Data.hxx"
9
#include "rutil/HeapInstanceCounter.hxx"
10
#include <iostream>
11
#include "rutil/Time.hxx"
12
13
/// !slg!  TODO - a large portion of the code in this file belongs in the resip/stack as
14
/// opposed to rutil.  Candidates for move are:
15
/// 1.  All SIP Specific timer logic in Timer class
16
/// 2.  Transaction Timer
17
/// 3.  TimerWithPayload
18
19
namespace resip
20
{
21
22
class Message;
23
24
/**
25
   @brief This class is used to get the current system time.
26
27
   @note Should we refactor this? It seems like the SIP-timer stuff should live
28
      in resip/stack somewhere.
29
*/
30
class Timer
31
{
32
   public:
33
      RESIP_HeapCount(Timer);
34
      typedef enum 
35
      {
36
         TimerA, // doubling
37
         TimerB,
38
         TimerC,
39
         TimerD,
40
         TimerE1,// doubling
41
         TimerE2,// doubling
42
         TimerF,
43
         TimerG, // doubling
44
         TimerH,
45
         TimerI,
46
         TimerJ,
47
         TimerK,
48
         TimerTrying,
49
         TimerStaleClient,
50
         TimerStaleServer,
51
         TimerStateless,
52
         TimerCleanUp,
53
         ApplicationTimer, // .dlb. Fifo, so no thread issues
54
         TcpConnectTimer
55
      } Type;
56
      
57
      static Data toData(Type timer);
58
59
      /** @deprecated.  Does not do anything at the moment.
60
      */
61
      static void setupTimeOffsets()
62
1
      {}
63
64
      /** Returns the current clock time in microseconds.
65
      */
66
      static uint64_t getTimeMicroSec()
67
9.09k
      {
68
9.09k
         return ResipClock::getTimeMicroSec();
69
9.09k
      }
70
      /** Returns the current clock time in milliseconds.
71
      */
72
      static uint64_t getTimeMs()
73
0
      {
74
0
         return ResipClock::getTimeMs();
75
0
      }
76
      /** Returns the current clock time in seconds.
77
      */
78
      static uint64_t getTimeSecs()
79
0
      {
80
0
         return ResipClock::getTimeSecs();
81
0
      }
82
      /** Returns an absolute time in ms that is between 50% and 90% of
83
          passed in ms from now.
84
      */
85
      static uint64_t getRandomFutureTimeMs(uint64_t futureMs)
86
0
      {
87
0
         return ResipClock::getRandomFutureTimeMs(futureMs);
88
0
      }
89
      /** Infinit time in future.
90
      */
91
      static uint64_t getForever()
92
0
      {
93
0
         return ResipClock::getForever();
94
0
      }
95
96
      /** On windows, change getSystemTime() to not use the actual clock time
97
          returned by ::GetSystemTime().  The replacement time function on some versions of windows (< Vista,
98
          GetTickCount()) returns a 32-bit value, so it will rollover at some point.
99
          Any code that waits on timers should not wait longer than this value.
100
101
         @see resiprocate.org devlist discussion "Timers: why system time?";
102
         @see resip::SipStack::getTimeTillNextProcessMS().
103
      */
104
      static unsigned getMaxSystemTimeWaitMs(void)
105
0
      {
106
0
         return ResipClock::getMaxSystemTimeWaitMs();
107
0
      }
108
109
      static void resetT1(unsigned long t1);
110
111
      // These values can be changed but it is not recommended to do so after a
112
      // stack is up and running since they are not mutexed
113
      // These times are all in ms 
114
      static unsigned long T1;
115
      static unsigned long T2;
116
      static unsigned long T4;
117
      static unsigned long T100;
118
      
119
      static unsigned long TB; // default 64*T1
120
      static unsigned long TC; 
121
      static unsigned long TF; // default 64*T1
122
      static unsigned long TH; // default 64*T1
123
      
124
      static unsigned long TD;
125
      static unsigned long TS;
126
127
      static unsigned long TcpConnectTimeout;
128
};
129
130
// !bwc! There is some duplicated code between TransactionTimer and 
131
// TimerWithPayload. Should eventually create a single template class like the 
132
// following, and use it with Payload=Message* and Payload=TransactionTimer
133
// template <typename Payload>
134
// class Timestamped
135
// {
136
//    // blah blah blah
137
//    private:
138
//       UInt64 mTimestamp;
139
//       Payload mPayload;
140
// };
141
class TransactionTimer
142
{
143
   public:
144
      RESIP_HeapCount(TransactionTimer);
145
146
      TransactionTimer(unsigned long ms, 
147
                        Timer::Type type, 
148
                        const Data& transactionId);
149
150
0
      ~TransactionTimer(){}
151
152
0
      const Data& getTransactionId() const {return mTransactionId;}
153
0
      Timer::Type getType() const { return mType; }
154
0
      unsigned long getDuration() const { return mDuration;} 
155
156
0
      uint64_t getWhen() const {return mWhen;}
157
#ifndef RESIP_USE_STL_STREAMS
158
      EncodeStream& encode(EncodeStream& str) const;
159
#endif
160
      std::ostream& encode(std::ostream& str) const;
161
162
      inline bool operator<(const TransactionTimer& rhs) const
163
0
      {
164
0
         return mWhen < rhs.mWhen;
165
0
      }
166
167
      inline bool operator>(const TransactionTimer& rhs) const
168
0
      {
169
0
         return mWhen > rhs.mWhen;
170
0
      }
171
172
   protected:
173
      uint64_t mWhen;
174
      Timer::Type mType;
175
      Data mTransactionId;
176
      unsigned long mDuration; // duration of time in ms 
177
178
   private:
179
      // disabled
180
      TransactionTimer();
181
};
182
183
class TimerWithPayload
184
{
185
   public:
186
      RESIP_HeapCount(TimerWithPayload);
187
188
      TimerWithPayload(unsigned long ms, Message* message);
189
0
      ~TimerWithPayload() {}
190
191
      // return the message to queue, possibly null
192
0
      Message* getMessage() const { return mMessage; }
193
194
0
      uint64_t getWhen() const {return mWhen;}
195
#ifndef RESIP_USE_STL_STREAMS
196
      EncodeStream& encode(EncodeStream& str) const;
197
#endif
198
      std::ostream& encode(std::ostream& str) const;
199
200
      inline bool operator<(const TimerWithPayload& rhs) const
201
0
      {
202
0
         return mWhen < rhs.mWhen;
203
0
      }
204
205
      inline bool operator>(const TimerWithPayload& rhs) const
206
0
      {
207
0
         return mWhen > rhs.mWhen;
208
0
      }
209
210
   protected:
211
      uint64_t mWhen;
212
      Message* mMessage; // message to queue on timeout
213
214
   private:
215
      // disabled
216
      TimerWithPayload();
217
};
218
219
220
inline std::ostream& operator<<(std::ostream& str, const TransactionTimer& t)
221
0
{
222
0
   return t.encode(str);
223
0
}
224
225
inline std::ostream& operator<<(std::ostream& str, const TimerWithPayload& t)
226
0
{
227
0
   return t.encode(str);
228
0
}
229
230
#ifndef RESIP_USE_STL_STREAMS
231
inline EncodeStream& operator<<(EncodeStream& str, const TransactionTimer& t)
232
{
233
   return t.encode(str);
234
}
235
236
inline EncodeStream& operator<<(EncodeStream& str, const TimerWithPayload& t)
237
{
238
   return t.encode(str);
239
}
240
#endif
241
242
}
243
244
#if 0
245
Timer    Value            Section               Meaning
246
----------------------------------------------------------------------
247
T1       500ms default    Section 17.1.1.1     RTT Estimate
248
T2       4s               Section 17.1.2.2     The maximum retransmit
249
                                               interval for non-INVITE
250
                                               requests and INVITE
251
                                               responses
252
T4       5s               Section 17.1.2.2     Maximum duration a
253
                                               message will
254
                                               remain in the network
255
Timer A  initially T1     Section 17.1.1.2     INVITE request retransmit
256
                                               interval, for UDP only
257
Timer B  64*T1            Section 17.1.1.2     INVITE transaction
258
                                               timeout timer
259
Timer C  > 3min           Section 16.6         proxy INVITE transaction
260
                           bullet 11            timeout
261
Timer D  > 32s for UDP    Section 17.1.1.2     Wait time for response
262
         0s for TCP/SCTP                       retransmits
263
Timer E  initially T1     Section 17.1.2.2     non-INVITE request
264
                                               retransmit interval,
265
                                               UDP only
266
Timer F  64*T1            Section 17.1.2.2     non-INVITE transaction
267
                                               timeout timer
268
Timer G  initially T1     Section 17.2.1       INVITE response
269
                                               retransmit interval
270
Timer H  64*T1            Section 17.2.1       Wait time for
271
                                               ACK receipt
272
Timer I  T4 for UDP       Section 17.2.1       Wait time for
273
         0s for TCP/SCTP                       ACK retransmits
274
Timer J  64*T1 for UDP    Section 17.2.2       Wait time for
275
         0s for TCP/SCTP                       non-INVITE request
276
                                               retransmits
277
Timer K  T4 for UDP       Section 17.1.2.2     Wait time for
278
         0s for TCP/SCTP                       response retransmits
279
280
                   Table 4: Summary of timers
281
#endif
282
283
#endif
284
285
/* ====================================================================
286
 * The Vovida Software License, Version 1.0 
287
 * 
288
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
289
 * 
290
 * Redistribution and use in source and binary forms, with or without
291
 * modification, are permitted provided that the following conditions
292
 * are met:
293
 * 
294
 * 1. Redistributions of source code must retain the above copyright
295
 *    notice, this list of conditions and the following disclaimer.
296
 * 
297
 * 2. Redistributions in binary form must reproduce the above copyright
298
 *    notice, this list of conditions and the following disclaimer in
299
 *    the documentation and/or other materials provided with the
300
 *    distribution.
301
 * 
302
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
303
 *    and "Vovida Open Communication Application Library (VOCAL)" must
304
 *    not be used to endorse or promote products derived from this
305
 *    software without prior written permission. For written
306
 *    permission, please contact vocal@vovida.org.
307
 *
308
 * 4. Products derived from this software may not be called "VOCAL", nor
309
 *    may "VOCAL" appear in their name, without prior written
310
 *    permission of Vovida Networks, Inc.
311
 * 
312
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
313
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
314
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
315
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
316
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
317
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
318
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
319
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
320
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
321
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
322
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
323
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
324
 * DAMAGE.
325
 * 
326
 * ====================================================================
327
 * 
328
 * This software consists of voluntary contributions made by Vovida
329
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
330
 * Inc.  For more information on Vovida Networks, Inc., please see
331
 * <http://www.vovida.org/>.
332
 *
333
 */