dodo  0.0.1
A C++ library to create containerized Linux services
dodo::network::Socket Class Reference

A Linux socket. More...

#include <socket.hpp>

Inheritance diagram for dodo::network::Socket:
Collaboration diagram for dodo::network::Socket:

Public Member Functions

 Socket ()
 Default constructor creates an invalid socket. More...
 
 Socket (bool blocking)
 Construct with default SocketParams. More...
 
 Socket (bool blocking, SocketParams params)
 Construct from SocketParams. More...
 
 Socket (int socket)
 Construct from a socket descriptor. More...
 
virtual ~Socket ()
 Destructs this Socket, but does not call close(). More...
 
virtual Socketaccept ()
 Accepts a connection request and return a pointer to a new Socket for the new connection, the caller will own the new Socket object. More...
 
virtual common::SystemError receive (void *buf, ssize_t request, ssize_t &received)
 Receive bytes. More...
 
virtual common::SystemError send (const void *buf, ssize_t len, bool more=false)
 Send len bytes as a bytestream. More...
 
virtual common::SystemError sendTo (const Address &address, const void *buf, ssize_t len)
 Send raw packets to the given Address. More...
 
- Public Member Functions inherited from dodo::network::BaseSocket
 BaseSocket ()
 Default constructor creates an invalid socket. More...
 
 BaseSocket (bool blocking, SocketParams params)
 Construct from SocketParams. More...
 
 BaseSocket (int socket)
 Construct from a socket descriptor. More...
 
virtual ~BaseSocket ()
 Destructs this Socket, but does not call close(). More...
 
common::SystemError bind (const Address &address)
 Bind the socket to the Address. More...
 
virtual void close ()
 Closes the socket, causing the connection, if it exists, to be terminated. More...
 
virtual common::SystemError connect (const Address &address)
 Connect to the address. More...
 
virtual std::string debugDetail () const
 Return debug object state as a string. More...
 
Address getAddress () const
 Get the local address for this socket. More...
 
SocketParams::AddressFamily getAddressFamily () const
 Get the SocketParams::AddressFamily of the socket. More...
 
virtual bool getBlocking () const
 Rerurn true if the socket is operating in blocking mode. More...
 
int getFD () const
 Return the socket file descriptor. More...
 
Address getPeerAddress () const
 Get the peer (remote) address for this socket. More...
 
SocketParams::ProtocolNumber getProtocolNumber () const
 Get the SocketParams::ProtocolNumber of the socket. More...
 
virtual socklen_t getReceiveBufSize () const
 Get the maximum buffer length for receive. More...
 
socklen_t getSendBufSize () const
 Get the maximum buffer length for send. More...
 
SocketParams getSocketParams () const
 Return the SocketParams. More...
 
SocketParams::SocketType getSocketType () const
 Get the SocketParams::SocketType of the socket. More...
 
int getTTL () const
 Get the Socket TTL (time to live) or the max number of packet hops. More...
 
bool isValid () const
 Return true when the socket descriptor is a valid, hence 'possible' descriptor. More...
 
common::SystemError listen (const Address &address, int backlog)
 Sets up a listening socket on Address. More...
 
bool operator< (const BaseSocket &socket) const
 Add ordering. More...
 
BaseSocketoperator= (const BaseSocket &socket)
 Assign from Socket. More...
 
BaseSocketoperator= (int socket)
 Assign from existing socket descriptor (int). More...
 
bool operator== (const BaseSocket &socket) const
 Add identity. More...
 
common::SystemError receiveInt16 (int16_t &value)
 receive an int16_t More...
 
common::SystemError receiveInt32 (int32_t &value)
 receive an int32_t More...
 
common::SystemError receiveInt64 (int64_t &value)
 receive an int64_t More...
 
common::SystemError receiveInt8 (int8_t &value)
 receive an int8_t More...
 
common::SystemError receiveLine (std::string &s)
 receive a stream of characters until a '
' is read (consumed from the socket read buffer but not added to s). More...
 
common::SystemError receiveString (std::string &s)
 Receive a std::string as sent by sendString( const std::string& ). More...
 
