argos::CTCPSocket Class Reference

#include <tcp_socket.h>

Public Types

enum class  EEvent : UInt32 {
  InputReady , OutputReady , HangUp , ErrorCondition ,
  InvalidRequest
}
 

Public Member Functions

 CTCPSocket (int n_stream=-1)
 
 CTCPSocket (const CTCPSocket &c_other)=delete
 
 CTCPSocket (CTCPSocket &&c_other)
 
 ~CTCPSocket ()
 
CTCPSocketoperator= (const CTCPSocket &c_other)=delete
 
CTCPSocketoperator= (CTCPSocket &&c_other)
 
bool operator== (const CTCPSocket &c_other) const
 Returns true if the two sockets refer to same file descriptor. More...
 
bool IsConnected () const
 Returns true if the socket is connected. More...
 
int GetStream () const
 Returns the socket stream. More...
 
const std::string & GetAddress () const
 Returns a string containing the IPv4 address in dot notation. More...
 
void Connect (const std::string &str_hostname, SInt32 n_port)
 Connects this socket to the specified hostname and port. More...
 
void Listen (SInt32 n_port, SInt32 n_queue_length=10)
 Listens for connections on the specified local port. More...
 
void Accept (CTCPSocket &c_socket)
 Accept a connection from a client. More...
 
void Disconnect ()
 Close the socket. More...
 
std::unordered_set< EEventGetEvents ()
 Check the socket for events. More...
 
void SendBuffer (const UInt8 *pun_buffer, size_t un_size)
 Sends the passed buffer through the socket. More...
 
bool ReceiveBuffer (UInt8 *pun_buffer, size_t un_size)
 Fills the passed buffer with the data received through the socket. More...
 
void SendByteArray (const CByteArray &c_byte_array)
 Sends the passed byte array through the socket. More...
 
bool ReceiveByteArray (CByteArray &c_byte_array)
 Receives the passed byte array through the socket. More...
 
void SendMsg (const CByteArray &c_payload, bool b_more=false)
 
void RecvMsg (CByteArray &c_payload)
 

Detailed Description

Definition at line 15 of file tcp_socket.h.

Member Enumeration Documentation

◆ EEvent

Enumerator
InputReady 
OutputReady 
HangUp 
ErrorCondition 
InvalidRequest 

Definition at line 19 of file tcp_socket.h.

Constructor & Destructor Documentation

◆ CTCPSocket() [1/3]

argos::CTCPSocket::CTCPSocket ( int  n_stream = -1)

Definition at line 20 of file tcp_socket.cpp.

◆ CTCPSocket() [2/3]

argos::CTCPSocket::CTCPSocket ( const CTCPSocket c_other)
delete

◆ CTCPSocket() [3/3]

argos::CTCPSocket::CTCPSocket ( CTCPSocket &&  c_other)

Definition at line 27 of file tcp_socket.cpp.

◆ ~CTCPSocket()

argos::CTCPSocket::~CTCPSocket ( )

Definition at line 54 of file tcp_socket.cpp.

Member Function Documentation

◆ Accept()

void argos::CTCPSocket::Accept ( CTCPSocket c_socket)

Accept a connection from a client.

Internally, the connection is forced to be only IPv4. Before calling this function, you must first call Listen() to setup connection listening.

Parameters
c_socketThe socket on which the connection has been created
Exceptions
CARGoSExceptionin case of error
See also
Listen
Connect

Definition at line 149 of file tcp_socket.cpp.

◆ Connect()

void argos::CTCPSocket::Connect ( const std::string &  str_hostname,
SInt32  n_port 
)

Connects this socket to the specified hostname and port.

Internally, the connection is forced to be only IPv4.

Parameters
str_hostnameThe wanted hostname
n_portThe wanted port
Exceptions
CARGoSExceptionin case of error
See also
Accept

Definition at line 61 of file tcp_socket.cpp.

◆ Disconnect()

void argos::CTCPSocket::Disconnect ( )

Close the socket.

Exceptions
CARGoSExceptionin case of error

Definition at line 165 of file tcp_socket.cpp.

◆ GetAddress()

const std::string& argos::CTCPSocket::GetAddress ( ) const
inline

