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
Definition at line 48 of file CClientTCPSocket.h.
#include <mrpt/utils/CClientTCPSocket.h>
Public Member Functions | |
CClientTCPSocket () | |
Default constructor. | |
~CClientTCPSocket () | |
Destructor. | |
void | connect (const std::string &remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms=0) |
Establishes a connection with a remote part. | |
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. | |
uint64_t | Seek (long Offset, CStream::TSeekOrigin Origin=sFromBeginning) |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception. | |
uint64_t | getTotalBytesCount () |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception. | |
uint64_t | getPosition () |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception. | |
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. | |
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. | |
bool | sendMessage (const CMessage &outMsg, const int timeout_ms=-1) |
Send a message through the TCP stream. | |
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. | |
size_t | getReadPendingBytes () |
Return the number of bytes already in the receive queue (they can be read without waiting) | |
Static Public Attributes | |
static unsigned int | DNS_LOOKUP_TIMEOUT_MS |
See description of CClientTCPSocket. | |
Protected Member Functions | |
size_t | Read (void *Buffer, size_t Count) |
Introduces a virtual method responsible for reading from the stream (This method BLOCKS) This method is implemented as a call to "readAsync" with infinite timeouts. | |
size_t | Write (const void *Buffer, size_t Count) |
Introduces a virtual method responsible for writing to the stream. | |
std::string | getLastErrorStr () |
Returns a description of the last error. | |
Protected Attributes | |
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. | |
unsigned short | m_remotePartPort |
The TCP port of the remote part of the connection. | |
Friends | |
class | CServerTCPSocket |
mrpt::utils::CClientTCPSocket::CClientTCPSocket | ( | ) |
Default constructor.
mrpt::utils::CClientTCPSocket::~CClientTCPSocket | ( | ) |
Destructor.
void mrpt::utils::CClientTCPSocket::close | ( | ) |
Closes the connection.
void mrpt::utils::CClientTCPSocket::connect | ( | const std::string & | remotePartAddress, |
unsigned short | remotePartTCPPort, | ||
unsigned int | timeout_ms = 0 |
||
) |
Establishes a connection with a remote part.
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. |
std::string mrpt::utils::CClientTCPSocket::getLastErrorStr | ( | ) | [protected] |
Returns a description of the last error.
uint64_t mrpt::utils::CClientTCPSocket::getPosition | ( | ) | [inline, virtual] |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
Implements mrpt::utils::CStream.
Definition at line 153 of file CClientTCPSocket.h.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
size_t mrpt::utils::CClientTCPSocket::getReadPendingBytes | ( | ) |
Return the number of bytes already in the receive queue (they can be read without waiting)
uint64_t mrpt::utils::CClientTCPSocket::getTotalBytesCount | ( | ) | [inline, virtual] |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
Implements mrpt::utils::CStream.
Definition at line 144 of file CClientTCPSocket.h.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
bool mrpt::utils::CClientTCPSocket::isConnected | ( | ) |
Returns true if this objects represents a successfully connected socket.
size_t mrpt::utils::CClientTCPSocket::Read | ( | void * | Buffer, |
size_t | Count | ||
) | [protected, virtual] |
Introduces a virtual method responsible for reading from the stream (This method BLOCKS) This method is implemented as a call to "readAsync" with infinite timeouts.
Implements mrpt::utils::CStream.
size_t mrpt::utils::CClientTCPSocket::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.
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. |
bool mrpt::utils::CClientTCPSocket::receiveMessage | ( | CMessage & | inMsg, |
const unsigned int | timeoutStart_ms = 100 , |
||
const unsigned int | timeoutBetween_ms = 1000 |
||
) |
Waits for an incoming message through the TCP stream.
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. |
uint64_t mrpt::utils::CClientTCPSocket::Seek | ( | long | Offset, |
CStream::TSeekOrigin | Origin = sFromBeginning |
||
) | [inline, virtual] |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
Implements mrpt::utils::CStream.
Definition at line 134 of file CClientTCPSocket.h.
References MRPT_END, MRPT_START, MRPT_UNUSED_PARAM, and THROW_EXCEPTION.
bool mrpt::utils::CClientTCPSocket::sendMessage | ( | const CMessage & | outMsg, |
const int | timeout_ms = -1 |
||
) |
Send a message through the TCP stream.
outMsg | The message to be shown. |
timeout_ms | The maximum timeout (in milliseconds) to wait for the socket in each write operation. |
void mrpt::utils::CClientTCPSocket::sendString | ( | const std::string & | str ) |
Writes a string to the socket.
std::exception | On communication errors |
size_t mrpt::utils::CClientTCPSocket::Write | ( | const void * | Buffer, |
size_t | Count | ||
) | [protected, virtual] |
Introduces a virtual method responsible for writing to the stream.
Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. This method is implemented as a call to "writeAsync" with infinite timeouts.
Implements mrpt::utils::CStream.
size_t mrpt::utils::CClientTCPSocket::writeAsync | ( | const void * | Buffer, |
const 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.
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. |
friend class CServerTCPSocket [friend] |
Definition at line 50 of file CClientTCPSocket.h.
unsigned int mrpt::utils::CClientTCPSocket::DNS_LOOKUP_TIMEOUT_MS [static] |
See description of CClientTCPSocket.
Definition at line 54 of file CClientTCPSocket.h.
int mrpt::utils::CClientTCPSocket::m_hSock [protected] |
The handle for the connected TCP socket, or -1.
Definition at line 70 of file CClientTCPSocket.h.
std::string mrpt::utils::CClientTCPSocket::m_remotePartIP [protected] |
The IP address of the remote part of the connection.
Definition at line 75 of file CClientTCPSocket.h.
unsigned short mrpt::utils::CClientTCPSocket::m_remotePartPort [protected] |
The TCP port of the remote part of the connection.
Definition at line 79 of file CClientTCPSocket.h.
Page generated by Doxygen 1.7.2 for MRPT 0.9.4 SVN: at Mon Jan 10 22:30:30 UTC 2011 |