common::SystemError receiveUInt16 (uint16_t &value)
 receive an uint16_t More...
 
common::SystemError receiveUInt32 (uint32_t &value)
 receive an uint32_t More...
 
common::SystemError receiveUInt64 (uint64_t &value)
 receive an uint64_t More...
 
common::SystemError receiveUInt8 (uint8_t &value)
 receive an uint8_t More...
 
common::SystemError sendInt16 (int16_t value, bool more=false)
 Send an int16_t. More...
 
common::SystemError sendInt32 (int32_t value, bool more=false)
 Send an int32_t. More...
 
common::SystemError sendInt64 (int64_t value, bool more=false)
 Send an int64_t. More...
 
common::SystemError sendInt8 (int8_t value, bool more=false)
 Send an int8_t. More...
 
common::SystemError sendLine (const std::string &s, bool more)
 Sends the std::string terminated by a '
'. More...
 
common::SystemError sendString (const std::string &s, bool more=false)
 Sends the std::string as understood by receiveString(). More...
 
common::SystemError sendUInt16 (uint16_t value, bool more=false)
 Send an uint16_t. More...
 
common::SystemError sendUInt32 (uint32_t value, bool more=false)
 Send an uint32_t. More...
 
common::SystemError sendUInt64 (uint64_t value, bool more=false)
 Send an sendUInt64. More...
 
common::SystemError sendUInt8 (uint8_t value, bool more=false)
 Send an uint8_t. More...
 
void setBlocking (bool blocking)
 Set the Socket blocking mode. More...
 
void setReceiveBufSize (socklen_t size)
 Set the size of the receive buffer. More...
 
void setReceiveTimeout (double sec)
 Set the receive timeout - a receive will fail if there is no data received before the timeout expires. More...
 
void setReUseAddress ()
 Enable the socket to re-use an address when listen/bind is called. More...
 
void setReUsePort ()
 Make the socket re-use a port when listen is called. More...
 
void setSendBufSize (socklen_t size)
 Set the size of the send buffer. More...
 
void setSendTimeout (double sec)
 Set the send timeout - a send will fail if there is no data send before the timeout expires. More...
 
void setTCPKeepAlive (bool enable)
 Enable or disable TCP keep-alive on the socket. More...
 
void setTCPNoDelay (bool set)
 Set TCP_NODELAY. More...
 
void setTTL (int ttl)
 Set the Socket TTL (Time to Live). More...
 
- Public Member Functions inherited from dodo::common::DebugObject
 DebugObject ()
 Default constructor does nothing. More...
 
virtual ~DebugObject ()
 Destructor does nothing. More...
 
std::string debugString () const
 Return the object dump to string. More...
 

Static Public Attributes

static Socket SocketInvalid = Socket()
 An invalid SocketInvalid for comparison convenience - initialized to a Socket with socket fd=-1. More...
 

Additional Inherited Members

