scclib
Stable Cloud Computing C++ Library
Public Member Functions | Protected Member Functions | List of all members
scc::net::SocketBase Class Reference

Socket base class. More...

#include <socket.h>

Inheritance diagram for scc::net::SocketBase:
Inheritance graph
[legend]

Public Member Functions

 SocketBase (SocketBase &&other)
 Move construct socket.
 
const SocketBaseoperator= (SocketBase &&other)
 Move assign socket.
 
int fd () const
 Return the underlying socket handle.
 
 operator int () const
 Allow object to be cast to a socket handle.
 
void reuse_addr (bool r=true)
 Set address reusable. More...
 
void reuse_port (bool r=true)
 Set port reusable. More...
 
void non_blocking (bool b=true)
 Set the socket non-blocking. More...
 
std::error_code error_code ()
 Get the current error code (status). More...
 
void recv_bufsize (unsigned)
 Set receive buffer size. More...
 
unsigned recv_bufsize ()
 Get total receive buffer size including overhead.
 
void send_bufsize (unsigned)
 Set send buffer size. More...
 
unsigned send_bufsize ()
 Get total send buffer size including overhead.
 
void send_timeout (std::chrono::milliseconds)
 Set the send timeout. More...
 
void recv_timeout (std::chrono::milliseconds)
 Set the receive timeout. More...
 
void bind (const SockaddrBase &)
 Bind an address to the socket.
 
size_t recv (void *loc, size_t len)
 Receive bytes, throwing an exception on error. More...
 
size_t recv (void *loc, size_t len, std::error_code &e) noexcept
 Receive bytes. More...
 
size_t send (const void *loc, size_t len)
 Send bytes, throwing an exception on error. More...
 
size_t send (const void *loc, size_t len, std::error_code &e) noexcept
 Send bytes, setting the error code for the call. More...
 
void close ()
 Close the socket.
 

Protected Member Functions

 SocketBase (int fd)
 
 SocketBase (int, int, int)
 
void reset (int, int, int)
 
void move (SocketBase &other)
 
void get_sockaddr (sockaddr &)
 

Detailed Description

Socket base class.

In general most errors are returned via exception. Some of the interfaces can be called with an error_code reference, in which case the error return must be checked by the caller.

See also
SocketBase::error_code

Definition at line 76 of file socket.h.

Member Function Documentation

◆ error_code()

std::error_code SocketBase::error_code ( )

Get the current error code (status).

In general, error category() is std::error_code::system_category

Error value() 0 means success, otherwise this is a system error.

<cerror> header contains system errors, e.g. EINPROGRESS means a call on a non-blocking socket cannot be completed immediately.

Definition at line 94 of file socket.cc.

◆ non_blocking()

void SocketBase::non_blocking ( bool  b = true)

Set the socket non-blocking.

Most socket operations that would normally block will now return with EAGAIN error code.

connect() calls will return the EINPROGRESS error.

Allows polling of the socket.

See also
scc::util::Poller

From the system documentation, the following polling flags can be used for the socket: ┌────────────────────────────────────────────────────────────────────┐ │ I/O events │ ├───────────┬───────────┬────────────────────────────────────────────┤ │Event │ Poll flag │ Occurrence │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read │ POLLIN │ New data arrived. │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read │ POLLIN │ A connection setup has been completed (for │ │ │ │ connection-oriented sockets) │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read │ POLLHUP │ A disconnection request has been initiated │ │ │ │ by the other end. │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read │ POLLHUP │ A connection is broken (only for connec‐ │ │ │ │ tion-oriented protocols). When the socket │ │ │ │ is written SIGPIPE is also sent. │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Write │ POLLOUT │ Socket has enough send buffer space for │ │ │ │ writing new data. │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read/Write │ POLLIN | │ An outgoing connect(2) finished. │ │ │ POLLOUT │ │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read/Write │ POLLERR │ An asynchronous error occurred. │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Read/Write │ POLLHUP │ The other end has shut down one direction. │ ├───────────┼───────────┼────────────────────────────────────────────┤ │Exception │ POLLPRI │ Urgent data arrived. SIGURG is sent then. │ └───────────┴───────────┴────────────────────────────────────────────┘

