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