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 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/tcp_client_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 Initializes the client connection.
60 : *
61 : * This function creates a connection using the address, port, and mode
62 : * parameters. This is very similar to using the bio_client class to
63 : * create a connection, only the resulting connection can be used with
64 : * the communicator object.
65 : *
66 : * \note
67 : * The function also saves the remote address and port used to open
68 : * the connection which can later be retrieved using the
69 : * get_remote_address() function. That address will remain valid
70 : * even after the socket is closed.
71 : *
72 : * \todo
73 : * If the remote address is an IPv6, we need to put it between [...]
74 : * (i.e. [::1]:4040) so we can extract the port safely.
75 : *
76 : * \param[in] addr The address of the server to connect to.
77 : * \param[in] port The port to connect to.
78 : * \param[in] mode Type of connection: plain or secure.
79 : */
80 0 : tcp_client_connection::tcp_client_connection(
81 : std::string const & addr
82 : , int port
83 0 : , mode_t mode)
84 : : tcp_bio_client(addr, port, mode)
85 0 : , f_remote_address(get_client_addr() + ":" + std::to_string(get_client_port()))
86 : {
87 0 : }
88 :
89 :
90 : /** \brief Retrieve the remote address information.
91 : *
92 : * This function can be used to retrieve the remove address and port
93 : * information as was specified on the constructor. These can be used
94 : * to find this specific connection at a later time or create another
95 : * connection.
96 : *
97 : * For example, you may get 192.168.2.17:4040.
98 : *
99 : * The function works even after the socket gets closed as we save
100 : * the remote address and port in a string just after the connection
101 : * was established.
102 : *
103 : * \note
104 : * These parameters are the same as what was passed to the constructor,
105 : * only both will have been converted to numbers. So for example when
106 : * you used "localhost", here you get "::1" or "127.0.0.1" for the
107 : * address.
108 : *
109 : * \return The remote host address and connection port.
110 : */
111 0 : std::string const & tcp_client_connection::get_remote_address() const
112 : {
113 0 : return f_remote_address;
114 : }
115 :
116 :
117 : /** \brief Read from the client socket.
118 : *
119 : * This function reads data from the client socket and copy it in
120 : * \p buf. A maximum of \p count bytes are read.
121 : *
122 : * \param[in,out] buf The buffer where the data is read.
123 : * \param[in] count The maximum number of bytes to read.
124 : *
125 : * \return -1 if an error occurs, zero if no data gets read, a positive
126 : * number representing the number of bytes read otherwise.
127 : */
128 0 : ssize_t tcp_client_connection::read(void * buf, size_t count)
129 : {
130 0 : if(get_socket() == -1)
131 : {
132 0 : errno = EBADF;
133 0 : return -1;
134 : }
135 0 : return tcp_bio_client::read(reinterpret_cast<char *>(buf), count);
136 : }
137 :
138 :
139 : /** \brief Write to the client socket.
140 : *
141 : * This function writes \p count bytes from \p buf to this
142 : * client socket.
143 : *
144 : * The function can safely be called after the socket was closed, although
145 : * it will return -1 and set errno to EBADF in that case.
146 : *
147 : * \param[in] buf The buffer to write to the client connection socket.
148 : * \param[in] count The maximum number of bytes to write on this connection.
149 : *
150 : * \return -1 if an error occurs, zero if nothing was written, a positive
151 : * number representing the number of bytes successfully written.
152 : */
153 0 : ssize_t tcp_client_connection::write(void const * buf, size_t count)
154 : {
155 0 : if(get_socket() == -1)
156 : {
157 0 : errno = EBADF;
158 0 : return -1;
159 : }
160 0 : return tcp_bio_client::write(reinterpret_cast<char const *>(buf), count);
161 : }
162 :
163 :
164 : /** \brief Check whether this connection is a reader.
165 : *
166 : * We change the default to true since TCP sockets are generally
167 : * always readers. You can still overload this function and
168 : * return false if necessary.
169 : *
170 : * However, we do not overload the is_writer() because that is
171 : * much more dynamic (i.e. you do not want to advertise as
172 : * being a writer unless you have data to write to the
173 : * socket.)
174 : *
175 : * \return The events to listen to for this connection.
176 : */
177 0 : bool tcp_client_connection::is_reader() const
178 : {
179 0 : return true;
180 : }
181 :
182 :
183 : /** \brief Retrieve the socket of this client connection.
184 : *
185 : * This function retrieves the socket this client connection. In this case
186 : * the socket is defined in the bio_client class.
187 : *
188 : * \return The socket of this client connection.
189 : */
190 0 : int tcp_client_connection::get_socket() const
191 : {
192 0 : return tcp_bio_client::get_socket();
193 : }
194 :
195 :
196 :
197 : } // namespace ed
198 : // vim: ts=4 sw=4 et
|