scclib
Stable Cloud Computing C++ Library
|
Socket base class. More...
#include <socket.h>
Public Member Functions | |
SocketBase (SocketBase &&other) | |
Move construct socket. | |
const SocketBase & | operator= (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 &) |
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.
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.
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.
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.
size_t SocketBase::recv | ( | void * | loc, |
size_t | len | ||
) |
|
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.
void SocketBase::recv_bufsize | ( | unsigned | s | ) |
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.
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.
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.
size_t SocketBase::send | ( | const void * | loc, |
size_t | len | ||
) |
|
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.
void SocketBase::send_bufsize | ( | unsigned | s | ) |
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.