Main MRPT website > C++ reference
MRPT logo
CClientTCPSocket.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CClientTCPSocket_H
00029 #define  CClientTCPSocket_H
00030 
00031 #include <mrpt/config.h>
00032 #include <mrpt/utils/utils_defs.h>
00033 #include <mrpt/system/os.h>
00034 #include <mrpt/utils/CStream.h>
00035 
00036 namespace mrpt
00037 {
00038 namespace utils
00039 {
00040         class CServerTCPSocket;
00041         class CMessage;
00042 
00043         /** \defgroup network_grp Networking, sockets, DNS
00044           * \ingroup mrpt_base_grp */
00045 
00046         /** A TCP socket that can be connected to a TCP server, implementing MRPT's CStream interface for passing objects as well as generic read/write methods.
00047           *  Unless otherwise noticed, operations are blocking.
00048           *
00049           *  Note that for convenience, DNS lookup is performed with a timeout (default=3000ms), which can be changed by the static member CClientTCPSocket::DNS_LOOKUP_TIMEOUT_MS
00050           * \ingroup network_grp
00051           */
00052         class BASE_IMPEXP CClientTCPSocket : public CStream
00053         {
00054                 friend class CServerTCPSocket;
00055 
00056         public:
00057                 /** See description of CClientTCPSocket */
00058                 static unsigned int DNS_LOOKUP_TIMEOUT_MS;
00059 
00060         protected:
00061 
00062 #ifdef MRPT_OS_WINDOWS
00063                 /** The handle for the connected TCP socket, or INVALID_SOCKET
00064                   */
00065 #       if MRPT_WORD_SIZE==64
00066                 uint64_t        m_hSock;
00067 #       else
00068                 uint32_t        m_hSock;
00069 #       endif
00070 #else
00071 
00072                 /** The handle for the connected TCP socket, or -1
00073                   */
00074                 int                             m_hSock;
00075 #endif
00076 
00077                 /** The IP address of the remote part of the connection.
00078                   */
00079                 std::string             m_remotePartIP;
00080 
00081                 /** The TCP port of the remote part of the connection.
00082                   */
00083                 unsigned short  m_remotePartPort;
00084 
00085 
00086                 /** Introduces a virtual method responsible for reading from the stream (This method BLOCKS)
00087                   * This method is implemented as a call to "readAsync" with infinite timeouts.
00088                   * \sa readAsync
00089                   */
00090                 size_t  Read(void *Buffer, size_t Count);
00091 
00092                 /** Introduces a virtual method responsible for writing to the stream.
00093                   *  Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
00094                   *  This method is implemented as a call to "writeAsync" with infinite timeouts.
00095                   * \sa writeAsync
00096                  */
00097                 size_t  Write(const void *Buffer, size_t Count);
00098 
00099                 /** Returns a description of the last error */
00100                 std::string  getLastErrorStr();
00101 
00102         public:
00103                 /** Default constructor
00104                   * \sa connect
00105                   */
00106                 CClientTCPSocket( );
00107 
00108                 /** Destructor
00109                  */
00110                 ~CClientTCPSocket( );
00111 
00112                 /** Establishes a connection with a remote part.
00113                   * \param remotePartAddress This string can be a host name, like "server" or "www.mydomain.org", or an IP address "11.22.33.44".
00114                   * \param remotePartTCPPort The port on the remote machine to connect to.
00115                   * \param timeout_ms  The timeout to wait for the connection (0: NO TIMEOUT)
00116                   * \exception This method raises an exception if an error is found with a textual description of the error.
00117                   */
00118                 void connect(
00119                         const std::string       &remotePartAddress,
00120                         unsigned short          remotePartTCPPort,
00121                         unsigned int            timeout_ms = 0 );
00122 
00123                 /** Returns true if this objects represents a successfully connected socket.
00124                   */
00125                 bool  isConnected();
00126 
00127                 /** Closes the connection.
00128                   */
00129                 void  close();
00130 
00131                 /** Writes a string to the socket.
00132                   * \exception std::exception On communication errors
00133                   */
00134                 void  sendString( const std::string &str );
00135 
00136                 /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception
00137                  */
00138                 uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning)
00139                 {
00140                     MRPT_START
00141                         MRPT_UNUSED_PARAM(Offset); MRPT_UNUSED_PARAM(Origin);
00142                         THROW_EXCEPTION("This method has no effect in this class!");
00143                     MRPT_END
00144                 }
00145 
00146                 /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception
00147                  */
00148                 uint64_t getTotalBytesCount()
00149                 {
00150                     MRPT_START
00151                         THROW_EXCEPTION("This method has no effect in this class!");
00152                     MRPT_END
00153                 }
00154 
00155                 /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception
00156                  */
00157                 uint64_t getPosition()
00158                 {
00159                     MRPT_START
00160                         THROW_EXCEPTION("This method has no effect in this class!");
00161                     MRPT_END
00162                 }
00163 
00164                 /** A method for reading from the socket with an optional timeout.
00165                   * \param Buffer The destination of data.
00166                   * \param Cound The number of bytes to read.
00167                   * \param timeoutStart_ms The maximum timeout (in milliseconds) to wait for the starting of data from the other side.
00168                   * \param timeoutBetween_ms The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one.
00169                   *  Set timeout's to -1 to block until the desired number of bytes are read, or an error happens.
00170                   *  \return The number of actually read bytes.
00171                   */
00172                 size_t  readAsync(
00173                         void    *Buffer,
00174                         const size_t    Count,
00175                         const int       timeoutStart_ms = -1,
00176                         const int       timeoutBetween_ms = -1);
00177 
00178                 /** A method for writing to the socket with optional timeouts.
00179                   *  The method supports writing block by block as the socket allows us to write more data.
00180                   * \param Buffer The data.
00181                   * \param Cound The number of bytes to write.
00182                   * \param timeout_ms The maximum timeout (in milliseconds) to wait for the socket to be available for writing (for each block).
00183                   *  Set timeout's to -1 to block until the desired number of bytes are written, or an error happens.
00184                   *  \return The number of actually written bytes.
00185                   */
00186                 size_t  writeAsync(
00187                         const void      *Buffer,
00188                         const size_t Count,
00189                         const int       timeout_ms = -1 );
00190 
00191                 /** Send a message through the TCP stream.
00192                   * \param outMsg The message to be shown.
00193                   * \param timeout_ms The maximum timeout (in milliseconds) to wait for the socket in each write operation.
00194                   * \return Returns false on any error, or true if everything goes fine.
00195                   */
00196                 bool  sendMessage(
00197                         const CMessage& outMsg,
00198                         const int timeout_ms = -1
00199                         );
00200 
00201                 /** Waits for an incoming message through the TCP stream.
00202                   * \param inMsg The received message is placed here.
00203                   * \param timeoutStart_ms The maximum timeout (in milliseconds) to wait for the starting of data from the other side.
00204                   * \param timeoutBetween_ms The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one.
00205                   * \return Returns false on any error (or timeout), or true if everything goes fine.
00206                   */
00207                 bool  receiveMessage(
00208                         CMessage&                       inMsg,
00209                         const unsigned int      timeoutStart_ms = 100,
00210                         const unsigned int      timeoutBetween_ms = 1000
00211                         );
00212 
00213                 /** Return the number of bytes already in the receive queue (they can be read without waiting) */
00214                 size_t  getReadPendingBytes();
00215 
00216         }; // End of class def.
00217 
00218         } // End of namespace
00219 } // end of namespace
00220 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011