class mrpt::comms::CClientTCPSocket
Overview
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.
Unless otherwise noticed, operations are blocking.
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
#include <mrpt/comms/CClientTCPSocket.h> class CClientTCPSocket: public mrpt::io::CStream { public: // enums enum TSeekOrigin; // fields static unsigned int DNS_LOOKUP_TIMEOUT_MS = 3000; // construction CClientTCPSocket(); // methods void connect( const std::string& remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms = 0 ); bool isConnected(); void close(); void sendString(const std::string& str); uint64_t Seek(int64_t off, CStream::TSeekOrigin org = sFromBeginning); virtual uint64_t getTotalBytesCount() const; virtual uint64_t getPosition() const; size_t readAsync( void* Buffer, size_t Count, const int timeoutStart_ms = -1, const int timeoutBetween_ms = -1 ); size_t writeAsync(const void* Buffer, size_t Count, const int timeout_ms = -1); template <class MESSAGE> bool sendMessage( const MESSAGE& outMsg, const int timeout_ms = -1 ); template <class MESSAGE> bool receiveMessage( MESSAGE& inMsg, const unsigned int timeoutStart_ms = 100, const unsigned int timeoutBetween_ms = 1000 ); size_t getReadPendingBytes(); int setTCPNoDelay(int newValue); int getTCPNoDelay(); int setSOSendBufffer(int newValue); int getSOSendBufffer(); virtual size_t ReadBufferImmediate(void* Buffer, size_t Count); virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) = 0; virtual int printf(const char* fmt, ...); template <typename CONTAINER_TYPE> int void printf_vector( const char* fmt, const CONTAINER_TYPE& V, char separator = ',' ); bool getline(std::string& out_str); virtual std::string getStreamDescription() const; };
Inherited Members
public: // methods virtual size_t Read(void* Buffer, size_t Count) = 0; virtual size_t Write(const void* Buffer, size_t Count) = 0; virtual uint64_t getTotalBytesCount() const = 0; virtual uint64_t getPosition() const = 0;
Fields
static unsigned int DNS_LOOKUP_TIMEOUT_MS = 3000
See description of CClientTCPSocket.
Construction
CClientTCPSocket()
Default constructor.
See also:
Methods
void connect( const std::string& remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms = 0 )
Establishes a connection with a remote part.
Parameters:
remotePartAddress |
This string can be a host name, like “server” or “www.mydomain.org”, or an IP address “11.22.33.44”. |
remotePartTCPPort |
The port on the remote machine to connect to. |
timeout_ms |
The timeout to wait for the connection (0: NO TIMEOUT) |
This |
method raises an exception if an error is found with a textual description of the error. |
bool isConnected()
Returns true if this objects represents a successfully connected socket.
void close()
Closes the connection.
void sendString(const std::string& str)
Writes a string to the socket.
Parameters:
std::exception |
On communication errors |
uint64_t Seek(int64_t off, CStream::TSeekOrigin org = sFromBeginning)
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
virtual uint64_t getTotalBytesCount() const
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
virtual uint64_t getPosition() const
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
size_t readAsync( void* Buffer, size_t Count, const int timeoutStart_ms = -1, const int timeoutBetween_ms = -1 )
A method for reading from the socket with an optional timeout.
Parameters:
Buffer |
The destination of data. |
Cound |
The number of bytes to read. |
timeoutStart_ms |
The maximum timeout (in milliseconds) to wait for the starting of data from the other side. |
timeoutBetween_ms |
The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one. Set timeout’s to -1 to block until the desired number of bytes are read, or an error happens. |
Returns:
The number of actually read bytes.
size_t writeAsync(const void* Buffer, size_t Count, const int timeout_ms = -1)
A method for writing to the socket with optional timeouts.
The method supports writing block by block as the socket allows us to write more data.
Parameters:
Buffer |
The data. |
Cound |
The number of bytes to write. |
timeout_ms |
The maximum timeout (in milliseconds) to wait for the socket to be available for writing (for each block). Set timeout’s to -1 to block until the desired number of bytes are written, or an error happens. |
Returns:
The number of actually written bytes.
template <class MESSAGE> bool sendMessage( const MESSAGE& outMsg, const int timeout_ms = -1 )
Send a message through the TCP stream.
Parameters:
outMsg |
The message to be shown. |
timeout_ms |
The maximum timeout (in milliseconds) to wait for the socket in each write operation. |
MESSAGE |
Returns:
Returns false on any error, or true if everything goes fine.
template <class MESSAGE> bool receiveMessage( MESSAGE& inMsg, const unsigned int timeoutStart_ms = 100, const unsigned int timeoutBetween_ms = 1000 )
Waits for an incoming message through the TCP stream.
Parameters:
inMsg |
The received message is placed here. |
timeoutStart_ms |
The maximum timeout (in milliseconds) to wait for the starting of data from the other side. |
timeoutBetween_ms |
The maximum timeout (in milliseconds) to wait for a chunk of data after a previous one. |
MESSAGE |
Returns:
Returns false on any error (or timeout), or true if everything goes fine.
size_t getReadPendingBytes()
Return the number of bytes already in the receive queue (they can be read without waiting)
int setTCPNoDelay(int newValue)
Set the TCP no delay option of the protocol (Nagle algorithm).
Parameters:
newValue |
New value (0 enable Nagle algorithm, 1 disable). |
Returns:
Return a number lower than 0 if any error occurred.
int getTCPNoDelay()
Return the value of the TCPNoDelay option.
int setSOSendBufffer(int newValue)
Set the size of the SO send buffer.
This buffer is used to store data, and is sended when is full.
Parameters:
newValue |
New size of the SO send buffer. |
Returns:
Return a number lower than 0 if any error occurred.
int getSOSendBufffer()
Return the current size of the SO send buffer.
virtual size_t ReadBufferImmediate(void* Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream).
Note that this method will fallback to ReadBuffer() in most CStream classes but in some hardware-related classes.
Parameters:
std::exception |
On any error, or if ZERO bytes are read. |
virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) = 0
Introduces a pure virtual method for moving to a specified position in the streamed resource.
he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:
sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.
sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.
sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.
Returns:
Seek returns the new value of the Position property.
virtual int printf(const char* fmt, ...)
Writes a string to the stream in a textual form.
See also:
CStdOutStream
template <typename CONTAINER_TYPE> int void printf_vector( const char* fmt, const CONTAINER_TYPE& V, char separator = ',' )
Prints a vector in the format [A,B,C,…] using CStream::printf, and the fmt string for each vector element T
.
Parameters:
CONTAINER_TYPE |
can be any vector<T>, deque<T> or alike. |
bool getline(std::string& out_str)
Reads from the stream until a \n
character is found (\r
characters are ignored).
Returns:
false on EOF or any other read error.
virtual std::string getStreamDescription() const
Returns a human-friendly description of the stream, e.g.
a filename.