dodo
0.0.1
A C++ library to create containerized Linux services
|
A Linux socket. More...
#include <socket.hpp>
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 Socket * | accept () |
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... | |
![]() | |
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... | |
BaseSocket & | operator= (const BaseSocket &socket) |
Assign from Socket. More... | |
BaseSocket & | operator= (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... | |
![]() | |
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 | |
![]() | |
std::string | debugHeader () const |
Generates a debug header (address of this object and a demangled class name. More... | |
![]() | |
int | socket_ |
The socket file decsriptor. More... | |
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.
|
inline |
Default constructor creates an invalid socket.
Definition at line 57 of file socket.hpp.
Referenced by accept().
|
inline |
Construct from a socket descriptor.
socket | The socket file descriptor. |
Definition at line 63 of file socket.hpp.
|
inline |
Construct from SocketParams.
blocking | If true, create the socket in blocking mode. |
params | The SocketParams to use. |
Definition at line 70 of file socket.hpp.
|
inline |
Construct with default SocketParams.
blocking | If true, create the socket in blocking mode. |
Definition at line 76 of file socket.hpp.
|
inlinevirtual |
Destructs this Socket, but does not call close().
Definition at line 81 of file socket.hpp.
|
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.
Implements dodo::network::BaseSocket.
Definition at line 98 of file socket.cpp.
References Socket(), dodo::network::BaseSocket::socket_, SocketInvalid, and throw_SystemExceptionObject.
|
virtual |
Receive bytes.
buf | The buffer to receive the data into. |
request | The number of bytes to receive (request <= sizeof(buf) ). |
received | The number of bytes actuially received |
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.
|
virtual |
Send len bytes as a bytestream.
connect must have been called first.
buf | The bytes to send. |
len | The number of bytes to send from buf. |
more | If true, do no forec a network send right away, but wait until send buffer full or a subsequent call with more set to false. |
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.
|
virtual |
Send raw packets to the given Address.
address | The destination Address |
buf | The bytes to send |
len | The number of bytes to send. |
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.
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().