Wizard
Software Engineering Project - Wizard
|
#include <socket.h>
Public Member Functions | |
socket () | |
socket (socket_t h) | |
socket (socket &&sock) noexcept | |
virtual | ~socket () |
bool | is_open () const |
bool | operator! () const |
operator bool () const | |
socket_t | handle () const |
virtual sa_family_t | family () const |
socket | clone () const |
void | clear (int val=0) |
socket_t | release () |
void | reset (socket_t h=INVALID_SOCKET) |
socket & | operator= (socket &&sock) noexcept |
bool | bind (const sock_address &addr) |
sock_address_any | address () const |
sock_address_any | peer_address () const |
bool | get_option (int level, int optname, void *optval, socklen_t *optlen) const |
template<typename T > | |
bool | get_option (int level, int optname, T *val) const |
bool | set_option (int level, int optname, const void *optval, socklen_t optlen) |
template<typename T > | |
bool | set_option (int level, int optname, const T &val) |
bool | set_non_blocking (bool on=true) |
int | last_error () const |
std::string | last_error_str () const |
bool | shutdown (int how=SHUT_RDWR) |
bool | close () |
Static Public Member Functions | |
static void | initialize () |
static void | destroy () |
static socket | create (int domain, int type, int protocol=0) |
static std::tuple< socket, socket > | pair (int domain, int type, int protocol=0) |
static std::string | error_str (int errNum) |
Protected Member Functions | |
bool | close_on_err () |
void | set_last_error () |
template<typename T > | |
T | check_ret (T ret) const |
template<typename T > | |
bool | check_ret_bool (T ret) const |
socket_t | check_socket (socket_t ret) const |
bool | check_socket_bool (socket_t ret) const |
Static Protected Member Functions | |
static int | get_last_error () |
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.
|
inline |
Creates an unconnected (invalid) socket
|
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. |
|
inlinevirtual |
Destructor closes the socket.
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.
|
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 |
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 |
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.
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. |
TODO: Consider a generic function: bool set_flag(int flag, bool on=true); Used like: set_flag(O_NONBLOCK, on);
|
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::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. |
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:
|