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