Default is false.

Definition at line 188 of file socket.cc.

◆ recv() [1/2]

size_t SocketBase::recv ( void *  loc,
size_t  len 
)

Receive bytes, throwing an exception on error.

Returns the number of bytes received. If a non-zero number of bytes is requested, 0 will be returned if the peer has performed an orderly shutdown.

Definition at line 267 of file socket.cc.

◆ recv() [2/2]

size_t SocketBase::recv ( void *  loc,
size_t  len,
std::error_code &  e 
)
noexcept

Receive bytes.

Returns the number of bytes received. If a non-zero number of bytes is requested, 0 will be returned if the peer has performed an orderly shutdown.

The caller should check the error code(), with 0 meaning success.

Non-blocking sockets may return error codes of EAGAIN or EWOULDBLOCK, meaning that there is no data available, and no other error.

Definition at line 247 of file socket.cc.

◆ recv_bufsize()

void SocketBase::recv_bufsize ( unsigned  s)

Set receive buffer size.

Minimum input value is 128. The system will double the value input to account for bookeeping overhead.

Definition at line 108 of file socket.cc.

◆ recv_timeout()

void SocketBase::recv_timeout ( std::chrono::milliseconds  t)

Set the receive timeout.

When a recv() is called on a blocking socket:

If the timeout elapses and there is data available, the recv() will return the amount of data available. If no data is availble, the system will return an error of EAGAIN or EWOULDBLOCK as if the socket were non-blocking.

The default is 0, meaning that the recv() has no timeout.

Definition at line 232 of file socket.cc.

◆ reuse_addr()

void SocketBase::reuse_addr ( bool  r = true)

Set address reusable.

Allows a bind to an address if there is not an active listener on the address.

The system may delay releasing a non-reusable address after it has been used.

The system default is non-reusable.

Examples
net/unittest/inet_stream.cc.

Definition at line 162 of file socket.cc.

◆ reuse_port()

void SocketBase::reuse_port ( bool  r = true)

Set port reusable.

Allows multiple sockets to be bound to an identical address.

This must be set on each socket before calling bind().

For TCP sockets, multiple threads or processes can now have a distinct listening socket.

This may be more efficient than using a single listening thread to distribute connections, or having multiple threads that compete to accept from the same socket.

For UDP sockets, having a distinct socket per thread or process may be more efficient than having multiple threads compete for datagrams from the same socket.

The system default is non-reuseable.

Definition at line 175 of file socket.cc.

◆ send() [1/2]

size_t SocketBase::send ( const void *  loc,
size_t  len 
)

Send bytes, throwing an exception on error.

Returns the number of bytes sent.

Definition at line 300 of file socket.cc.

◆ send() [2/2]

size_t SocketBase::send ( const void *  loc,
size_t  len,
std::error_code &  e 
)
noexcept

Send bytes, setting the error code for the call.

Returns the number of bytes sent.

The caller should check the error code(), with 0 meaning success.

Non-blocking sockets may return error codes of EAGAIN or EWOULDBLOCK, meaning that there is no data available, and no other error.

Definition at line 280 of file socket.cc.

◆ send_bufsize()

void SocketBase::send_bufsize ( unsigned  s)

Set send buffer size.

Minimum input value is 1024. The system will double the value input to account for bookeeping overhead.

Definition at line 135 of file socket.cc.

◆ send_timeout()

void SocketBase::send_timeout ( std::chrono::milliseconds  t)

Set the send timeout.

When a send() is called on a blocking socket:

If the timeout elapses and there is data available, the send() will return the amount of data available. If no data is availble, the system will return an error of EAGAIN or EWOULDBLOCK as if the socket were non-blocking.

The default is 0, meaning that the send() has no timeout.

Definition at line 217 of file socket.cc.


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