sockpp
Modern C++ socket library wrapper
|
Base class for streaming sockets, such as TCP and Unix Domain. More...
#include <stream_socket.h>
Public Member Functions | |
stream_socket () | |
Creates an unconnected streaming socket. | |
stream_socket (socket_t handle) | |
Creates a streaming socket from an existing OS socket handle and claims ownership of the handle. More... | |
stream_socket (stream_socket &&sock) | |
Creates a stream socket by copying the socket handle from the specified socket object and transfers ownership of the socket. | |
stream_socket & | operator= (stream_socket &&rhs) |
Move assignment. More... | |
stream_socket | clone () const |
Creates a new stream_socket that refers to this one. More... | |
virtual ssize_t | read (void *buf, size_t n) |
Reads from the port. More... | |
virtual ssize_t | read_n (void *buf, size_t n) |
Best effort attempts to read the specified number of bytes. More... | |
ssize_t | read (const std::vector< iovec > &ranges) |
Reads discontiguous memory ranges from the socket. More... | |
virtual bool | read_timeout (const std::chrono::microseconds &to) |
Set a timeout for read operations. More... | |
template<class Rep , class Period > | |
bool | read_timeout (const std::chrono::duration< Rep, Period > &to) |
Set a timeout for read operations. More... | |
virtual ssize_t | write (const void *buf, size_t n) |
Writes the buffer to the socket. More... | |
virtual ssize_t | write_n (const void *buf, size_t n) |
Best effort attempt to write the whole buffer to the socket. More... | |
virtual ssize_t | write (const std::string &s) |
Best effort attempt to write a string to the socket. More... | |
virtual ssize_t | write (const std::vector< iovec > &ranges) |
Writes discontiguous memory ranges to the socket. More... | |
virtual bool | write_timeout (const std::chrono::microseconds &to) |
Set a timeout for write operations. More... | |
template<class Rep , class Period > | |
bool | write_timeout (const std::chrono::duration< Rep, Period > &to) |
Set a timeout for write operations. More... | |
![]() | |
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 stream_socket | create (int domain, int protocol=0) |
Creates a socket with the specified communications characterics. More... | |
![]() | |
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... | |
Static Public Attributes | |
static constexpr int | COMM_TYPE = SOCK_STREAM |
The socket 'type' for communications semantics. More... | |
Static Protected Member Functions | |
static socket_t | create_handle (int domain) |
Creates a streaming socket. More... | |
![]() | |
static int | get_last_error () |
OS-specific means to retrieve the last error from an operation. More... | |
Friends | |
class | acceptor |
Acceptor can create stream sockets. More... | |
Additional Inherited Members | |
![]() | |
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... | |
Base class for streaming sockets, such as TCP and Unix Domain.
This is the streaming connection between two peers. It looks like a readable/writeable device.
|
inlineexplicit |
Creates a streaming socket from an existing OS socket handle and claims ownership of the handle.
handle | A socket handle from the operating system. |
|
inline |
Creates a new stream_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.
|
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) |
protocol | The particular protocol to be used with the sockets |
|
inlinestaticprotected |
Creates a streaming socket.
|
inline |
Move assignment.
rhs | The other socket to move into this one. |
|
virtual |
Reads from the port.
buf | Buffer to get the incoming data. |
n | The number of bytes to try to read. |
ssize_t sockpp::stream_socket::read | ( | const std::vector< iovec > & | ranges | ) |
Reads discontiguous memory ranges from the socket.
ranges | The vector of memory ranges to fill |
|
virtual |
Best effort attempts to read the specified number of bytes.
This will make repeated read attempts until all the bytes are read in or until an error occurs.
buf | Buffer to get the incoming data. |
n | The number of bytes to try to read. |
|
virtual |
Set a timeout for read operations.
Sets the timeout that the device uses for read operations. Not all devices support timeouts, so the caller should prepare for failure.
to | The amount of time to wait for the operation to complete. |
|
inline |
Set a timeout for read operations.
Sets the timout that the device uses for read operations. Not all devices support timouts, so the caller should prepare for failure.
to | The amount of time to wait for the operation to complete. |
|
virtual |
Writes the buffer to the socket.
buf | The buffer to write |
n | The number of bytes in the buffer. |
|
inlinevirtual |
Best effort attempt to write a string to the socket.
s | The string to write. |
|
virtual |
Writes discontiguous memory ranges to the socket.
ranges | The vector of memory ranges to write |
|
virtual |
Best effort attempt to write the whole buffer to the socket.
buf | The buffer to write |
n | The number of bytes in the buffer. |
|
virtual |
Set a timeout for write operations.
Sets the timout that the device uses for write operations. Not all devices support timouts, so the caller should prepare for failure.
to | The amount of time to wait for the operation to complete. |
|
inline |
Set a timeout for write operations.
Sets the timout that the device uses for write operations. Not all devices support timouts, so the caller should prepare for failure.
to | The amount of time to wait for the operation to complete. |
|
friend |
Acceptor can create stream sockets.
|
static |
The socket 'type' for communications semantics.