LCOV - code coverage report
Current view: top level - eventdispatcher - tcp_client_buffer_connection.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 76 1.3 %
Date: 2022-06-18 10:10:36 Functions: 2 10 20.0 %
Legend: Lines: hit not hit

          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_buffer_connection.h"
      46             : 
      47             : #include    "eventdispatcher/utils.h"
      48             : 
      49             : 
      50             : // snaplogger
      51             : //
      52             : #include    <snaplogger/message.h>
      53             : 
      54             : 
      55             : // C++
      56             : //
      57             : #include    <algorithm>
      58             : #include    <cstring>
      59             : 
      60             : 
      61             : // last include
      62             : //
      63             : #include    <snapdev/poison.h>
      64             : 
      65             : 
      66             : 
      67             : namespace ed
      68             : {
      69             : 
      70             : 
      71             : 
      72             : /** \brief Initialize a client socket.
      73             :  *
      74             :  * The client socket gets initialized with the specified 'socket'
      75             :  * parameter.
      76             :  *
      77             :  * This constructor creates a writer connection too. This gives you
      78             :  * a read/write connection. You can get the writer with the writer()
      79             :  * function. So you may write data with:
      80             :  *
      81             :  * \code
      82             :  *      my_reader.writer().write(buf, buf_size);
      83             :  * \endcode
      84             :  *
      85             :  * \param[in] address  The address to connect to.
      86             :  * \param[in] mode  The mode to connect as (PLAIN or SECURE).
      87             :  * \param[in] blocking  If true, keep a blocking socket, other non-blocking.
      88             :  */
      89           0 : tcp_client_buffer_connection::tcp_client_buffer_connection(
      90             :               addr::addr const & address
      91             :             , mode_t const mode
      92           0 :             , bool const blocking)
      93           0 :     : tcp_client_connection(address, mode)
      94             : {
      95           0 :     if(!blocking)
      96             :     {
      97           0 :         non_blocking();
      98             :     }
      99           0 : }
     100             : 
     101             : 
     102             : /** \brief Check whether this connection still has some input in its buffer.
     103             :  *
     104             :  * This function returns true if there is partial incoming data in this
     105             :  * object's buffer.
     106             :  *
     107             :  * \return true if some buffered input is waiting for completion.
     108             :  */
     109           0 : bool tcp_client_buffer_connection::has_input() const
     110             : {
     111           0 :     return !f_line.empty();
     112             : }
     113             : 
     114             : 
     115             : 
     116             : /** \brief Check whether this connection still has some output in its buffer.
     117             :  *
     118             :  * This function returns true if there is still some output in the client
     119             :  * buffer. Output is added by the write() function, which is called by
     120             :  * the send_message() function.
     121             :  *
     122             :  * \return true if some buffered output is waiting to be sent out.
     123             :  */
     124           0 : bool tcp_client_buffer_connection::has_output() const
     125             : {
     126           0 :     return !f_output.empty();
     127             : }
     128             : 
     129             : 
     130             : 
     131             : /** \brief Write data to the connection.
     132             :  *
     133             :  * This function can be used to send data to this TCP/IP connection.
     134             :  * The data is bufferized and as soon as the connection can WRITE
     135             :  * to the socket, it will wake up and send the data. In other words,
     136             :  * we cannot just sleep and wait for an answer. The transfer will
     137             :  * be asynchronous.
     138             :  *
     139             :  * \todo
     140             :  * Optimization: look into writing the \p data buffer directly in
     141             :  * the socket if the f_output cache is empty. If that works then
     142             :  * we can completely bypass our intermediate cache. This works only
     143             :  * if we make sure that the socket is non-blocking, though.
     144             :  *
     145             :  * \todo
     146             :  * Determine whether we may end up with really large buffers that
     147             :  * grow for a long time. This function only inserts and the
     148             :  * process_signal() function only reads some of the bytes but it
     149             :  * does not reduce the size of the buffer until all the data was
     150             :  * sent.
     151             :  *
     152             :  * \param[in] data  The pointer to the buffer of data to be sent.
     153             :  * \param[out] length  The number of bytes to send.
     154             :  *
     155             :  * \return The number of bytes that were saved in our buffer, 0 if
     156             :  *         no data was written to the buffer (i.e. length is zero or data
     157             :  *         is a null pointer). Or -1 on an error (i.e. the socket is closed).
     158             :  */
     159           0 : ssize_t tcp_client_buffer_connection::write(void const * data, size_t length)
     160             : {
     161           0 :     if(get_socket() == -1)
     162             :     {
     163           0 :         errno = EBADF;
     164           0 :         return -1;
     165             :     }
     166             : 
     167           0 :     if(data != nullptr && length > 0)
     168             :     {
     169           0 :         char const * d(reinterpret_cast<char const *>(data));
     170           0 :         f_output.insert(f_output.end(), d, d + length);
     171           0 :         return length;
     172             :     }
     173             : 
     174           0 :     return 0;
     175             : }
     176             : 
     177             : 
     178             : /** \brief The buffer is a writer when the output buffer is not empty.
     179             :  *
     180             :  * This function returns true as long as the output buffer of this
     181             :  * client connection is not empty.
     182             :  *
     183             :  * \return true if the output buffer is not empty, false otherwise.
     184             :  */
     185           0 : bool tcp_client_buffer_connection::is_writer() const
     186             : {
     187           0 :     return get_socket() != -1 && !f_output.empty();
     188             : }
     189             : 
     190             : 
     191             : /** \brief Instantiation of process_read().
     192             :  *
     193             :  * This function reads incoming data from a socket.
     194             :  *
     195             :  * The function is what manages our low level TCP/IP connection protocol
     196             :  * which is to read one line of data (i.e. bytes up to the next '\\n'
     197             :  * character; note that '\\r' are not understood.)
     198             :  *
     199             :  * Once a complete line of data was read, it is converted to UTF-8 and
     200             :  * sent to the next layer using the process_line() function passing
     201             :  * the line it just read (without the '\\n') to that callback.
     202             :  *
     203             :  * \sa process_write()
     204             :  * \sa process_line()
     205             :  */
     206           0 : void tcp_client_buffer_connection::process_read()
     207             : {
     208             :     // we read one character at a time until we get a '\n'
     209             :     // since we have a non-blocking socket we can read as
     210             :     // much as possible and then check for a '\n' and keep
     211             :     // any extra data in a cache.
     212             :     //
     213           0 :     if(get_socket() != -1)
     214             :     {
     215           0 :         int count_lines(0);
     216           0 :         std::int64_t const date_limit(get_current_date() + get_processing_time_limit());
     217           0 :         std::vector<char> buffer;
     218           0 :         buffer.resize(1024);
     219             :         for(;;)
     220             :         {
     221           0 :             errno = 0;
     222           0 :             ssize_t const r(read(&buffer[0], buffer.size()));
     223           0 :             if(r > 0)
     224             :             {
     225           0 :                 for(ssize_t position(0); position < r; )
     226             :                 {
     227           0 :                     std::vector<char>::const_iterator it(std::find(buffer.begin() + position, buffer.begin() + r, '\n'));
     228           0 :                     if(it == buffer.begin() + r)
     229             :                     {
     230             :                         // no newline, just add the whole thing
     231           0 :                         f_line += std::string(&buffer[position], r - position);
     232           0 :                         break; // do not waste time, we know we are done
     233             :                     }
     234             : 
     235             :                     // retrieve the characters up to the newline
     236             :                     // character and process the line
     237             :                     //
     238           0 :                     f_line += std::string(&buffer[position], it - buffer.begin() - position);
     239           0 :                     process_line(f_line);
     240           0 :                     ++count_lines;
     241             : 
     242             :                     // done with that line
     243             :                     //
     244           0 :                     f_line.clear();
     245             : 
     246             :                     // we had a newline, we may still have some data
     247             :                     // in that buffer; (+1 to skip the '\n' itself)
     248             :                     //
     249           0 :                     position = it - buffer.begin() + 1;
     250             :                 }
     251             : 
     252             :                 // when we reach here all the data read in `buffer` is
     253             :                 // now either fully processed or in f_line
     254             :                 //
     255             :                 // TODO: change the way this works so we can test the
     256             :                 //       limit after each process_line() call
     257             :                 //
     258           0 :                 if(count_lines >= get_event_limit()
     259           0 :                 || get_current_date() >= date_limit)
     260             :                 {
     261             :                     // we reach one or both limits, stop processing so
     262             :                     // the other events have a chance to run
     263             :                     //
     264           0 :                     break;
     265             :                 }
     266             :             }
     267           0 :             else if(r == 0 || errno == 0 || errno == EAGAIN || errno == EWOULDBLOCK)
     268             :             {
     269             :                 // no more data available at this time
     270             :                 break;
     271             :             }
     272             :             else //if(r < 0)
     273             :             {
     274             :                 // TODO: do something about the error
     275           0 :                 int const e(errno);
     276           0 :                 SNAP_LOG_ERROR
     277           0 :                     << "an error occurred while reading from socket (errno: "
     278             :                     << e
     279             :                     << " -- "
     280           0 :                     << strerror(e)
     281             :                     << ")."
     282             :                     << SNAP_LOG_SEND;
     283           0 :                 process_error();
     284           0 :                 return;
     285             :             }
     286           0 :         }
     287             :     }
     288             : 
     289             :     // process next level too
     290           0 :     tcp_client_connection::process_read();
     291             : }
     292             : 
     293             : 
     294             : /** \brief Instantiation of process_write().
     295             :  *
     296             :  * This function writes outgoing data to a socket.
     297             :  *
     298             :  * This function manages our own internal cache, which we use to allow
     299             :  * for out of synchronization (non-blocking) output.
     300             :  *
     301             :  * When the output buffer goes empty, this function calls the
     302             :  * process_empty_buffer() callback.
     303             :  *
     304             :  * \sa write()
     305             :  * \sa process_read()
     306             :  * \sa process_empty_buffer()
     307             :  */
     308           0 : void tcp_client_buffer_connection::process_write()
     309             : {
     310           0 :     if(get_socket() != -1)
     311             :     {
     312           0 :         errno = 0;
     313           0 :         ssize_t const r(tcp_client_connection::write(&f_output[f_position], f_output.size() - f_position));
     314           0 :         if(r > 0)
     315             :         {
     316             :             // some data was written
     317           0 :             f_position += r;
     318           0 :             if(f_position >= f_output.size())
     319             :             {
     320           0 :                 f_output.clear();
     321           0 :                 f_position = 0;
     322           0 :                 process_empty_buffer();
     323             :             }
     324             :         }
     325           0 :         else if(r < 0 && errno != 0 && errno != EAGAIN && errno != EWOULDBLOCK)
     326             :         {
     327             :             // connection is considered bad, generate an error
     328             :             //
     329           0 :             int const e(errno);
     330           0 :             SNAP_LOG_ERROR
     331             :                 << "an error occurred while writing to socket of \""
     332           0 :                 << get_name()
     333           0 :                 << "\" (errno: "
     334             :                 << e
     335             :                 << " -- "
     336           0 :                 << strerror(e)
     337             :                 << ")."
     338             :                 << SNAP_LOG_SEND;
     339           0 :             process_error();
     340           0 :             return;
     341             :         }
     342             :     }
     343             : 
     344             :     // process next level too
     345           0 :     tcp_client_connection::process_write();
     346             : }
     347             : 
     348             : 
     349             : /** \brief The hang up event occurred.
     350             :  *
     351             :  * This function closes the socket and then calls the previous level
     352             :  * hang up code which removes this connection from the communicator
     353             :  * object it was last added in.
     354             :  */
     355           0 : void tcp_client_buffer_connection::process_hup()
     356             : {
     357             :     // this connection is dead...
     358             :     //
     359           0 :     close();
     360             : 
     361             :     // process next level too
     362           0 :     tcp_client_connection::process_hup();
     363           0 : }
     364             : 
     365             : 
     366             : /** \fn tcp_client_buffer_connection::process_line(std::string const & line);
     367             :  * \brief Process a line of data.
     368             :  *
     369             :  * This is the default virtual class that can be overridden to implement
     370             :  * your own processing. By default this function does nothing.
     371             :  *
     372             :  * \note
     373             :  * At this point I implemented this function so one can instantiate
     374             :  * a tcp_server_client_buffer_connection without having to
     375             :  * derive it, although I do not think that is 100% proper.
     376             :  *
     377             :  * \param[in] line  The line of data that was just read from the input
     378             :  *                  socket.
     379             :  */
     380             : 
     381             : 
     382             : 
     383           6 : } // namespace ed
     384             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13