MRPT  1.9.9
mrpt::comms::CClientTCPSocket Class Referenceabstract

Detailed Description

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>

Inheritance diagram for mrpt::comms::CClientTCPSocket:
Inheritance graph

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
 

Member Enumeration Documentation

◆ TSeekOrigin

Used in CStream::Seek.

Enumerator
sFromBeginning 
sFromCurrent 
sFromEnd 

Definition at line 32 of file io/CStream.h.

Constructor & Destructor Documentation

◆ CClientTCPSocket()

CClientTCPSocket::CClientTCPSocket ( )

Default constructor.

See also
connect

Definition at line 47 of file CClientTCPSocket.cpp.

References MRPT_END, MRPT_START, and THROW_EXCEPTION.

◆ ~CClientTCPSocket()

CClientTCPSocket::~CClientTCPSocket ( )

Destructor.

Definition at line 70 of file CClientTCPSocket.cpp.

Member Function Documentation

◆ close()

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().

◆ connect()

void CClientTCPSocket::connect ( const std::string remotePartAddress,
unsigned short  remotePartTCPPort,
unsigned int  timeout_ms = 0 
)

Establishes a connection with a remote part.

Parameters
remotePartAddressThis string can be a host name, like "server" or "www.mydomain.org", or an IP address "11.22.33.44".
remotePartTCPPortThe port on the remote machine to connect to.
timeout_msThe timeout to wait for the connection (0: NO TIMEOUT)
Exceptions
Thismethod 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().

◆ getLastErrorStr()

std::string CClientTCPSocket::getLastErrorStr ( )
protected

Returns a description of the last Sockets error.

Definition at line 525 of file CClientTCPSocket.cpp.

References mrpt::comms::net::getLastSocketErrorStr().

◆ getline()

bool CStream::getline ( std::string out_str)
inherited

Reads from the stream until a '
' character is found ('' characters are ignored).

Returns
false on EOF or any other read error.

Definition at line 69 of file CStream.cpp.

◆ getPosition()

uint64_t CClientTCPSocket::getPosition ( ) const
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.

◆ getReadPendingBytes()

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().

◆ getSOSendBufffer()

int CClientTCPSocket::getSOSendBufffer ( )

Return the current size of the SO send buffer.

Definition at line 512 of file CClientTCPSocket.cpp.

◆ getTCPNoDelay()

int CClientTCPSocket::getTCPNoDelay ( )

Return the value of the TCPNoDelay option.

Definition at line 482 of file CClientTCPSocket.cpp.

◆ getTotalBytesCount()

uint64_t CClientTCPSocket::getTotalBytesCount ( ) const
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.

◆ isConnected()

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().

◆ printf()

int CStream::printf ( const char *  fmt,
  ... 
)
virtualinherited

Writes a string to the stream in a textual form.

See also
CStdOutStream

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().

◆ printf_vector()

template<typename CONTAINER_TYPE >
virtual int void mrpt::io::CStream::printf_vector ( const char *  fmt,
const CONTAINER_TYPE &  V,
char  separator = ',' 
)
inlineinherited

Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T.

Template Parameters
CONTAINER_TYPEcan 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().

◆ Read()

size_t CClientTCPSocket::Read ( void Buffer,
size_t  Count 
)
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.

See also
readAsync

Implements mrpt::io::CStream.

Definition at line 108 of file CClientTCPSocket.cpp.

References MRPT_END, and MRPT_START.

◆ readAsync()

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.

Parameters
BufferThe destination of data.
CoundThe number of bytes to read.
timeoutStart_msThe maximum timeout (in milliseconds) to wait for the starting of data from the other side.
timeoutBetween_msThe 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.

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().

◆ ReadBufferImmediate()

virtual size_t mrpt::io::CStream::ReadBufferImmediate ( void Buffer,
size_t  Count 
)
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.

Exceptions
std::exceptionOn 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().

◆ receiveMessage()

template<class MESSAGE >
bool mrpt::comms::CClientTCPSocket::receiveMessage ( MESSAGE &  inMsg,
const unsigned int  timeoutStart_ms = 100,
const unsigned int  timeoutBetween_ms = 1000 
)
inline

Waits for an incoming message through the TCP stream.

