3es  0.7
tes::TcpSocket Class Reference

A TCP/IP communication socket implementation. More...

#include <3estcpsocket.h>

Public Member Functions

 TcpSocket ()
 Constructor.
 
 TcpSocket (TcpSocketDetail *detail)
 Constructor.
 
 ~TcpSocket ()
 Destructor.
 
bool open (const char *host, unsigned short port)
 Open a connection to the target host and port (blocking). More...
 
void close ()
 Close the socket connection (no custom messages sent). Safe to call when not open.
 
bool isConnected () const
 Checks the connected state. More...
 
void setNoDelay (bool noDelay)
 Disable Nagle's algorithm, effectively disabling send delays? More...
 
bool noDelay () const
 Check if Nagle's algorithm is disable. More...
 
void setReadTimeout (unsigned timeoutMs)
 Sets the blocking timeout on calls to read(). More...
 
unsigned readTimeout () const
 Returns the read timeout. More...
 
void setIndefiniteReadTimeout ()
 Clears the timeout for read() calls, blocking indefinitely until data are available. More...
 
void setWriteTimeout (unsigned timeoutMs)
 Sets the blocking timeout on calls to write(). More...
 
unsigned writeTimeout () const
 Returns the write timeout. More...
 
void setIndefiniteWriteTimeout ()
 Clears the timeout for read() calls, blocking indefinitely until data have been sent. More...
 
void setReadBufferSize (int bufferSize)
 Sets the read buffer size (bytes). More...
 
int readBufferSize () const
 Gets the current read buffer size (bytes).
 
void setSendBufferSize (int bufferSize)
 Sets the send buffer size (bytes). More...
 
int sendBufferSize () const
 Gets the current send buffer size (bytes).
 
int read (char *buffer, int bufferLength) const
 Attempts to read data from the socket until the buffer is full. More...
 
int read (unsigned char *buffer, int bufferLength) const
 
int readAvailable (char *buffer, int bufferLength) const
 Reads available data from the socket, returning immediately if there are no data available. More...
 
int readAvailable (unsigned char *buffer, int bufferLength) const
 
int write (const char *buffer, int bufferLength) const
 Attempts to write data from the socket. More...
 
int write (const unsigned char *buffer, int bufferLength) const
 
unsigned short port () const
 

Static Public Attributes

static const unsigned IndefiniteTimeout
 Value used to signify an indefinite timeout.
 

Detailed Description

A TCP/IP communication socket implementation.

Member Function Documentation

◆ isConnected()

bool tes::TcpSocket::isConnected ( ) const

Checks the connected state.

Bug:
This is not reliable for a client socket. It only works when the TcpSocket was created from a TcpListenSocket.
Returns
true if the socket is currently connected.

◆ noDelay()

bool tes::TcpSocket::noDelay ( ) const

Check if Nagle's algorithm is disable.

Returns
True if Nagle's algorithm is disabled.

◆ open()

bool tes::TcpSocket::open ( const char *  host,
unsigned short  port 
)

Open a connection to the target host and port (blocking).

Parameters
hostThe host IP address or host name.
portThe target port.
Returns
True if the connection was successfully established before timing out.

◆ read() [1/2]

int tes::TcpSocket::read ( char *  buffer,
int  bufferLength 
) const

Attempts to read data from the socket until the buffer is full.

This may block. The blocking time may vary, but it will only block for at least the read timeout value so long as there is no activity.

Parameters
bufferThe data buffer to read into.
bufferLengthThe maximum number of types to read into buffer.
Returns
The number of bytes read, which may be less than bufferLength, or -1 on error.

◆ read() [2/2]

int tes::TcpSocket::read ( unsigned char *  buffer,
int  bufferLength 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References read().

Referenced by read().

◆ readAvailable() [1/2]

int tes::TcpSocket::readAvailable ( char *  buffer,
int  bufferLength 
) const

Reads available data from the socket, returning immediately if there are no data available.

Parameters
bufferThe data buffer to read into.
bufferLengthThe maximum number of types to read into buffer.
Returns
The number of bytes read, or -1 on error.

◆ readAvailable() [2/2]

int tes::TcpSocket::readAvailable ( unsigned char *  buffer,
int  bufferLength 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References readAvailable().

Referenced by readAvailable().

◆ readTimeout()

unsigned tes::TcpSocket::readTimeout ( ) const

Returns the read timeout.

Returns
The current read timeout in milliseconds. A value of IndefiniteTimeout indicates indefinite blocking.

◆ setIndefiniteReadTimeout()

void tes::TcpSocket::setIndefiniteReadTimeout ( )

Clears the timeout for read() calls, blocking indefinitely until data are available.

◆ setIndefiniteWriteTimeout()

void tes::TcpSocket::setIndefiniteWriteTimeout ( )

Clears the timeout for read() calls, blocking indefinitely until data have been sent.

◆ setNoDelay()

void tes::TcpSocket::setNoDelay ( bool  noDelay)

Disable Nagle's algorithm, effectively disabling send delays?

Parameters
noDelayDisable the delay?

◆ setReadBufferSize()

void tes::TcpSocket::setReadBufferSize ( int  bufferSize)

Sets the read buffer size (bytes).

Parameters
bufferSizeThe new buffer size. Max is 2^16 - 1.

◆ setReadTimeout()

void tes::TcpSocket::setReadTimeout ( unsigned  timeoutMs)

Sets the blocking timeout on calls to read().

All calls to read() either until there are data available or this time elapses. Set to zero for non-blocking Use setIndefiniteReadTimeout() to block undefinately. read() calls.

Parameters
timeoutMsRead timeout in milliseconds.

◆ setSendBufferSize()

void tes::TcpSocket::setSendBufferSize ( int  bufferSize)

Sets the send buffer size (bytes).

Parameters
bufferSizeThe new buffer size. Max is 2^16 - 1.

◆ setWriteTimeout()

void tes::TcpSocket::setWriteTimeout ( unsigned  timeoutMs)

Sets the blocking timeout on calls to write().

This behaves in the same way as setReadTimeout(), except that it relates to write() calls.

Parameters
timeoutMsRead timeout in milliseconds.

◆ write() [1/2]

int tes::TcpSocket::write ( const char *  buffer,
int  bufferLength 
) const

Attempts to write data from the socket.

This may block for the set write timeout.

Parameters
bufferThe data buffer to send.
bufferLengthThe number of bytes to send.
Returns
The number of bytes sent, or -1 on error.

◆ write() [2/2]

int tes::TcpSocket::write ( const unsigned char *  buffer,
int  bufferLength 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References write().

Referenced by write().

◆ writeTimeout()

unsigned tes::TcpSocket::writeTimeout ( ) const

Returns the write timeout.

Returns
The current write timeout in milliseconds. A value of IndefiniteTimeout indicates indefinite blocking.

The documentation for this class was generated from the following file: