My Project
Loading...
Searching...
No Matches
chai3d::cSocket Class Referenceabstract

This class implements all methods shared by TCP and UDP sockets.

#include <CSocket.h>

Inheritance diagram for chai3d::cSocket:

Public Member Functions

 cSocket ()
 
 cSocket (int a_socket)
 
virtual ~cSocket ()
 
int getLocalPort ()
 This method returns the local port number that the socket is connected to.
 
std::string getRemoteIP ()
 This method retrieves the remote IP address that the socket is connected to.
 
int getRemotePort ()
 This method returns the remote port number that the socket is connected to.
 
int close ()
 This method closes the socket.
 
int peek (char *a_output, unsigned int a_size)
 This method reads a copy of incoming data (without removing the data).
 
int peek (std::string &a_output)
 This method reads a copy of incoming data (without removing the data).
 
int read (char *a_output, unsigned int a_size)
 This method reads a incoming data.
 
int read (std::string &a_output)
 This method reads a incoming data.
 
int readLine (char *a_output, unsigned int a_size)
 This method reads an entire line of data (delimited by the '\n' character).
 
int readLine (std::string &a_output)
 This method reads an entire line of data (delimited by the '\n' character).
 
int send (const char *a_data, int a_length)
 This method sends data through the socket.
 
int send (std::string a_data)
 This method sends data through the socket.
 
double getTimeout ()
 This method retrieves the socket timeout in (s).
 
int setTimeout (double a_timeout)
 This method sets the socket timeout in (s).
 
int getSendSize ()
 This method retrieves the socket outgoing buffer size.
 
int setSendSize (int a_size)
 This method sets the socket outgoing buffer size.
 
int getRecvSize ()
 This method retrieves the socket incoming buffer size.
 
int setRecvSize (int a_size)
 This method sets the socket incoming buffer size.
 
virtual unsigned char getAttributes ()=0
 This method retrieves the socket attributes.
 
virtual int setAttributes (unsigned char a_attributes)=0
 This method sets the socket attributes.
 

Static Public Member Functions

static std::string getLocalIP ()
 This method retrieves the local IP address.
 

Static Public Attributes

static const unsigned int BUFFERSIZE = 2048
 This constant defines the default outgoing and incoming socket buffer sizes.
 
static const unsigned char BLOCKING = 0x01
 This constant defines the attribute bit to hold the socket BLOCKING attribute.
 
static const unsigned char REUSEADDR = 0x02
 This constant defines the attribute bit to hold the socket REUSEADDR attribute.
 
static const unsigned char NODELAY = 0x04
 This constant defines the attribute bit to hold the socket NODELAY attribute.
 
static const unsigned char DEFAULT_ATTR = 0x00 | BLOCKING | REUSEADDR | ~NODELAY
 This constant defines the default attributes for new sockets.
 
static const int WOULDBLOCK = -2
 This return constant is used to signify that a non-blocking socket would block.
 
static const int SHORTLINE = -3
 This return constant is used to signify that a buffer is too small to hold a line.
 
static const int TIMEOUT = -4
 This return constant is used to signify that a read operation on a socket has timed out.
 

Protected Member Functions

unsigned char getBaseAttributes ()
 This method retrieves the socket attributes shared between TCP and UDP.
 
int setBaseAttributes (unsigned char a_attributes)
 This method sets the socket attributes shared between TCP and UDP.
 

Protected Attributes

int m_socket
 Unique socket identifier.
 
unsigned char m_attributes
 Socket attributes.
 
char m_buffer [BUFFERSIZE]
 Socket read buffer.
 
unsigned long m_remoteIP
 Socket remote IP address.
 
unsigned int m_remotePort
 Socket remote port number.
 
unsigned long m_localIP
 Socket local IP address.
 
unsigned int m_localPort
 Socket local port number.
 

Constructor & Destructor Documentation

◆ cSocket() [1/2]

chai3d::cSocket::cSocket ( )

Constructor of cSocket.
This method calls the global initialization function and sets sensible initial values for all data members.

◆ cSocket() [2/2]

chai3d::cSocket::cSocket ( int  a_socket)

Constructor of cSocket.
This method creates a base socket object around an existing socket handle and sets sensible initial values for all other data members.

Parameters
a_socketA valid handle to an existing socket.

◆ ~cSocket()

chai3d::cSocket::~cSocket ( )
virtual

Destructor of cSocket.

Member Function Documentation

◆ getLocalIP()

std::string chai3d::cSocket::getLocalIP ( )
static

This method retrieves the local IP address. If the system is connected to the network, this method returns the first non-loopback IP address available on the system. Otherwise, the loopback adapter address (usually 127.0.0.1) is returned.

