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 : // self
43 : //
44 : #include "eventdispatcher/tcp_server_client_buffer_connection.h"
45 :
46 : #include "eventdispatcher/utils.h"
47 :
48 :
49 : // snaplogger lib
50 : //
51 : #include <snaplogger/message.h>
52 :
53 :
54 : // C++ lib
55 : //
56 : #include <algorithm>
57 :
58 :
59 : // last include
60 : //
61 : #include <snapdev/poison.h>
62 :
63 :
64 :
65 : namespace ed
66 : {
67 :
68 :
69 :
70 : /** \brief Initialize a client socket.
71 : *
72 : * The client socket gets initialized with the specified 'socket'
73 : * parameter.
74 : *
75 : * If you are a pure client (opposed to a client that was just accepted)
76 : * you may want to consider using the snap_tcp_client_buffer_connection
77 : * instead. That gives you a way to open the socket from a set of address
78 : * and port definitions among other things.
79 : *
80 : * This initialization, so things work as expected in our environment,
81 : * the function marks the socket as non-blocking. This is important for
82 : * the reader and writer capabilities.
83 : *
84 : * \param[in] client The client to be used for reading and writing.
85 : */
86 0 : tcp_server_client_buffer_connection::tcp_server_client_buffer_connection(tcp_bio_client::pointer_t client)
87 0 : : tcp_server_client_connection(client)
88 : {
89 0 : non_blocking();
90 0 : }
91 :
92 :
93 : /** \brief Check whether this connection still has some input in its buffer.
94 : *
95 : * This function returns true if there is partial incoming data in this
96 : * object's buffer.
97 : *
98 : * \return true if some buffered input is waiting for completion.
99 : */
100 0 : bool tcp_server_client_buffer_connection::has_input() const
101 : {
102 0 : return !f_line.empty();
103 : }
104 :
105 :
106 :
107 : /** \brief Check whether this connection still has some output in its buffer.
108 : *
109 : * This function returns true if there is still some output in the client
110 : * buffer. Output is added by the write() function, which is called by
111 : * the send_message() function.
112 : *
113 : * \return true if some buffered output is waiting to be sent out.
114 : */
115 0 : bool tcp_server_client_buffer_connection::has_output() const
116 : {
117 0 : return !f_output.empty();
118 : }
119 :
120 :
121 :
122 : /** \brief Tells that this connection is a writer when we have data to write.
123 : *
124 : * This function checks to know whether there is data to be writen to
125 : * this connection socket. If so then the function returns true. Otherwise
126 : * it just returns false.
127 : *
128 : * This happens whenever you called the write() function and our cache
129 : * is not empty yet.
130 : *
131 : * \return true if there is data to write to the socket, false otherwise.
132 : */
133 0 : bool tcp_server_client_buffer_connection::is_writer() const
134 : {
135 0 : return get_socket() != -1 && !f_output.empty();
136 : }
137 :
138 :
139 : /** \brief Write data to the connection.
140 : *
141 : * This function can be used to send data to this TCP/IP connection.
142 : * The data is bufferized and as soon as the connection can WRITE
143 : * to the socket, it will wake up and send the data. In other words,
144 : * we cannot just sleep and wait for an answer. The transfer will
145 : * be asynchroneous.
146 : *
147 : * \todo
148 : * Determine whether we may end up with really large buffers that
149 : * grow for a long time. This function only inserts and the
150 : * process_signal() function only reads some of the bytes but it
151 : * does not reduce the size of the buffer until all the data was
152 : * sent.
153 : *
154 : * \param[in] data The pointer to the buffer of data to be sent.
155 : * \param[out] length The number of bytes to send.
156 : */
157 0 : ssize_t tcp_server_client_buffer_connection::write(void const * data, size_t const length)
158 : {
159 0 : if(get_socket() == -1)
160 : {
161 0 : errno = EBADF;
162 0 : return -1;
163 : }
164 :
165 0 : if(data != nullptr && length > 0)
166 : {
167 0 : char const * d(reinterpret_cast<char const *>(data));
168 0 : f_output.insert(f_output.end(), d, d + length);
169 0 : return length;
170 : }
171 :
172 0 : return 0;
173 : }
174 :
175 :
176 : /** \brief Read and process as much data as possible.
177 : *
178 : * This function reads as much incoming data as possible and processes
179 : * it.
180 : *
181 : * If the input includes a newline character ('\n') then this function
182 : * calls the process_line() callback which can further process that
183 : * line of data.
184 : *
185 : * \todo
186 : * Look into a way, if possible, to have a single instantiation since
187 : * as far as I know this code matches the one written in the
188 : * process_read() of the snap_tcp_client_buffer_connection and
189 : * the snap_pipe_buffer_connection classes.
190 : */
191 0 : void tcp_server_client_buffer_connection::process_read()
192 : {
193 : // we read one character at a time until we get a '\n'
194 : // since we have a non-blocking socket we can read as
195 : // much as possible and then check for a '\n' and keep
196 : // any extra data in a cache.
197 : //
198 0 : if(get_socket() != -1)
199 : {
200 0 : int count_lines(0);
201 0 : std::int64_t const date_limit(get_current_date() + get_processing_time_limit());
202 0 : std::vector<char> buffer;
203 0 : buffer.resize(1024);
204 0 : for(;;)
205 : {
206 0 : errno = 0;
207 0 : ssize_t const r(read(&buffer[0], buffer.size()));
208 0 : if(r > 0)
209 : {
210 0 : for(ssize_t position(0); position < r; )
211 : {
212 0 : std::vector<char>::const_iterator it(std::find(buffer.begin() + position, buffer.begin() + r, '\n'));
213 0 : if(it == buffer.begin() + r)
214 : {
215 : // no newline, just add the whole thing
216 0 : f_line += std::string(&buffer[position], r - position);
217 0 : break; // do not waste time, we know we are done
218 : }
219 :
220 : // retrieve the characters up to the newline
221 : // character and process the line
222 : //
223 0 : f_line += std::string(&buffer[position], it - buffer.begin() - position);
224 0 : process_line(f_line);
225 0 : ++count_lines;
226 :
227 : // done with that line
228 : //
229 0 : f_line.clear();
230 :
231 : // we had a newline, we may still have some data
232 : // in that buffer; (+1 to skip the '\n' itself)
233 : //
234 0 : position = it - buffer.begin() + 1;
235 : }
236 :
237 : // when we reach here all the data read in `buffer` is
238 : // now either fully processed or in f_line
239 : //
240 : // TODO: change the way this works so we can test the
241 : // limit after each process_line() call
242 : //
243 0 : if(count_lines >= get_event_limit()
244 0 : || get_current_date() >= date_limit)
245 : {
246 : // we reach one or both limits, stop processing so
247 : // the other events have a chance to run
248 : //
249 0 : break;
250 : }
251 : }
252 0 : else if(r == 0 || errno == 0 || errno == EAGAIN || errno == EWOULDBLOCK)
253 : {
254 : // no more data available at this time
255 : break;
256 : }
257 : else //if(r < 0)
258 : {
259 0 : int const e(errno);
260 : SNAP_LOG_WARNING
261 0 : << "an error occurred while reading from socket (errno: "
262 0 : << e
263 0 : << " -- "
264 0 : << strerror(e)
265 0 : << ").";
266 0 : process_error();
267 0 : return;
268 : }
269 : }
270 : }
271 :
272 : // process next level too
273 0 : tcp_server_client_connection::process_read();
274 : }
275 :
276 :
277 : /** \brief Write to the connection's socket.
278 : *
279 : * This function implementation writes as much data as possible to the
280 : * connection's socket.
281 : *
282 : * This function calls the process_empty_buffer() callback whenever the
283 : * output buffer goes empty.
284 : */
285 0 : void tcp_server_client_buffer_connection::process_write()
286 : {
287 0 : if(get_socket() != -1)
288 : {
289 0 : errno = 0;
290 0 : ssize_t const r(tcp_server_client_connection::write(&f_output[f_position], f_output.size() - f_position));
291 0 : if(r > 0)
292 : {
293 : // some data was written
294 0 : f_position += r;
295 0 : if(f_position >= f_output.size())
296 : {
297 0 : f_output.clear();
298 0 : f_position = 0;
299 0 : process_empty_buffer();
300 : }
301 : }
302 0 : else if(r != 0 && errno != 0 && errno != EAGAIN && errno != EWOULDBLOCK)
303 : {
304 : // connection is considered bad, get rid of it
305 : //
306 0 : int const e(errno);
307 : SNAP_LOG_ERROR
308 0 : << "an error occurred while writing to socket of \""
309 0 : << get_name()
310 0 : << "\" (errno: "
311 0 : << e
312 0 : << " -- "
313 0 : << strerror(e)
314 0 : << ").";
315 0 : process_error();
316 0 : return;
317 : }
318 : }
319 :
320 : // process next level too
321 0 : tcp_server_client_connection::process_write();
322 : }
323 :
324 :
325 : /** \brief The remote hanged up.
326 : *
327 : * This function makes sure that the local connection gets closed properly.
328 : */
329 0 : void tcp_server_client_buffer_connection::process_hup()
330 : {
331 : // this connection is dead...
332 : //
333 0 : close();
334 :
335 0 : tcp_server_client_connection::process_hup();
336 0 : }
337 :
338 :
339 :
340 6 : } // namespace ed
341 : // vim: ts=4 sw=4 et
|