MRPT
1.9.9
|
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 34 of file CClientTCPSocket.h.
#include <mrpt/comms/CClientTCPSocket.h>
Public Types | |
enum | TSeekOrigin { sFromBeginning = 0, sFromCurrent = 1, sFromEnd = 2 } |
Used in CStream::Seek. More... | |
Public Member Functions | |
CClientTCPSocket () | |
Default constructor. More... | |
~CClientTCPSocket () | |
Destructor. More... | |
void | connect (const std::string &remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms=0) |
Establishes a connection with a remote part. More... | |
bool | isConnected () |
Returns true if this objects represents a successfully connected socket. More... | |
void | close () |
Closes the connection. More... | |
void | sendString (const std::string &str) |
Writes a string to the socket. More... | |
uint64_t | Seek (int64_t off, CStream::TSeekOrigin org=sFromBeginning) override |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception. More... | |
uint64_t | getTotalBytesCount () const override |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception. More... | |
uint64_t | getPosition () const override |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception. More... | |
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. More... | |
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. More... | |
template<class MESSAGE > | |
bool | sendMessage (const MESSAGE &outMsg, const int timeout_ms=-1) |
Send a message through the TCP stream. More... | |
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. More... | |
size_t | getReadPendingBytes () |
Return the number of bytes already in the receive queue (they can be read without waiting) More... | |
int | setTCPNoDelay (const int &newValue) |
Set the TCP no delay option of the protocol (Nagle algorithm). More... | |
int | getTCPNoDelay () |
Return the value of the TCPNoDelay option. More... | |
int | setSOSendBufffer (const int &newValue) |
Set the size of the SO send buffer. More... | |
int | getSOSendBufffer () |
Return the current size of the SO send buffer. More... | |
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). More... | |
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. More... | |
virtual int | printf (const char *fmt,...) MRPT_printf_format_check(2 |
Writes a string to the stream in a textual form. More... | |
template<typename CONTAINER_TYPE > | |
virtual 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 . More... | |
bool | getline (std::string &out_str) |
Reads from the stream until a ' ' character is found ('' characters are ignored). More... | |
Static Public Attributes | |
static unsigned int | DNS_LOOKUP_TIMEOUT_MS = 3000 |
See description of CClientTCPSocket. More... | |
Protected Member Functions | |
size_t | Read (void *Buffer, size_t Count) override |
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. More... | |
size_t | Write (const void *Buffer, size_t Count) override |
Introduces a virtual method responsible for writing to the stream. More... | |
std::string | getLastErrorStr () |
Returns a description of the last Sockets error. More... | |
Protected Attributes | |
uint32_t | m_hSock |
The handle for the connected TCP socket, or INVALID_SOCKET. More... | |
std::string | m_remotePartIP |
The IP address of the remote part of the connection. More... | |
unsigned short | m_remotePartPort |
The TCP port of the remote part of the connection. More... | |
Friends | |
class | CServerTCPSocket |
|
inherited |
Used in CStream::Seek.
Enumerator | |
---|---|
sFromBeginning | |
sFromCurrent | |
sFromEnd |
Definition at line 32 of file io/CStream.h.
CClientTCPSocket::CClientTCPSocket | ( | ) |
Default constructor.
Definition at line 47 of file CClientTCPSocket.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
CClientTCPSocket::~CClientTCPSocket | ( | ) |
Destructor.
Definition at line 70 of file CClientTCPSocket.cpp.
void CClientTCPSocket::close | ( | ) |
Closes the connection.
Definition at line 81 of file CClientTCPSocket.cpp.
References MRPT_END, and MRPT_START.
Referenced by mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread().
void 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. |
Definition at line 137 of file CClientTCPSocket.cpp.
References mrpt::comms::net::DNS_resolve_async(), mrpt::format(), mrpt::obs::gnss::lon, MRPT_END, MRPT_START, THROW_EXCEPTION, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CHokuyoURG::ensureStreamIsOpen(), mrpt::comms::net::http_request(), mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread(), and thread_client().
|
protected |
Returns a description of the last Sockets error.
Definition at line 525 of file CClientTCPSocket.cpp.
References mrpt::comms::net::getLastSocketErrorStr().
|
inherited |
Reads from the stream until a '
' character is found ('' characters are ignored).
Definition at line 69 of file CStream.cpp.
|
overridevirtual |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
Implements mrpt::io::CStream.
Definition at line 546 of file CClientTCPSocket.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
size_t CClientTCPSocket::getReadPendingBytes | ( | ) |
Return the number of bytes already in the receive queue (they can be read without waiting)
Definition at line 450 of file CClientTCPSocket.cpp.
References THROW_EXCEPTION.
Referenced by mrpt::hwdrivers::CHokuyoURG::purgeBuffers().
int CClientTCPSocket::getSOSendBufffer | ( | ) |
Return the current size of the SO send buffer.
Definition at line 512 of file CClientTCPSocket.cpp.
int CClientTCPSocket::getTCPNoDelay | ( | ) |
Return the value of the TCPNoDelay option.
Definition at line 482 of file CClientTCPSocket.cpp.
|
overridevirtual |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
Implements mrpt::io::CStream.
Definition at line 539 of file CClientTCPSocket.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION.
bool CClientTCPSocket::isConnected | ( | ) |
Returns true if this objects represents a successfully connected socket.
Definition at line 275 of file CClientTCPSocket.cpp.
Referenced by mrpt::hwdrivers::CHokuyoURG::ensureStreamIsOpen(), mrpt::comms::net::http_request(), and mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread().
|
virtualinherited |
Writes a string to the stream in a textual form.
Definition at line 30 of file CStream.cpp.
References MRPT_END, MRPT_START, and vsnprintf.
Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel(), and mrpt::io::CStream::printf_vector().
|
inlineinherited |
Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T
.
CONTAINER_TYPE | can be any vector<T>, deque<T> or alike. |
Definition at line 102 of file io/CStream.h.
References mrpt::io::CStream::printf().
Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel().
|
overrideprotectedvirtual |
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::io::CStream.
Definition at line 108 of file CClientTCPSocket.cpp.
References MRPT_END, and MRPT_START.
size_t 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. |
Definition at line 279 of file CClientTCPSocket.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CHokuyoURG::assureBufferHasBytes(), mrpt::hwdrivers::CGPSInterface::doProcess(), mrpt::comms::net::http_request(), mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread(), and receiveMessage().
|
inlinevirtualinherited |
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.
std::exception | On any error, or if ZERO bytes are read. |
Reimplemented in mrpt::comms::CInterfaceFTDI.
Definition at line 62 of file io/CStream.h.
References mrpt::io::CStream::Read().
|
inline |
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. |
MESSAGE | can be mrpt::serialization::CMessage |
Definition at line 194 of file CClientTCPSocket.h.
References readAsync().
Referenced by thread_client().
|
pure virtualinherited |
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:
Implemented in mrpt::io::CPipeBaseEndPoint, mrpt::io::CFileStream, mrpt::io::CMemoryStream, mrpt::io::CFileGZInputStream, mrpt::io::CFileGZOutputStream, mrpt::io::CFileOutputStream, and mrpt::io::CFileInputStream.
|
override |
This virtual method has no effect in this implementation over a TCP socket, and its use raises an exception.
Definition at line 530 of file CClientTCPSocket.cpp.
References MRPT_END, MRPT_START, MRPT_UNUSED_PARAM, and THROW_EXCEPTION.
|
inline |
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. |
MESSAGE | can be mrpt::serialization::CMessage |
Definition at line 160 of file CClientTCPSocket.h.
References writeAsync().
Referenced by thread_server().
void CClientTCPSocket::sendString | ( | const std::string & | str | ) |
Writes a string to the socket.
std::exception | On communication errors |
Definition at line 129 of file CClientTCPSocket.cpp.
Referenced by mrpt::comms::net::http_request(), and mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread().
int CClientTCPSocket::setSOSendBufffer | ( | const int & | newValue | ) |
Set the size of the SO send buffer.
This buffer is used to store data, and is sended when is full.
newValue | New size of the SO send buffer. |
Definition at line 502 of file CClientTCPSocket.cpp.
int CClientTCPSocket::setTCPNoDelay | ( | const int & | newValue | ) |
Set the TCP no delay option of the protocol (Nagle algorithm).
newValue | New value (0 enable Nagle algorithm, 1 disable). |
Definition at line 471 of file CClientTCPSocket.cpp.
|
overrideprotectedvirtual |
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::io::CStream.
Definition at line 120 of file CClientTCPSocket.cpp.
References MRPT_END, and MRPT_START.
size_t 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. |
Definition at line 370 of file CClientTCPSocket.cpp.
References MRPT_END, MRPT_START, and THROW_EXCEPTION_FMT.
Referenced by mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread(), and sendMessage().
|
friend |
Definition at line 36 of file CClientTCPSocket.h.
|
static |
See description of CClientTCPSocket.
Definition at line 40 of file CClientTCPSocket.h.
|
protected |
The handle for the connected TCP socket, or INVALID_SOCKET.
Definition at line 49 of file CClientTCPSocket.h.
|
protected |
The IP address of the remote part of the connection.
Definition at line 56 of file CClientTCPSocket.h.
|
protected |
The TCP port of the remote part of the connection.
Definition at line 58 of file CClientTCPSocket.h.
Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020 |