sockpp
Modern C++ socket library wrapper
|
Class to create a client stream connection. More...
#include <connector.h>
Public Member Functions | |
connector () | |
Creates an unconnected connector. | |
connector (const sock_address &addr) | |
Creates the connector and attempts to connect to the specified address. More... | |
connector (connector &&conn) | |
Move constructor. More... | |
connector & | operator= (connector &&rhs) |
Move assignment. More... | |
bool | is_connected () const |
Determines if the socket connected to a remote host. More... | |
bool | connect (const sock_address &addr) |
Attempts to connect to the specified server. More... | |
![]() | |
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... | |
Additional Inherited Members | |
![]() | |
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 constexpr int | COMM_TYPE = SOCK_STREAM |
The socket 'type' for communications semantics. More... | |
![]() | |
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 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... | |
Class to create a client stream connection.
This is a base class for creating active, streaming sockets that initiate connections to a server. It can be used to derive classes that implement TCP on IPv4 or IPv6.
|
inline |
Creates the connector and attempts to connect to the specified address.
addr | The remote server address. |
|
inline |
Move constructor.
Creates a connector by moving the other connector to this one.
conn | Another connector. |
bool sockpp::connector::connect | ( | const sock_address & | addr | ) |
Attempts to connect to the specified server.
If the socket is currently connected, this will close the current connection and open the new one.
addr | The remote server address. |
|
inline |
Determines if the socket connected to a remote host.
Note that this is not a reliable determination if the socket is currently connected, but rather that an initial connection was established.
Move assignment.
rhs | The other connector to move into this one. |