sockpp
Modern C++ socket library wrapper
|
Base class for socket objects. More...
#include <socket.h>
Public Member Functions | |
socket () | |
Creates an unconnected (invalid) socket. | |
socket (socket_t h) | |
Creates a socket from an existing OS socket handle. More... | |
socket (socket &&sock) noexcept | |
Move constructor. More... | |
virtual | ~socket () |
Destructor closes the socket. | |
bool | is_open () const |
Determines if the socket is open (valid). More... | |
bool | operator! () const |
Determines if the socket is closed or in an error state. More... | |
operator bool () const | |
Determines if the socket is open and in an error-free state. More... | |
socket_t | handle () const |
Get the underlying OS socket handle. More... | |
virtual sa_family_t | family () const |
Gets the network family of the address to which the socket is bound. More... | |
socket | clone () const |
Creates a new socket that refers to this one. More... | |
void | clear (int val=0) |
Clears the error flag for the object. More... | |
socket_t | release () |
Releases ownership of the underlying socket object. More... | |
void | reset (socket_t h=INVALID_SOCKET) |
Replaces the underlying managed socket object. More... | |
socket & | operator= (socket &&sock) noexcept |
Move assignment. More... | |
bool | bind (const sock_address &addr) |
Binds the socket to the specified address. More... | |
sock_address_any | address () const |
Gets the local address to which the socket is bound. More... | |
sock_address_any | peer_address () const |
Gets the address of the remote peer, if this socket is connected. More... | |
bool | get_option (int level, int optname, void *optval, socklen_t *optlen) const |
Gets the value of a socket option. More... | |
template<typename T > | |
bool | get_option (int level, int optname, T *val) const |
Gets the value of a socket option. More... | |
bool | set_option (int level, int optname, const void *optval, socklen_t optlen) |
Sets the value of a socket option. More... | |
template<typename T > | |
bool | set_option (int level, int optname, const T &val) |
Sets the value of a socket option. More... | |
bool | set_non_blocking (bool on=true) |
Places the socket into or out of non-blocking mode. More... | |
int | last_error () const |
Gets the code for the last errror. More... | |
std::string | last_error_str () const |
Gets a string describing the last errror. More... | |
bool | shutdown (int how=SHUT_RDWR) |
Shuts down all or part of the full-duplex connection. More... | |
bool | close () |
Closes the socket. More... | |
Static Public Member Functions | |
static void | initialize () |
Initializes the socket (sockpp) library. More... | |
static void | destroy () |
Shuts down the socket library. More... | |
static socket | create (int domain, int type, int protocol=0) |
Creates a socket with the specified communications characterics. More... | |
static std::tuple< socket, socket > | pair (int domain, int type, int protocol=0) |
Creates a pair of connected sockets. More... | |
static std::string | error_str (int errNum) |
Gets a string describing the specified error. More... | |
Protected Member Functions | |
bool | close_on_err () |
Closes the socket without checking for errors or updating the last error. More... | |
void | set_last_error () |
Cache the last system error code into this object. More... | |
template<typename T > | |
T | check_ret (T ret) const |
Checks the value and if less than zero, sets last error. More... | |
template<typename T > | |
bool | check_ret_bool (T ret) const |
Checks the value and if less than zero, sets last error. More... | |
socket_t | check_socket (socket_t ret) const |
Checks the value and if it is not a valid socket, sets last error. More... | |
bool | check_socket_bool (socket_t ret) const |
Checks the value and if it is INVALID_SOCKET, sets last error. More... | |
Static Protected Member Functions | |
static int | get_last_error () |
OS-specific means to retrieve the last error from an operation. More... | |
Base class for socket objects.
This class wraps an OS socket handle and maintains strict ownership semantics. If a socket object has a valid, open socket, then it owns the handle and will close it when the object is destroyed.
Objects of this class are not copyable, but they are moveable.
|
inlineexplicit |
Creates a socket from an existing OS socket handle.
The object takes ownership of the handle and will close it when destroyed.
h | An OS socket handle. |
|
inlinenoexcept |
Move constructor.
This takes ownership of the underlying handle in sock.
sock | An rvalue reference to a socket object. |
sock_address_any sockpp::socket::address | ( | ) | const |
Gets the local address to which the socket is bound.
bool sockpp::socket::bind | ( | const sock_address & | addr | ) |
Binds the socket to the specified address.
addr | The address to which we get bound. |
|
inlineprotected |
Checks the value and if less than zero, sets last error.
T | A signed integer type of any size |
ret | The return value from a library or system call. |
ret
.
|
inlineprotected |
Checks the value and if less than zero, sets last error.
T | A signed integer type of any size |
ret | The return value from a library or system call. |
Checks the value and if it is not a valid socket, sets last error.
This is specifically required for Windows which uses an unsigned type for its SOCKET.
ret | The return value from a library or system call that returns a socket, such as socket() or accept(). |
ret
.
|
inlineprotected |
Checks the value and if it is INVALID_SOCKET, sets last error.
This is specifically required for Windows which uses an unsigned type for its SOCKET.
ret | The return value from a library or system call that returns a socket such as socket() or accept(). |
|
inline |
Clears the error flag for the object.
val | The value to set the flag, normally zero. |
socket sockpp::socket::clone | ( | ) | const |
Creates a new socket that refers to this one.
This creates a new object with an independent lifetime, but refers back to this same socket. On most systems, this duplicates the file handle using the dup() call. A typical use of this is to have separate threads for reading and writing the socket. One thread would get the original socket and the other would get the cloned one.
bool sockpp::socket::close | ( | ) |
Closes the socket.
After closing the socket, the handle is invalid, and can not be used again until reassigned.
|
inlineprotected |
Closes the socket without checking for errors or updating the last error.
This is used in internal open/connect type functions that fail after creating the socket, but want to preserve the previous failure condition. Assumes that the socket handle is valid.
|
static |
Creates a socket with the specified communications characterics.
Not that this is not normally how a socket is creates in the sockpp library. Applications would typically create a connector (client) or acceptor (server) socket which would take care of the details.
This is included for completeness or for creating different types of sockets than are supported by the library.
domain | The communications domain for the sockets (i.e. the address family) |
type | The communication semantics for the sockets (SOCK_STREAM, SOCK_DGRAM, etc). |
protocol | The particular protocol to be used with the sockets |
|
static |
Shuts down the socket library.
This is only required for Win32. On platforms that use a standard socket implementation this is an empty call.
|
static |
Gets a string describing the specified error.
This is typically the returned message from the system strerror().
errNum | The error number. |
|
inlinevirtual |
Gets the network family of the address to which the socket is bound.
|
staticprotected |
OS-specific means to retrieve the last error from an operation.
This should be called after a failed system call to set the lastErr_ member variable. Normally this would be called from check_ret.
bool sockpp::socket::get_option | ( | int | level, |
int | optname, | ||
void * | optval, | ||
socklen_t * | optlen | ||
) | const |
Gets the value of a socket option.
This is a thin wrapper for the system getsockopt
.
level | The protocol level at which the option resides, such as SOL_SOCKET. |
optname | The option passed directly to the protocol module. |
optval | The buffer for the value to retrieve |
optlen | Initially contains the lenth of the buffer, and on return contains the length of the value retrieved. |
|
inline |
Gets the value of a socket option.
level | The protocol level at which the option resides, such as SOL_SOCKET. |
optname | The option passed directly to the protocol module. |
val | The value to retrieve |
|
inline |
Get the underlying OS socket handle.
|
static |
Initializes the socket (sockpp) library.
This is only required for Win32. On platforms that use a standard socket implementation this is an empty call.
|
inline |
Determines if the socket is open (valid).
|
inline |
Gets the code for the last errror.
This is typically the code from the underlying OS operation.
|
inline |
Gets a string describing the last errror.
This is typically the returned message from the system strerror().
|
inlineexplicit |
Determines if the socket is open and in an error-free state.
|
inline |
Determines if the socket is closed or in an error state.
Move assignment.
This assigns ownership of the socket from the other object to this one.
|
static |
Creates a pair of connected sockets.
Whether this will work at all is highly system and domain dependent. Currently it is only known to work for Unix-domain sockets on *nix systems.
Note that applications would normally call this from a derived socket class which would return the properly type-cast sockets to match the domain
and type
.
domain | The communications domain for the sockets (i.e. the address family) |
type | The communication semantics for the sockets (SOCK_STREAM, SOCK_DGRAM, etc). |
protocol | The particular protocol to be used with the sockets |
sock_address_any sockpp::socket::peer_address | ( | ) | const |
Gets the address of the remote peer, if this socket is connected.
|
inline |
Releases ownership of the underlying socket object.
void sockpp::socket::reset | ( | socket_t | h = INVALID_SOCKET | ) |
Replaces the underlying managed socket object.
h | The new socket handle to manage. |
|
inlineprotected |
Cache the last system error code into this object.
This should be called after a failed system call to store the error value.
bool sockpp::socket::set_non_blocking | ( | bool | on = true | ) |
Places the socket into or out of non-blocking mode.
When in non-blocking mode, a call that is not immediately ready to complete (read, write, accept, etc) will return immediately with the error EWOULDBLOCK.
on | Whether to turn non-blocking mode on or off. |
bool sockpp::socket::set_option | ( | int | level, |
int | optname, | ||
const void * | optval, | ||
socklen_t | optlen | ||
) |
Sets the value of a socket option.
This is a thin wrapper for the system setsockopt
.
level | The protocol level at which the option resides, such as SOL_SOCKET. |
optname | The option passed directly to the protocol module. |
optval | The buffer with the value to set. |
optlen | Contains the lenth of the value buffer. |
|
inline |
Sets the value of a socket option.
level | The protocol level at which the option resides, such as SOL_SOCKET. |
optname | The option passed directly to the protocol module. |
val | The value to set. |
bool sockpp::socket::shutdown | ( | int | how = SHUT_RDWR | ) |
Shuts down all or part of the full-duplex connection.
how | Which part of the connection should be shut:
|