- Protected Member Functions inherited from dodo::common::DebugObject
std::string debugHeader () const
 Generates a debug header (address of this object and a demangled class name. More...
 
- Protected Attributes inherited from dodo::network::BaseSocket
int socket_
 The socket file decsriptor. More...
 

Detailed Description

A Linux socket.

Socket objects refer to, but do not own a particular socket descriptor. The programmer controls closing the socket by calling close(), the Socket destructor does not do this implicitly, as two Socket objects may refer to the same socket descriptor.

The Socket operates either in blocking or non-blocking mode. In blocking mode, calls to listen() or receive() will block until data is received. Moreover, in blocking mode, there is no limit to the amount of data that can be sent or received in one go. In non-blocking mode, the send and receive sizes per call must fit in Socket::getSendBufSize() or Socket::getReceiveBufSize().

Definition at line 50 of file socket.hpp.

Constructor & Destructor Documentation

◆ Socket() [1/4]

dodo::network::Socket::Socket ( )
inline

Default constructor creates an invalid socket.

Definition at line 57 of file socket.hpp.

Referenced by accept().

Here is the caller graph for this function:

◆ Socket() [2/4]

dodo::network::Socket::Socket ( int  socket)
inline

Construct from a socket descriptor.

Parameters
socketThe socket file descriptor.

Definition at line 63 of file socket.hpp.

◆ Socket() [3/4]

dodo::network::Socket::Socket ( bool  blocking,
SocketParams  params 
)
inline

Construct from SocketParams.

Parameters
blockingIf true, create the socket in blocking mode.
paramsThe SocketParams to use.

Definition at line 70 of file socket.hpp.

◆ Socket() [4/4]

dodo::network::Socket::Socket ( bool  blocking)
inline

Construct with default SocketParams.

Parameters
blockingIf true, create the socket in blocking mode.

Definition at line 76 of file socket.hpp.

◆ ~Socket()

virtual dodo::network::Socket::~Socket ( )
inlinevirtual

Destructs this Socket, but does not call close().

Definition at line 81 of file socket.hpp.

Member Function Documentation

◆ accept()

Socket * dodo::network::Socket::accept ( )
virtual

Accepts a connection request and return a pointer to a new Socket for the new connection, the caller will own the new Socket object.

Returns
The accepted socket.

Implements dodo::network::BaseSocket.

Definition at line 98 of file socket.cpp.

References Socket(), dodo::network::BaseSocket::socket_, SocketInvalid, and throw_SystemExceptionObject.

Here is the call graph for this function:

◆ receive()

common::SystemError dodo::network::Socket::receive ( void *  buf,
ssize_t  request,
ssize_t &  received 
)
virtual

Receive bytes.

Parameters
bufThe buffer to receive the data into.
requestThe number of bytes to receive (request <= sizeof(buf) ).
receivedThe number of bytes actuially received
Returns
The error code:
  • SystemError::ecOK
  • SystemError::ecEAGAIN
  • SystemError::ecENOTCONN
  • SystemError::ecECONNREFUSED On other errors returned by the internal call to recv, an exception is thrown.

Implements dodo::network::BaseSocket.

Definition at line 81 of file socket.cpp.

References dodo::common::SystemError::ecEAGAIN, dodo::common::SystemError::ecECONNREFUSED, dodo::common::SystemError::ecENOTCONN, dodo::common::SystemError::ecOK, dodo::network::BaseSocket::socket_, and throw_SystemExceptionObject.

◆ send()

common::SystemError dodo::network::Socket::send ( const void *  buf,
ssize_t  len,
bool  more = false 
)
virtual

Send len bytes as a bytestream.

connect must have been called first.

Parameters
bufThe bytes to send.
lenThe number of bytes to send from buf.
moreIf true, do no forec a network send right away, but wait until send buffer full or a subsequent call with more set to false.
Returns
SystemError::ecOK or
  • SystemError::ecEAGAIN: For non-blocking sockets, neither failure nor succces, try again.
  • SystemError::ecECONNRESET : The peer has disconnected during the send.
See also
sendTo()

Implements dodo::network::BaseSocket.

Definition at line 36 of file socket.cpp.

References dodo::common::SystemError::ecEAGAIN, dodo::common::SystemError::ecECONNRESET, dodo::common::SystemError::ecOK, dodo::network::BaseSocket::socket_, and throw_SystemExceptionObject.

◆ sendTo()

common::SystemError dodo::network::Socket::sendTo ( const Address address,
const void *  buf,
ssize_t  len 
)
virtual

Send raw packets to the given Address.

Parameters
addressThe destination Address
bufThe bytes to send
lenThe number of bytes to send.
Returns
The SystemError.

Definition at line 59 of file socket.cpp.

References dodo::network::Address::addr_, dodo::common::SystemError::ecEAGAIN, dodo::common::SystemError::ecECONNRESET, dodo::common::SystemError::ecOK, dodo::network::BaseSocket::socket_, and throw_SystemExceptionObject.

Field Documentation

◆ SocketInvalid

Socket dodo::network::Socket::SocketInvalid = Socket()
static

An invalid SocketInvalid for comparison convenience - initialized to a Socket with socket fd=-1.

Definition at line 129 of file socket.hpp.

Referenced by accept().


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