LCOV - code coverage report
Current view: top level - eventdispatcher - tcp_client_connection.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 19 0.0 %
Date: 2019-08-10 01:48:51 Functions: 0 6 0.0 %
Legend: Lines: hit not hit

          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/tcp_client_connection.h"
      46             : 
      47             : 
      48             : // last include
      49             : //
      50             : #include    <snapdev/poison.h>
      51             : 
      52             : 
      53             : 
      54             : namespace ed
      55             : {
      56             : 
      57             : 
      58             : 
      59             : /** \brief Initializes the client connection.
      60             :  *
      61             :  * This function creates a connection using the address, port, and mode
      62             :  * parameters. This is very similar to using the bio_client class to
      63             :  * create a connection, only the resulting connection can be used with
      64             :  * the snap_communicator object.
      65             :  *
      66             :  * \note
      67             :  * The function also saves the remote address and port used to open
      68             :  * the connection which can later be retrieved using the
      69             :  * get_remote_address() function. That address will remain valid
      70             :  * even after the socket is closed.
      71             :  *
      72             :  * \todo
      73             :  * If the remote address is an IPv6, we need to put it between [...]
      74             :  * (i.e. [::1]:4040) so we can extract the port safely.
      75             :  *
      76             :  * \param[in] communicator  The snap communicator controlling this connection.
      77             :  * \param[in] addr  The address of the server to connect to.
      78             :  * \param[in] port  The port to connect to.
      79             :  * \param[in] mode  Type of connection: plain or secure.
      80             :  */
      81           0 : tcp_client_connection::tcp_client_connection(std::string const & addr, int port, mode_t mode)
      82             :     : tcp_bio_client(addr, port, mode)
      83           0 :     , f_remote_address(get_client_addr() + ":" + std::to_string(get_client_port()))
      84             : {
      85           0 : }
      86             : 
      87             : 
      88             : /** \brief Retrieve the remote address information.
      89             :  *
      90             :  * This function can be used to retrieve the remove address and port
      91             :  * information as was specified on the constructor. These can be used
      92             :  * to find this specific connection at a later time or create another
      93             :  * connection.
      94             :  *
      95             :  * For example, you may get 192.168.2.17:4040.
      96             :  *
      97             :  * The function works even after the socket gets closed as we save
      98             :  * the remote address and port in a string just after the connection
      99             :  * was established.
     100             :  *
     101             :  * \note
     102             :  * These parameters are the same as what was passed to the constructor,
     103             :  * only both will have been converted to numbers. So for example when
     104             :  * you used "localhost", here you get "::1" or "127.0.0.1" for the
     105             :  * address.
     106             :  *
     107             :  * \return The remote host address and connection port.
     108             :  */
     109           0 : std::string const & tcp_client_connection::get_remote_address() const
     110             : {
     111           0 :     return f_remote_address;
     112             : }
     113             : 
     114             : 
     115             : /** \brief Read from the client socket.
     116             :  *
     117             :  * This function reads data from the client socket and copy it in
     118             :  * \p buf. A maximum of \p count bytes are read.
     119             :  *
     120             :  * \param[in,out] buf  The buffer where the data is read.
     121             :  * \param[in] count  The maximum number of bytes to read.
     122             :  *
     123             :  * \return -1 if an error occurs, zero if no data gets read, a positive
     124             :  *         number representing the number of bytes read otherwise.
     125             :  */
     126           0 : ssize_t tcp_client_connection::read(void * buf, size_t count)
     127             : {
     128           0 :     if(get_socket() == -1)
     129             :     {
     130           0 :         errno = EBADF;
     131           0 :         return -1;
     132             :     }
     133           0 :     return tcp_bio_client::read(reinterpret_cast<char *>(buf), count);
     134             : }
     135             : 
     136             : 
     137             : /** \brief Write to the client socket.
     138             :  *
     139             :  * This function writes \p count bytes from \p buf to this
     140             :  * client socket.
     141             :  *
     142             :  * The function can safely be called after the socket was closed, although
     143             :  * it will return -1 and set errno to EBADF in tha case.
     144             :  *
     145             :  * \param[in] buf  The buffer to write to the client connection socket.
     146             :  * \param[in] count  The maximum number of bytes to write on this connection.
     147             :  *
     148             :  * \return -1 if an error occurs, zero if nothing was written, a positive
     149             :  *         number representing the number of bytes successfully written.
     150             :  */
     151           0 : ssize_t tcp_client_connection::write(void const * buf, size_t count)
     152             : {
     153           0 :     if(get_socket() == -1)
     154             :     {
     155           0 :         errno = EBADF;
     156           0 :         return -1;
     157             :     }
     158           0 :     return tcp_bio_client::write(reinterpret_cast<char const *>(buf), count);
     159             : }
     160             : 
     161             : 
     162             : /** \brief Check whether this connection is a reader.
     163             :  *
     164             :  * We change the default to true since TCP sockets are generally
     165             :  * always readers. You can still overload this function and
     166             :  * return false if necessary.
     167             :  *
     168             :  * However, we do not overload the is_writer() because that is
     169             :  * much more dynamic (i.e. you do not want to advertise as
     170             :  * being a writer unless you have data to write to the
     171             :  * socket.)
     172             :  *
     173             :  * \return The events to listen to for this connection.
     174             :  */
     175           0 : bool tcp_client_connection::is_reader() const
     176             : {
     177           0 :     return true;
     178             : }
     179             : 
     180             : 
     181             : /** \brief Retrieve the socket of this client connection.
     182             :  *
     183             :  * This function retrieves the socket this client connection. In this case
     184             :  * the socket is defined in the bio_client class.
     185             :  *
     186             :  * \return The socket of this client connection.
     187             :  */
     188           0 : int tcp_client_connection::get_socket() const
     189             : {
     190           0 :     return tcp_bio_client::get_socket();
     191             : }
     192             : 
     193             : 
     194             : 
     195             : } // namespace ed
     196             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12