Line data Source code
1 : // Event Dispatcher
2 : // Copyright (c) 2012-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 :
18 :
19 :
20 : // self
21 : //
22 : #include "eventdispatcher/udp_client.h"
23 :
24 :
25 : // last include
26 : //
27 : #include "snapdev/poison.h"
28 :
29 :
30 :
31 : namespace ed
32 : {
33 :
34 :
35 :
36 : /** \brief Initialize a UDP client object.
37 : *
38 : * This function initializes the UDP client object using the address and the
39 : * port as specified.
40 : *
41 : * The port is expected to be a host side port number (i.e. 59200).
42 : *
43 : * The \p addr parameter is a textual address. It may be an IPv4 or IPv6
44 : * address and it can represent a host name or an address defined with
45 : * just numbers. If the address cannot be resolved then an error occurs
46 : * and constructor throws.
47 : *
48 : * \note
49 : * The socket is open in this process. If you fork() or exec() then the
50 : * socket will be closed by the operating system.
51 : *
52 : * \warning
53 : * We only make use of the first address found by getaddrinfo(). All
54 : * the other addresses are ignored.
55 : *
56 : * \exception udp_client_server_runtime_error
57 : * The server could not be initialized properly. Either the address cannot be
58 : * resolved, the port is incompatible or not available, or the socket could
59 : * not be created.
60 : *
61 : * \param[in] addr The address to convert to a numeric IP.
62 : * \param[in] port The port number.
63 : * \param[in] family The family used to search for 'addr'.
64 : */
65 0 : udp_client::udp_client(std::string const & addr, int port, int family)
66 0 : : udp_base(addr, port, family)
67 : {
68 0 : }
69 :
70 :
71 : /** \brief Clean up the UDP client object.
72 : *
73 : * This function frees the address information structure and close the socket
74 : * before returning.
75 : */
76 0 : udp_client::~udp_client()
77 : {
78 0 : }
79 :
80 :
81 : /** \brief Send a message through this UDP client.
82 : *
83 : * This function sends \p msg through the UDP client socket. The function
84 : * cannot be used to change the destination as it was defined when creating
85 : * the udp_client object.
86 : *
87 : * The size must be small enough for the message to fit. In most cases we
88 : * use these in Snap! to send very small signals (i.e. 4 bytes commands.)
89 : * Any data we would want to share remains in the Cassandra database so
90 : * that way we can avoid losing it because of a UDP message.
91 : *
92 : * \param[in] msg The message to send.
93 : * \param[in] size The number of bytes representing this message.
94 : *
95 : * \return -1 if an error occurs, otherwise the number of bytes sent. errno
96 : * is set accordingly on error.
97 : */
98 0 : int udp_client::send(char const * msg, size_t size)
99 : {
100 0 : return static_cast<int>(sendto(f_socket.get(), msg, size, 0, f_addrinfo->ai_addr, f_addrinfo->ai_addrlen));
101 : }
102 :
103 :
104 :
105 6 : } // namespace ed
106 : // vim: ts=4 sw=4 et
|