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 : // self
43 : //
44 : #include "eventdispatcher/tcp_server_client_connection.h"
45 :
46 : #include "eventdispatcher/exception.h"
47 :
48 :
49 : // snaplogger lib
50 : //
51 : #include <snaplogger/message.h>
52 :
53 :
54 : // C++ lib
55 : //
56 : #include <cstring>
57 :
58 :
59 : // C lib
60 : //
61 : #include <arpa/inet.h>
62 : #include <netdb.h>
63 :
64 :
65 : // last include
66 : //
67 : #include <snapdev/poison.h>
68 :
69 :
70 :
71 : namespace ed
72 : {
73 :
74 :
75 :
76 : /** \brief Create a client connection created from an accept().
77 : *
78 : * This constructor initializes a client connection from a socket
79 : * that we received from an accept() call.
80 : *
81 : * The destructor will automatically close that socket on destruction.
82 : *
83 : * \param[in] client The client that accept() returned.
84 : */
85 0 : tcp_server_client_connection::tcp_server_client_connection(tcp_bio_client::pointer_t client)
86 0 : : f_client(client)
87 : {
88 0 : }
89 :
90 :
91 : /** \brief Make sure the socket gets released.
92 : *
93 : * This destructor makes sure that the socket gets closed.
94 : */
95 0 : tcp_server_client_connection::~tcp_server_client_connection()
96 : {
97 0 : close();
98 0 : }
99 :
100 :
101 : /** \brief Read data from the TCP server client socket.
102 : *
103 : * This function reads as much data up to the specified amount
104 : * in \p count. The read data is saved in \p buf.
105 : *
106 : * \param[in,out] buf The buffer where the data gets read.
107 : * \param[in] count The maximum number of bytes to read in buf.
108 : *
109 : * \return The number of bytes read or -1 if an error occurred.
110 : */
111 0 : ssize_t tcp_server_client_connection::read(void * buf, size_t count)
112 : {
113 0 : if(!f_client)
114 : {
115 0 : errno = EBADF;
116 0 : return -1;
117 : }
118 0 : return f_client->read(reinterpret_cast<char *>(buf), count);
119 : }
120 :
121 :
122 : /** \brief Write data to this connection's socket.
123 : *
124 : * This function writes up to \p count bytes of data from \p buf
125 : * to this connection's socket.
126 : *
127 : * \warning
128 : * This write function may not always write all the data you are
129 : * trying to send to the remote connection. If you want to make
130 : * sure that all your data is written to the other connection,
131 : * you want to instead use the tcp_server_client_buffer_connection
132 : * which overloads the write() function and saves the data to be
133 : * written to the socket in a buffer. The communicator run
134 : * loop is then responsible for sending all the data.
135 : *
136 : * \param[in] buf The buffer of data to be written to the socket.
137 : * \param[in] count The number of bytes the caller wants to write to the
138 : * connection.
139 : *
140 : * \return The number of bytes written to the socket or -1 if an error occurred.
141 : */
142 0 : ssize_t tcp_server_client_connection::write(void const * buf, size_t count)
143 : {
144 0 : if(!f_client)
145 : {
146 0 : errno = EBADF;
147 0 : return -1;
148 : }
149 0 : return f_client->write(reinterpret_cast<char const *>(buf), count);
150 : }
151 :
152 :
153 : /** \brief Close the socket of this connection.
154 : *
155 : * This function is automatically called whenever the object gets
156 : * destroyed (see destructor) or detects that the client closed
157 : * the network connection.
158 : *
159 : * Connections cannot be reopened.
160 : */
161 0 : void tcp_server_client_connection::close()
162 : {
163 0 : f_client.reset();
164 0 : }
165 :
166 :
167 : /** \brief Retrieve the socket of this connection.
168 : *
169 : * This function returns the socket defined in this connection.
170 : *
171 : * \return The socket file descriptor or -1 if the connection is closed.
172 : */
173 0 : int tcp_server_client_connection::get_socket() const
174 : {
175 0 : if(f_client == nullptr)
176 : {
177 : // client connection was closed
178 : //
179 0 : return -1;
180 : }
181 0 : return f_client->get_socket();
182 : }
183 :
184 :
185 : /** \brief Tell that we are always a reader.
186 : *
187 : * This function always returns true meaning that the connection is
188 : * always of a reader. In most cases this is safe because if nothing
189 : * is being written to you then poll() never returns so you do not
190 : * waste much time in have a TCP connection always marked as a
191 : * reader.
192 : *
193 : * \return The events to listen to for this connection.
194 : */
195 0 : bool tcp_server_client_connection::is_reader() const
196 : {
197 0 : return true;
198 : }
199 :
200 :
201 : /** \brief Retrieve a copy of the client's address.
202 : *
203 : * This function retrieves a copy of the client's address and returns it.
204 : *
205 : * \return A reference to the client's address.
206 : */
207 0 : addr::addr tcp_server_client_connection::get_client_address()
208 : {
209 0 : if(f_address.is_default())
210 : {
211 0 : int const s(get_socket());
212 0 : if(s >= 0)
213 : {
214 0 : f_address.set_from_socket(s, false);
215 : }
216 : }
217 :
218 0 : return f_address;
219 : }
220 :
221 :
222 :
223 6 : } // namespace ed
224 : // vim: ts=4 sw=4 et
|