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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019