LCOV - code coverage report
Current view: top level - eventdispatcher - tcp_server_client_buffer_connection.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 75 1.3 %
Date: 2021-09-19 09:06:58 Functions: 2 10 20.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13