Line data Source code
1 : // Copyright (c) 2012-2024 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] address The address and port to listen on. The address can be 66 : * the default address. 67 : * \param[in] multicast_addr A multicast address (224.x.x.x) or the default 68 : * address. 69 : */ 70 0 : udp_server_connection::udp_server_connection( 71 : addr::addr const & address 72 0 : , addr::addr const & multicast_address) 73 0 : : udp_server(address, multicast_address) 74 : { 75 0 : } 76 : 77 : 78 : /** \brief Check to know whether this UDP connection is a reader. 79 : * 80 : * This function returns true to say that this UDP connection is 81 : * indeed a reader. 82 : * 83 : * \return This function already returns true as we are likely to 84 : * always want a UDP socket to be listening for incoming 85 : * packets. 86 : */ 87 0 : bool udp_server_connection::is_reader() const 88 : { 89 0 : return true; 90 : } 91 : 92 : 93 : /** \brief Retrieve the socket of this server connection. 94 : * 95 : * This function retrieves the socket this server connection. In this case 96 : * the socket is defined in the udp_server class. 97 : * 98 : * \return The socket of this client connection. 99 : */ 100 0 : int udp_server_connection::get_socket() const 101 : { 102 0 : return udp_server::get_socket(); 103 : } 104 : 105 : 106 : /** \brief Define a secret code. 107 : * 108 : * When receiving a message through this UDP socket, this secret code must 109 : * be included in the message. If not present, then the message gets 110 : * discarded. 111 : * 112 : * By default this parameter is an empty string. This means no secret 113 : * code is required and UDP communication can be done without it. 114 : * 115 : * \note 116 : * Secret codes are expected to be used only on connections between 117 : * computers. If the IP address is 127.0.0.1, you probably don't need 118 : * to have a secret code. 119 : * 120 : * \warning 121 : * Remember that UDP messages are limited in size. If too long, the 122 : * send_message() function throws an error. So your secret code should 123 : * remain relatively small. 124 : * 125 : * \todo 126 : * The secret_code string must be a valid UTF-8 string. At this point 127 : * this is not enforced. 128 : * 129 : * \param[in] secret_code The secret code that has to be included in the 130 : * incoming messages for those to be accepted. 131 : */ 132 0 : void udp_server_connection::set_secret_code(std::string const & secret_code) 133 : { 134 0 : f_secret_code = secret_code; 135 0 : } 136 : 137 : 138 : /** \brief Retrieve the server secret code. 139 : * 140 : * This function returns the server secret code as defined with the 141 : * set_secret_code() function. By default this parameter is set to 142 : * the empty string. 143 : * 144 : * Whenever a message is received, this code is checked. If defined 145 : * in the server and not equal to the code in the message, then the 146 : * message is discarded (hackers?) 147 : * 148 : * The message is also used when sending a message. It gets added 149 : * to the message if it is not the empty string. 150 : * 151 : * \return The secret code. 152 : */ 153 0 : std::string const & udp_server_connection::get_secret_code() const 154 : { 155 0 : return f_secret_code; 156 : } 157 : 158 : 159 : 160 : } // namespace ed 161 : // vim: ts=4 sw=4 et