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