Line data Source code
1 : // Copyright (c) 2012-2019 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 Snap Communicator class.
22 : *
23 : * This class wraps the C poll() interface in a C++ object with many types
24 : * of objects:
25 : *
26 : * \li Server Connections; for software that want to offer a port to
27 : * which clients can connect to; the server will call accept()
28 : * once a new client connection is ready; this results in a
29 : * Server/Client connection object
30 : * \li Client Connections; for software that want to connect to
31 : * a server; these expect the IP address and port to connect to
32 : * \li Server/Client Connections; for the server when it accepts a new
33 : * connection; in this case the server gets a socket from accept()
34 : * and creates one of these objects to handle the connection
35 : *
36 : * Using the poll() function is the easiest and allows us to listen
37 : * on pretty much any number of sockets (on my server it is limited
38 : * at 16,768 and frankly over 1,000 we probably will start to have
39 : * real slowness issues on small VPN servers.)
40 : */
41 :
42 :
43 : // self
44 : //
45 : #include "eventdispatcher/udp_server_connection.h"
46 :
47 :
48 : // last include
49 : //
50 : #include <snapdev/poison.h>
51 :
52 :
53 :
54 : namespace ed
55 : {
56 :
57 :
58 :
59 : /** \brief Initialize a UDP listener.
60 : *
61 : * This function is used to initialize a server connection, a UDP/IP
62 : * listener which wakes up whenever a send() is sent to this listener
63 : * address and port.
64 : *
65 : * \param[in] communicator The snap communicator controlling this connection.
66 : * \param[in] addr The address to listen on. It may be set to "0.0.0.0".
67 : * \param[in] port The port to listen on.
68 : */
69 0 : udp_server_connection::udp_server_connection(std::string const & addr, int port)
70 0 : : udp_server(addr, port)
71 : {
72 0 : }
73 :
74 :
75 : /** \brief Check to know whether this UDP connection is a reader.
76 : *
77 : * This function returns true to say that this UDP connection is
78 : * indeed a reader.
79 : *
80 : * \return This function already returns true as we are likely to
81 : * always want a UDP socket to be listening for incoming
82 : * packets.
83 : */
84 0 : bool udp_server_connection::is_reader() const
85 : {
86 0 : return true;
87 : }
88 :
89 :
90 : /** \brief Retrieve the socket of this server connection.
91 : *
92 : * This function retrieves the socket this server connection. In this case
93 : * the socket is defined in the udp_server class.
94 : *
95 : * \return The socket of this client connection.
96 : */
97 0 : int udp_server_connection::get_socket() const
98 : {
99 0 : return udp_server::get_socket();
100 : }
101 :
102 :
103 : /** \brief Define a secret code.
104 : *
105 : * When receiving a message through this UDP socket, this secret code must
106 : * be included in the message. If not present, then the message gets
107 : * discarded.
108 : *
109 : * By default this parameter is an empty string. This means no secret
110 : * code is required and UDP communication can be done without it.
111 : *
112 : * \note
113 : * Secret codes are expected to be used only on connections between
114 : * computers. If the IP address is 127.0.0.1, you probably don't need
115 : * to have a secret code.
116 : *
117 : * \warning
118 : * Remember that UDP messages are limited in size. If too long, the
119 : * send_message() function throws an error. So your secret code should
120 : * remain relatively small.
121 : *
122 : * \todo
123 : * The secret_code string must be a valid UTF-8 string. At this point
124 : * this is not enforced.
125 : *
126 : * \param[in] secret_code The secret code that has to be included in the
127 : * incoming messages for those to be accepted.
128 : */
129 0 : void udp_server_connection::set_secret_code(std::string const & secret_code)
130 : {
131 0 : f_secret_code = secret_code;
132 0 : }
133 :
134 :
135 : /** \brief Retrieve the server secret code.
136 : *
137 : * This function returns the server secret code as defined with the
138 : * set_secret_code() function. By default this parameter is set to
139 : * the empty string.
140 : *
141 : * Whenever a message is received, this code is checked. If defined
142 : * in the server and not equal to the code in the message, then the
143 : * message is discarded (hackers?)
144 : *
145 : * The message is also used when sending a message. It gets added
146 : * to the message if it is not the empty string.
147 : *
148 : * \return The secret code.
149 : */
150 0 : std::string const & udp_server_connection::get_secret_code() const
151 : {
152 0 : return f_secret_code;
153 : }
154 :
155 :
156 :
157 6 : } // namespace ed
158 : // vim: ts=4 sw=4 et
|