00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 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 /** 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. 00044 * Unless otherwise noticed, operations are blocking. 00045 * 00046 * 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 00047 */ 00048 class BASE_IMPEXP CClientTCPSocket : public CStream 00049 { 00050 friend class CServerTCPSocket; 00051 00052 public: 00053 /** See description of CClientTCPSocket */ 00054 static unsigned int DNS_LOOKUP_TIMEOUT_MS; 00055 00056 protected: 00057 00058 #ifdef MRPT_OS_WINDOWS 00059 /** The handle for the connected TCP socket, or INVALID_SOCKET 00060 */ 00061 # if MRPT_WORD_SIZE==64 00062 uint64_t m_hSock; 00063 # else 00064 uint32_t m_hSock; 00065 # endif 00066 #else 00067 00068 /** The handle for the connected TCP socket, or -1 00069 */ 00070 int m_hSock; 00071 #endif 00072 00073 /** The IP address of the remote part of the connection. 00074 */ 00075 std::string m_remotePartIP; 00076 00077 /** The TCP port of the remote part of the connection. 00078 */ 00079 unsigned short m_remotePartPort; 00080 00081 00082 /** Introduces a virtual method responsible for reading from the stream (This method BLOCKS) 00083 * This method is implemented as a call to "readAsync" with infinite timeouts. 00084 * \sa readAsync 00085 */ 00086 size_t Read(void *Buffer, size_t Count); 00087 00088 /** Introduces a virtual method responsible for writing to the stream. 00089 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. 00090 * This method is implemented as a call to "writeAsync" with infinite timeouts. 00091 * \sa writeAsync 00092 */ 00093 size_t Write(const void *Buffer, size_t Count); 00094 00095 /** Returns a description of the last error */ 00096 std::string getLastErrorStr(); 00097 00098 public: 00099 /** Default constructor 00100 * \sa connect 00101 */ 00102 CClientTCPSocket( ); 00103 00104 /** Destructor 00105 */ 00106 ~CClientTCPSocket( ); 00107 00108 /** Establishes a connection with a remote part. 00109 * \param remotePartAddress This string can be a host name, like "server" or "www.mydomain.org", or an IP address "11.22.33.44". 00110 * \param remotePartTCPPort The port on the remote machine to connect to. 00111 * \param timeout_ms The timeout to wait for the connection (0: NO TIMEOUT) 00112 * \exception This method raises an exception if an error is found with a textual description of the error. 00113 */ 00114 void connect( 00115 const std::string &remotePartAddress, 00116 unsigned short remotePartTCPPort, 00117 unsigned int timeout_ms = 0 ); 00118 00119 /** Returns true if this objects represents a successfully connected socket. 00120 */ 00121 bool isConnected(); 00122 00123 /** Closes the connection. 00124 */ 00125 void close(); 00126 00127 /** Writes a string to the socket. 00128 * \exception std::exception On communication errors 00129 */ 00130 void sendString( const std::string &str ); 00131 00132 /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception 00133 */ 00134 uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning) 00135 { 00136 MRPT_START 00137 MRPT_UNUSED_PARAM(Offset); MRPT_UNUSED_PARAM(Origin); 00138 THROW_EXCEPTION("This method has no effect in this class!"); 00139 MRPT_END 00140 } 00141 00142 /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception 00143 */ 00144 uint64_t getTotalBytesCount() 00145 { 00146 MRPT_START 00147 THROW_EXCEPTION("This method has no effect in this class!"); 00148 MRPT_END 00149 } 00150 00151 /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception 00152 */ 00153 uint64_t getPosition() 00154 { 00155 MRPT_START 00156 THROW_EXCEPTION("This method has no effect in this class!"); 00157 MRPT_END 00158 } 00159 00160 /** A method for reading from the socket with an optional timeout. 00161 * \param Buffer The destination of data. 00162 * \param Cound The number of bytes to read. 00163 * \param timeoutStart_ms The maximum timeout (in milliseconds) to wait for the starting of data from the other side. 00164 * \param timeoutBetween_ms The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one. 00165 * Set timeout's to -1 to block until the desired number of bytes are read, or an error happens. 00166 * \return The number of actually read bytes. 00167 */ 00168 size_t readAsync( 00169 void *Buffer, 00170 const size_t Count, 00171 const int timeoutStart_ms = -1, 00172 const int timeoutBetween_ms = -1); 00173 00174 /** A method for writing to the socket with optional timeouts. 00175 * The method supports writing block by block as the socket allows us to write more data. 00176 * \param Buffer The data. 00177 * \param Cound The number of bytes to write. 00178 * \param timeout_ms The maximum timeout (in milliseconds) to wait for the socket to be available for writing (for each block). 00179 * Set timeout's to -1 to block until the desired number of bytes are written, or an error happens. 00180 * \return The number of actually written bytes. 00181 */ 00182 size_t writeAsync( 00183 const void *Buffer, 00184 const size_t Count, 00185 const int timeout_ms = -1 ); 00186 00187 /** Send a message through the TCP stream. 00188 * \param outMsg The message to be shown. 00189 * \param timeout_ms The maximum timeout (in milliseconds) to wait for the socket in each write operation. 00190 * \return Returns false on any error, or true if everything goes fine. 00191 */ 00192 bool sendMessage( 00193 const CMessage& outMsg, 00194 const int timeout_ms = -1 00195 ); 00196 00197 /** Waits for an incoming message through the TCP stream. 00198 * \param inMsg The received message is placed here. 00199 * \param timeoutStart_ms The maximum timeout (in milliseconds) to wait for the starting of data from the other side. 00200 * \param timeoutBetween_ms The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one. 00201 * \return Returns false on any error (or timeout), or true if everything goes fine. 00202 */ 00203 bool receiveMessage( 00204 CMessage& inMsg, 00205 const unsigned int timeoutStart_ms = 100, 00206 const unsigned int timeoutBetween_ms = 1000 00207 ); 00208 00209 /** Return the number of bytes already in the receive queue (they can be read without waiting) */ 00210 size_t getReadPendingBytes(); 00211 00212 }; // End of class def. 00213 00214 } // End of namespace 00215 } // end of namespace 00216 #endif
Page generated by Doxygen 1.7.2 for MRPT 0.9.4 SVN: at Mon Jan 10 22:46:17 UTC 2011 |