Line data Source code
1 : // Copyright (c) 2012-2021 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] communicator The communicator controlling this connection.
71 : * \param[in] timeout_us The timeout in microseconds.
72 : */
73 1 : timer::timer(std::int64_t timeout_us)
74 : {
75 1 : if(timeout_us == 0)
76 : {
77 : // if zero, we assume that the timeout is a one time trigger
78 : // and that it will be set to other dates at other later times
79 : //
80 1 : set_timeout_date(get_current_date());
81 : }
82 : else
83 : {
84 0 : set_timeout_delay(timeout_us);
85 : }
86 1 : }
87 :
88 :
89 : /** \brief Retrieve the socket of the timer object.
90 : *
91 : * Timer objects are never attached to a socket so this function always
92 : * returns -1.
93 : *
94 : * \note
95 : * You should not override this function since there is not other
96 : * value it can return.
97 : *
98 : * \return Always -1.
99 : */
100 1 : int timer::get_socket() const
101 : {
102 1 : return -1;
103 : }
104 :
105 :
106 : /** \brief Tell that the socket is always valid.
107 : *
108 : * This function always returns true since the timer never uses a socket.
109 : *
110 : * \return Always true.
111 : */
112 1 : bool timer::valid_socket() const
113 : {
114 1 : return true;
115 : }
116 :
117 :
118 :
119 6 : } // namespace ed
120 : // vim: ts=4 sw=4 et
|