Parameters
inMsgThe received message is placed here.
timeoutStart_msThe maximum timeout (in milliseconds) to wait for the starting of data from the other side.
timeoutBetween_msThe maximum timeout (in milliseconds) to wait for a chunk of data after a previous one.
Returns
Returns false on any error (or timeout), or true if everything goes fine.
Template Parameters
MESSAGEcan be mrpt::serialization::CMessage

Definition at line 194 of file CClientTCPSocket.h.

References readAsync().

Referenced by thread_client().

◆ Seek() [1/2]

virtual uint64_t mrpt::io::CStream::Seek ( int64_t  Offset,
CStream::TSeekOrigin  Origin = sFromBeginning 
)
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:

  • 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.

Implemented in mrpt::io::CPipeBaseEndPoint, mrpt::io::CFileStream, mrpt::io::CMemoryStream, mrpt::io::CFileGZInputStream, mrpt::io::CFileGZOutputStream, mrpt::io::CFileOutputStream, and mrpt::io::CFileInputStream.

◆ Seek() [2/2]

uint64_t CClientTCPSocket::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.

Definition at line 530 of file CClientTCPSocket.cpp.

References MRPT_END, MRPT_START, MRPT_UNUSED_PARAM, and THROW_EXCEPTION.

◆ sendMessage()

template<class MESSAGE >
bool mrpt::comms::CClientTCPSocket::sendMessage ( const MESSAGE &  outMsg,
const int  timeout_ms = -1 
)
inline

Send a message through the TCP stream.

Parameters
outMsgThe message to be shown.
timeout_msThe maximum timeout (in milliseconds) to wait for the socket in each write operation.
Returns
Returns false on any error, or true if everything goes fine.
Template Parameters
MESSAGEcan be mrpt::serialization::CMessage

Definition at line 160 of file CClientTCPSocket.h.

References writeAsync().

Referenced by thread_server().

◆ sendString()

void CClientTCPSocket::sendString ( const std::string str)

Writes a string to the socket.

Exceptions
std::exceptionOn communication errors

Definition at line 129 of file CClientTCPSocket.cpp.

Referenced by mrpt::comms::net::http_request(), and mrpt::hwdrivers::CNTRIPClient::private_ntrip_thread().

◆ setSOSendBufffer()

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.

Parameters
newValueNew size of the SO send buffer.
Returns
Return a number lower than 0 if any error occurred.

Definition at line 502 of file CClientTCPSocket.cpp.

◆ setTCPNoDelay()

int CClientTCPSocket::setTCPNoDelay ( const int &  newValue)

Set the TCP no delay option of the protocol (Nagle algorithm).

Parameters
newValueNew value (0 enable Nagle algorithm, 1 disable).
Returns
Return a number lower than 0 if any error occurred.

Definition at line 471 of file CClientTCPSocket.cpp.

◆ Write()

size_t CClientTCPSocket::Write ( const void Buffer,
size_t  Count 
)
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.

See also
writeAsync

Implements mrpt::io::CStream.

Definition at line 120 of file CClientTCPSocket.cpp.

References MRPT_END, and MRPT_START.

◆ writeAsync()

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.

Parameters
BufferThe data.
CoundThe number of bytes to write.
timeout_msThe 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.

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().

Friends And Related Function Documentation

◆ CServerTCPSocket

friend class CServerTCPSocket
friend

Definition at line 36 of file CClientTCPSocket.h.

Member Data Documentation

◆ DNS_LOOKUP_TIMEOUT_MS

unsigned int CClientTCPSocket::DNS_LOOKUP_TIMEOUT_MS = 3000
static

See description of CClientTCPSocket.

Definition at line 40 of file CClientTCPSocket.h.

◆ m_hSock

uint32_t mrpt::comms::CClientTCPSocket::m_hSock
protected

The handle for the connected TCP socket, or INVALID_SOCKET.

Definition at line 49 of file CClientTCPSocket.h.

◆ m_remotePartIP

std::string mrpt::comms::CClientTCPSocket::m_remotePartIP
protected

The IP address of the remote part of the connection.

Definition at line 56 of file CClientTCPSocket.h.

◆ m_remotePartPort

unsigned short mrpt::comms::CClientTCPSocket::m_remotePartPort
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