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



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Sun Nov 27 02:56:59 UTC 2022