Main MRPT website > C++ reference for MRPT 1.5.6
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 #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 <mrpt/utils/net_utils.h>
16 #include <string>
17 
18 namespace mrpt
19 {
20 namespace 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 */
78  ~CClientTCPSocket( );
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 */
92  bool isConnected();
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(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE
104  {
105  MRPT_START
106  MRPT_UNUSED_PARAM(Offset); MRPT_UNUSED_PARAM(Origin);
107  THROW_EXCEPTION("This method has no effect in this class!");
108  MRPT_END
109  }
110 
111  /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception */
113  {
114  MRPT_START
115  THROW_EXCEPTION("This method has no effect in this class!");
116  MRPT_END
117  }
118 
119  /** This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception */
121  {
122  MRPT_START
123  THROW_EXCEPTION("This method has no effect in this class!");
124  MRPT_END
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  */
149  size_t writeAsync(
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  */
159  bool sendMessage(
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  */
170  bool receiveMessage(
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) */
177  size_t getReadPendingBytes();
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. */
186  int getTCPNoDelay();
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. */
195  int getSOSendBufffer();
196 
197  }; // End of class def.
198 
199  } // End of namespace
200 } // end of namespace
201 #endif
uint64_t Seek(int64_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...
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
std::string getLastErrorStr()
Returns a description of the last Sockets error.
#define THROW_EXCEPTION(msg)
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.
__int64 int64_t
Definition: rptypes.h:51
std::string BASE_IMPEXP getLastSocketErrorStr()
Returns a description of the last Sockets error.
Definition: net_utils.cpp:519
static unsigned int DNS_LOOKUP_TIMEOUT_MS
See description of CClientTCPSocket.
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:52
GLsizei const GLcharARB ** string
Definition: glew.h:3293
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 getPosition() MRPT_OVERRIDE
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's CStream interface for passing...
unsigned __int32 uint32_t
Definition: rptypes.h:49
uint64_t getTotalBytesCount() MRPT_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:29



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018