Returns
a string containing the local IP on success, the loopback adapter address otherwise.

◆ getLocalPort()

int chai3d::cSocket::getLocalPort ( )

This method retrieves the local port number that the socket is connected to. If the socket is not connected, 0 is returned.

Returns
the local port number on success, 0 otherwise.

◆ getRemoteIP()

std::string chai3d::cSocket::getRemoteIP ( )

This method retrieves the remote IP address. If the socket is connected to a remote system, this method returns the remote IP address. Otherwise, an illegal IP address is returned (0.0.0.0).

Returns
a string containing the remote IP on success, an illegal IP otherwise.

◆ getRemotePort()

int chai3d::cSocket::getRemotePort ( )

This method retrieves the remote port number that the socket is connected to. If the socket is not connected, 0 is returned.

Returns
the remote port number on success, 0 otherwise.

◆ close()

int chai3d::cSocket::close ( )

This method closes the socket.

Returns
0 on success, -1 otherwise.

◆ peek() [1/2]

int chai3d::cSocket::peek ( char *  a_output,
unsigned int  a_size 
)

This method reads any available data on a socket into a given buffer, but without emptying the buffer. The size of the buffer returned depends on the buffer size and the amount of available data (whichever is smaller). If the BLOCKING attribute is set, the call blocks until data is available. Otherwise, the call returns immediately if no data is available.

Parameters
a_outputA buffer to collect the data requested.
a_sizeThe size of the a_output buffer in bytes.
Returns
The number of bytes read on success, TIMEOUT if the socket read operation timed out, WOULDBLOCK if the socket is non-blocking and there is no data available yet, -1 on error.

◆ peek() [2/2]

int chai3d::cSocket::peek ( std::string &  a_output)

This method reads any available data on a socket into a given string, but without emptying the buffer. The size of the string returned depends on the the amount of available data. If the BLOCKING attribute is set, the call blocks until data is available. Otherwise, the call returns immediately if no data is available.

Parameters
a_outputA string to collect the data requested.
Returns
The number of bytes read on success, TIMEOUT if the socket read operation timed out, WOULDBLOCK if the socket is non-blocking and there is no data available yet, -1 on error.

◆ read() [1/2]

int chai3d::cSocket::read ( char *  a_output,
unsigned int  a_size 
)

This method reads any available data on a socket into a given buffer. The size of the buffer returned depends on the buffer size and the amount of available data (whichever is smaller). If the BLOCKING attribute is set, the call blocks until data is available. Otherwise, the call returns immediately if no data is available.

Parameters
a_outputA buffer to collect the data requested.
a_sizeThe size of the a_output buffer in bytes.
Returns
The number of bytes read on success, TIMEOUT if the socket read operation timed out, WOULDBLOCK if the socket is non-blocking and there is no data available yet, -1 on error.

◆ read() [2/2]

int chai3d::cSocket::read ( std::string &  a_output)

This method reads any available data on a socket into a given string. The size of the buffer returned depends on the amount of available data. If the BLOCKING attribute is set, the call blocks until data is available. Otherwise, the call returns immediately if no data is available.

Parameters
a_outputA string to collect the data requested.
Returns
The number of bytes read on success, TIMEOUT if the socket read operation timed out, WOULDBLOCK if the socket is non-blocking and there is no data available yet, -1 on error.

◆ readLine() [1/2]

int chai3d::cSocket::readLine ( char *  a_output,
unsigned int  a_size 
)

This method reads a '
' terminated line of data on a socket into a given buffer. The size of the buffer returned depends on the buffer size and the amount of available data (whichever is smaller). If the BLOCKING attribute is set, the call blocks until data is available. Otherwise, the call returns immediately if no data is available. If the buffer is too small to hold a line, cSocket::SHORTLINE is returned.

Parameters
a_outputA buffer to collect the data requested.
a_sizeThe size of the a_output buffer in bytes.
Returns
The number of bytes read on success, TIMEOUT if the socket read operation timed out, WOULDBLOCK if the socket is non-blocking and there is no data available yet, SHORTLINE if the buffer is too small to hold the line, -1 on error.

◆ readLine() [2/2]

int chai3d::cSocket::readLine ( std::string &  a_output)

This method reads a '
' terminated line of data on a socket into a given string. The size of the buffer returned depends on the amount of available data. If the BLOCKING attribute is set, the call blocks until data is available. Otherwise, the call returns immediately if no data is available.

Parameters
a_outputA string to collect the data requested.
Returns
The number of bytes read on success, TIMEOUT if the socket read operation timed out, WOULDBLOCK if the socket is non-blocking and there is no data available yet, -1 on error.