Returns a string containing the IPv4 address in dot notation.

Returns
A string containing the IPv4 address in dot notation.

Definition at line 69 of file tcp_socket.h.

◆ GetEvents()

std::unordered_set< CTCPSocket::EEvent > argos::CTCPSocket::GetEvents ( )

Check the socket for events.

Returns
an ordered set of events

Definition at line 173 of file tcp_socket.cpp.

◆ GetStream()

int argos::CTCPSocket::GetStream ( ) const
inline

Returns the socket stream.

Returns
the socket stream.

Definition at line 61 of file tcp_socket.h.

◆ IsConnected()

bool argos::CTCPSocket::IsConnected ( ) const
inline

Returns true if the socket is connected.

Returns
true if the socket is connected.

Definition at line 53 of file tcp_socket.h.

◆ Listen()

void argos::CTCPSocket::Listen ( SInt32  n_port,
SInt32  n_queue_length = 10 
)

Listens for connections on the specified local port.

Internally, the connection is forced to be only IPv4. To actually accept connections, you must call Accept() after calling this function.

Parameters
n_portThe wanted port
n_queue_lengthThe maximum length of the queue of pending connections (also called the backlog)
Exceptions
CARGoSExceptionin case of error
See also
Accept

Definition at line 101 of file tcp_socket.cpp.

◆ operator=() [1/2]

CTCPSocket& argos::CTCPSocket::operator= ( const CTCPSocket c_other)
delete

◆ operator=() [2/2]

CTCPSocket & argos::CTCPSocket::operator= ( CTCPSocket &&  c_other)

Definition at line 38 of file tcp_socket.cpp.

◆ operator==()

bool argos::CTCPSocket::operator== ( const CTCPSocket c_other) const
inline

Returns true if the two sockets refer to same file descriptor.

Returns
true if the two sockets refer to same file descriptor.

Definition at line 45 of file tcp_socket.h.

◆ ReceiveBuffer()

bool argos::CTCPSocket::ReceiveBuffer ( UInt8 pun_buffer,
size_t  un_size 
)

Fills the passed buffer with the data received through the socket.

Parameters
pun_bufferThe buffer to fill
un_sizeThe size of the buffer
Returns
true if the buffer was filled correctly; false if the connection was closed by the other peer
Exceptions
CARGoSExceptionin case of error

Definition at line 212 of file tcp_socket.cpp.

◆ ReceiveByteArray()

bool argos::CTCPSocket::ReceiveByteArray ( CByteArray c_byte_array)

Receives the passed byte array through the socket.

Internally, this function first receives the size of the byte array as a long int, and then receives the content of the byte array. It is meant to the be used in conjunction with SendByteArray().

Parameters
c_byte_arrayThe byte array
Returns
true if the buffer was filled correctly; false if the connection was closed by the other peer
Exceptions
CARGoSExceptionin case of error
See also
CByteArray
ReceiveBuffer
SendByteArray

Definition at line 242 of file tcp_socket.cpp.

◆ RecvMsg()

void argos::CTCPSocket::RecvMsg ( CByteArray c_payload)

Definition at line 272 of file tcp_socket.cpp.

◆ SendBuffer()

void argos::CTCPSocket::SendBuffer ( const UInt8 pun_buffer,
size_t  un_size 
)

Sends the passed buffer through the socket.

Parameters
pun_bufferThe wanted buffer
un_sizeThe size of the buffer
Exceptions
CARGoSExceptionin case of error

Definition at line 195 of file tcp_socket.cpp.

◆ SendByteArray()

void argos::CTCPSocket::SendByteArray ( const CByteArray c_byte_array)

Sends the passed byte array through the socket.

Internally, this function first sends the size of the byte array as a long int, and then sends the content of the byte array. It is meant to the be used in conjunction with ReceiveByteArray().

Parameters
c_byte_arrayThe byte array
Exceptions
CARGoSExceptionin case of error
See also
CByteArray
SendBuffer
ReceiveByteArray

Definition at line 231 of file tcp_socket.cpp.

◆ SendMsg()

void argos::CTCPSocket::SendMsg ( const CByteArray c_payload,
bool  b_more = false 
)

Definition at line 258 of file tcp_socket.cpp.