Main MRPT website > C++ reference for MRPT 1.3.2
CClientTCPSocket.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CClientTCPSocket_H
10 #define CClientTCPSocket_H
11 
12 #include <mrpt/utils/core_defs.h>
13 #include <mrpt/utils/mrpt_stdint.h>
14 #include <mrpt/utils/CStream.h>
15 #include <string>
16 
17 namespace mrpt
18 {
19 namespace utils
20 {
21  class CServerTCPSocket;
22  class CMessage;
23 
24  /** \defgroup network_grp Networking, sockets, DNS
25  * \ingroup mrpt_base_grp */
26 
27  /** 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.
28  * Unless otherwise noticed, operations are blocking.
29  *
30  * 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
31  * \ingroup network_grp
32  */
34  {
35  friend class CServerTCPSocket;
36 
37  public:
38  /** See description of CClientTCPSocket */
39  static unsigned int DNS_LOOKUP_TIMEOUT_MS;
40 
41  protected:
42 
43 #ifdef MRPT_OS_WINDOWS
44  /** The handle for the connected TCP socket, or INVALID_SOCKET
45  */
46 # if MRPT_WORD_SIZE==64
47  uint64_t m_hSock;
48 # else
49  uint32_t m_hSock;
50 # endif
51 #else
52 
53  /** The handle for the connected TCP socket, or -1
54  */
55  int m_hSock;
56 #endif
57 
58  /** The IP address of the remote part of the connection.
59  */
60  std::string m_remotePartIP;
61 
62  /** The TCP port of the remote part of the connection.
63  */
64  unsigned short m_remotePartPort;
65 
66 
67  /** Introduces a virtual method responsible for reading from the stream (This method BLOCKS)
68  * This method is implemented as a call to "readAsync" with infinite timeouts.
69  * \sa readAsync
70  */
71  size_t Read(void *Buffer, size_t Count);
72 
73  /** Introduces a virtual method responsible for writing to the stream.
74  * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
75  * This method is implemented as a call to "writeAsync" with infinite timeouts.
76  * \sa writeAsync
77  */
78  size_t Write(const void *Buffer, size_t Count);
79 
80  /** Returns a description of the last error */
81  std::string getLastErrorStr();
82 
83  public:
84  /** Default constructor
85  * \sa connect
86  */
88 
89  /** Destructor
90  */
91  ~CClientTCPSocket( );
92 
93  /** Establishes a connection with a remote part.
94  * \param remotePartAddress This string can be a host name, like "server" or "www.mydomain.org", or an IP address "11.22.33.44".
95  * \param remotePartTCPPort The port on the remote machine to connect to.
96  * \param timeout_ms The timeout to wait for the connection (0: NO TIMEOUT)
97  * \exception This method raises an exception if an error is found with a textual description of the error.
98  */
99  void connect(
100  const std::string &remotePartAddress,
101  unsigned short remotePartTCPPort,
102  unsigned int timeout_ms = 0 );
103 
104  /** Returns true if this objects represents a successfully connected socket.
105  */
106  bool isConnected();
107 
108  /** Closes the connection.
109  */
110  void close();
111 
112  /** Writes a string to the socket.
113  * \exception std::exception On communication errors
114  */
115  void sendString( const std::string &str );
116 
117  /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception
118  */
119  uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning)
120  {
121  MRPT_START
122  MRPT_UNUSED_PARAM(Offset); MRPT_UNUSED_PARAM(Origin);
123  THROW_EXCEPTION("This method has no effect in this class!");
124  MRPT_END
125  }
126 
127  /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception
128  */
130  {
131  MRPT_START
132  THROW_EXCEPTION("This method has no effect in this class!");
133  MRPT_END
134  }
135 
136  /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception
137  */
138  uint64_t getPosition()
139  {
140  MRPT_START
141  THROW_EXCEPTION("This method has no effect in this class!");
142  MRPT_END
143  }
144 
145  /** A method for reading from the socket with an optional timeout.
146  * \param Buffer The destination of data.
147  * \param Cound The number of bytes to read.
148  * \param timeoutStart_ms The maximum timeout (in milliseconds) to wait for the starting of data from the other side.
149  * \param timeoutBetween_ms The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one.
150  * Set timeout's to -1 to block until the desired number of bytes are read, or an error happens.
151  * \return The number of actually read bytes.
152  */
153  size_t readAsync(
154  void *Buffer,
155  const size_t Count,
156  const int timeoutStart_ms = -1,
157  const int timeoutBetween_ms = -1);
158 
159  /** A method for writing to the socket with optional timeouts.
160  * The method supports writing block by block as the socket allows us to write more data.
161  * \param Buffer The data.
162  * \param Cound The number of bytes to write.
163  * \param timeout_ms The maximum timeout (in milliseconds) to wait for the socket to be available for writing (for each block).
164  * Set timeout's to -1 to block until the desired number of bytes are written, or an error happens.
165  * \return The number of actually written bytes.
166  */
167  size_t writeAsync(
168  const void *Buffer,
169  const size_t Count,
170  const int timeout_ms = -1 );
171 
172  /** Send a message through the TCP stream.
173  * \param outMsg The message to be shown.
174  * \param timeout_ms The maximum timeout (in milliseconds) to wait for the socket in each write operation.
175  * \return Returns false on any error, or true if everything goes fine.
176  */
177  bool sendMessage(
178  const CMessage& outMsg,
179  const int timeout_ms = -1
180  );
181 
182  /** Waits for an incoming message through the TCP stream.
183  * \param inMsg The received message is placed here.
184  * \param timeoutStart_ms The maximum timeout (in milliseconds) to wait for the starting of data from the other side.
185  * \param timeoutBetween_ms The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one.
186  * \return Returns false on any error (or timeout), or true if everything goes fine.
187  */
188  bool receiveMessage(
189  CMessage& inMsg,
190  const unsigned int timeoutStart_ms = 100,
191  const unsigned int timeoutBetween_ms = 1000
192  );
193 
194  /** Return the number of bytes already in the receive queue (they can be read without waiting) */
195  size_t getReadPendingBytes();
196 
197  /** Set the TCP no delay option of the protocol (Nagle algorithm).
198  * \param newValue New value (0 enable Nagle algorithm, 1 disable).
199  * \return Return a number lower than 0 if any error occurred.
200  */
201  int setTCPNoDelay( const int &newValue );
202 
203  /** Return the value of the TCPNoDelay option. */
204  int getTCPNoDelay();
205 
206  /** Set the size of the SO send buffer. This buffer is used to store data, and is sended when is full.
207  * \param newValue New size of the SO send buffer.
208  * \return Return a number lower than 0 if any error occurred.
209  */
210  int setSOSendBufffer( const int &newValue );
211 
212  /** Return the current size of the SO send buffer. */
213  int getSOSendBufffer();
214 
215  }; // End of class def.
216 
217  } // End of namespace
218 } // end of namespace
219 #endif
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
static unsigned int DNS_LOOKUP_TIMEOUT_MS
See description of CClientTCPSocket.
#define THROW_EXCEPTION(msg)
uint64_t getTotalBytesCount()
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exc...
unsigned short m_remotePartPort
The TCP port of the remote part of the connection.
A TCP socket that can be wait for client connections to enter.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
uint64_t getPosition()
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exc...
class BASE_IMPEXP CMessage
Definition: CStream.h:25
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
int m_hSock
The handle for the connected TCP socket, or -1.
std::string m_remotePartIP
The IP address of the remote part of the connection.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exc...
A TCP socket that can be connected to a TCP server, implementing MRPT&#39;s CStream interface for passing...
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:32



Page generated by Doxygen 1.8.12 for MRPT 1.3.2 SVN: at Thu Nov 10 13:22:34 UTC 2016