◆ send() [1/2]

int chai3d::cSocket::send ( const char *  a_data,
int  a_length 
)

This method sends data over a socket.

Parameters
a_dataA buffer containing the data to send.
a_lengthThe data buffer length in bytes.
Returns
The number of bytes sent on success, TIMEOUT if the socket send operation timed out, WOULDBLOCK if the socket is non-blocking sending would block, -1 on error.

◆ send() [2/2]

int chai3d::cSocket::send ( std::string  a_data)

This method sends data over the socket.

Parameters
a_dataA string containing the data to send.
Returns
The number of bytes sent on success, TIMEOUT if the socket send operation timed out, WOULDBLOCK if the socket is non-blocking sending would block, -1 on error.

◆ getTimeout()

double chai3d::cSocket::getTimeout ( )

This method sets the timeout value in seconds for the socket.

Returns
The timeout value if set, 0 if timeout is not set, or -1 on error.

◆ setTimeout()

int chai3d::cSocket::setTimeout ( double  a_timeout)

This method retrieves the timeout value in seconds for a given socket.

Parameters
a_timeoutThe timeout value in seconds.
Returns
0 on success, or -1 otherwise.

◆ getSendSize()

int chai3d::cSocket::getSendSize ( )

This method retrieves the socket outgoing buffer size.

Returns
The outgoing buffer size in bytes on success, -1 on failure.

◆ setSendSize()

int chai3d::cSocket::setSendSize ( int  a_size)

This method sets the socket outgoing buffer size.

Parameters
a_sizeOutgoing buffer size in bytes.
Returns
0 on success, -1 otherwise.

◆ getRecvSize()

int chai3d::cSocket::getRecvSize ( )

This method retrieves the socket incoming buffer size.

Returns
The incoming buffer size in bytes on success, -1 on failure.

◆ setRecvSize()

int chai3d::cSocket::setRecvSize ( int  a_size)

This method sets the socket incoming buffer size.

Parameters
a_sizeIncoming buffer size in bytes.
Returns
0 on success, -1 otherwise.

◆ getAttributes()

virtual unsigned char chai3d::cSocket::getAttributes ( )
pure virtual

Implemented in chai3d::cSocketUDP, and chai3d::cSocketTCP.

◆ setAttributes()

virtual int chai3d::cSocket::setAttributes ( unsigned char  a_attributes)
pure virtual

Implemented in chai3d::cSocketUDP, and chai3d::cSocketTCP.

◆ getBaseAttributes()

unsigned char chai3d::cSocket::getBaseAttributes ( )
protected

This method retrieves the socket attributes that are shared between TCP and UDP configurations.

Attributes are single bit flags contained in a single byte. The following attributes are available:

Returns
A bitwise mask describing the socket attributes.

◆ setBaseAttributes()

int chai3d::cSocket::setBaseAttributes ( unsigned char  a_attributes)
protected

This method sets the socket attributes that are shared between TCP and UDP configurations.

This includes the following attributes:

Parameters
a_attributesA bitwise mask describing the socket attributes.
Returns
0 on success, -1 on failure.

Member Data Documentation

◆ BUFFERSIZE

const unsigned int chai3d::cSocket::BUFFERSIZE = 2048
static

◆ BLOCKING

const unsigned char chai3d::cSocket::BLOCKING = 0x01
static

◆ REUSEADDR

const unsigned char chai3d::cSocket::REUSEADDR = 0x02
static

◆ NODELAY

const unsigned char chai3d::cSocket::NODELAY = 0x04
static

◆ DEFAULT_ATTR

const unsigned char chai3d::cSocket::DEFAULT_ATTR = 0x00 | BLOCKING | REUSEADDR | ~NODELAY
static

◆ WOULDBLOCK

const int chai3d::cSocket::WOULDBLOCK = -2
static

◆ SHORTLINE

const int chai3d::cSocket::SHORTLINE = -3
static

◆ TIMEOUT

const int chai3d::cSocket::TIMEOUT = -4
static

◆ m_socket

int chai3d::cSocket::m_socket
protected

◆ m_attributes

unsigned char chai3d::cSocket::m_attributes
protected

◆ m_buffer

char chai3d::cSocket::m_buffer[BUFFERSIZE]
protected

◆ m_remoteIP

unsigned long chai3d::cSocket::m_remoteIP
protected

◆ m_remotePort

unsigned int chai3d::cSocket::m_remotePort
protected

◆ m_localIP

unsigned long chai3d::cSocket::m_localIP
protected

◆ m_localPort

unsigned int chai3d::cSocket::m_localPort
protected

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