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