Line data Source code
1 : // Copyright (c) 2012-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : /** \file
21 : * \brief Implementation of the Timer class.
22 : *
23 : * This class allows you to create a "connection" which is just a timer.
24 : *
25 : * All connections have a timer feature, but at times you have to either
26 : * disable a connection or you already use the timer for some other
27 : * reasons so we offer a separate timer class for you additional needs.
28 : */
29 :
30 :
31 : // self
32 : //
33 : #include "eventdispatcher/timer.h"
34 :
35 : #include "eventdispatcher/utils.h"
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 :
44 :
45 : namespace ed
46 : {
47 :
48 :
49 :
50 : /** \brief Initializes the timer object.
51 : *
52 : * This function initializes the timer object with the specified \p timeout
53 : * defined in microseconds.
54 : *
55 : * Note that by default all connection objects are marked as persistent
56 : * since in most cases that is the type of connections you are interested
57 : * in. Therefore timers are also marked as persistent. This means if you
58 : * want a one time callback, you want to call the remove_connection()
59 : * function with your timer from your callback.
60 : *
61 : * \note
62 : * POSIX offers timers (in Linux since kernel version 2.6), only
63 : * (a) these generate signals, which is generally considered slow
64 : * in comparison to a timeout assigned to the poll() function, and
65 : * (b) the kernel posts at most one timer signal at a time across
66 : * one process, in other words, if 5 timers time out before you are
67 : * given a chance to process the timer, you only get one single
68 : * signal.
69 : *
70 : * \param[in] timeout_us The timeout in microseconds.
71 : */
72 1 : timer::timer(std::int64_t timeout_us)
73 : {
74 1 : if(timeout_us == 0)
75 : {
76 : // if zero, we assume that the timeout is a one time trigger
77 : // and that it will be set to other dates at other later times
78 : //
79 1 : set_timeout_date(get_current_date());
80 : }
81 : else
82 : {
83 0 : set_timeout_delay(timeout_us);
84 : }
85 1 : }
86 :
87 :
88 : /** \brief Retrieve the socket of the timer object.
89 : *
90 : * Timer objects are never attached to a socket so this function always
91 : * returns -1.
92 : *
93 : * \note
94 : * You should not override this function since there is not other
95 : * value it can return.
96 : *
97 : * \return Always -1.
98 : */
99 1 : int timer::get_socket() const
100 : {
101 1 : return -1;
102 : }
103 :
104 :
105 : /** \brief Tell that the socket is always valid.
106 : *
107 : * This function always returns true since the timer never uses a socket.
108 : *
109 : * \return Always true.
110 : */
111 1 : bool timer::valid_socket() const
112 : {
113 1 : return true;
114 : }
115 :
116 :
117 :
118 6 : } // namespace ed
119 : // vim: ts=4 sw=4 et
|