sockpp
Modern C++ socket library wrapper
Public Types | Public Member Functions | List of all members
sockpp::connector_tmpl< STREAM_SOCK, ADDR > Class Template Reference

Class to create a client TCP connection. More...

#include <connector.h>

Inheritance diagram for sockpp::connector_tmpl< STREAM_SOCK, ADDR >:
sockpp::connector sockpp::stream_socket sockpp::socket

Public Types

using stream_sock_t = STREAM_SOCK
 The type of streaming socket from the acceptor. More...
 
using addr_t = ADDR
 The type of address for the connector. More...
 

Public Member Functions

 connector_tmpl ()
 Creates an unconnected connector.
 
 connector_tmpl (const addr_t &addr)
 Creates the connector and attempts to connect to the specified address. More...
 
connector_tmploperator= (connector_tmpl &&rhs)
 Move assignment. More...
 
addr_t address () const
 Gets the local address to which the socket is bound. More...
 
addr_t peer_address () const
 Gets the address of the remote peer, if this socket is connected. More...
 
bool bind (const addr_t &addr)
 Binds the socket to the specified address. More...
 
bool connect (const addr_t &addr)
 Attempts to connects to the specified server. More...
 
- Public Member Functions inherited from sockpp::connector
 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...
 
connectoroperator= (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...
 
- Public Member Functions inherited from sockpp::stream_socket
 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_socketoperator= (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...
 
- Public Member Functions inherited from sockpp::socket
 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...
 
socketoperator= (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 Public Member Functions inherited from sockpp::stream_socket
static stream_socket create (int domain, int protocol=0)
 Creates a socket with the specified communications characterics. More...
 
- Static Public Member Functions inherited from sockpp::socket
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, socketpair (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 inherited from sockpp::stream_socket
static constexpr int COMM_TYPE = SOCK_STREAM
 The socket 'type' for communications semantics. More...
 
- Protected Member Functions inherited from sockpp::socket
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 >
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 inherited from sockpp::stream_socket
static socket_t create_handle (int domain)
 Creates a streaming socket. More...
 
- Static Protected Member Functions inherited from sockpp::socket
static int get_last_error ()
 OS-specific means to retrieve the last error from an operation. More...
 

Detailed Description

template<typename STREAM_SOCK, typename ADDR = typename STREAM_SOCK::addr_t>
class sockpp::connector_tmpl< STREAM_SOCK, ADDR >

Class to create a client TCP connection.

Member Typedef Documentation

◆ addr_t

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
using sockpp::connector_tmpl< STREAM_SOCK, ADDR >::addr_t = ADDR

The type of address for the connector.

◆ stream_sock_t

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
using sockpp::connector_tmpl< STREAM_SOCK, ADDR >::stream_sock_t = STREAM_SOCK

The type of streaming socket from the acceptor.

Constructor & Destructor Documentation

◆ connector_tmpl()

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
sockpp::connector_tmpl< STREAM_SOCK, ADDR >::connector_tmpl ( const addr_t addr)
inline

Creates the connector and attempts to connect to the specified address.

Parameters
addrThe remote server address.

Member Function Documentation

◆ address()

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
addr_t sockpp::connector_tmpl< STREAM_SOCK, ADDR >::address ( ) const
inline

Gets the local address to which the socket is bound.

Returns
The local address to which the socket is bound.
Exceptions
sys_erroron error

◆ bind()

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
bool sockpp::connector_tmpl< STREAM_SOCK, ADDR >::bind ( const addr_t addr)
inline

Binds the socket to the specified address.

This call is optional for a client connector, though it is rarely used.

Parameters
addrThe address to which we get bound.
Returns
true on success, false on error

◆ connect()

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
bool sockpp::connector_tmpl< STREAM_SOCK, ADDR >::connect ( const addr_t addr)
inline

Attempts to connects to the specified server.

If the socket is currently connected, this will close the current connection and open the new one.

Parameters
addrThe remote server address.
Returns
true on success, false on error

◆ operator=()

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
connector_tmpl& sockpp::connector_tmpl< STREAM_SOCK, ADDR >::operator= ( connector_tmpl< STREAM_SOCK, ADDR > &&  rhs)
inline

Move assignment.

Parameters
rhsThe other connector to move into this one.
Returns
A reference to this object.

◆ peer_address()

template<typename STREAM_SOCK , typename ADDR = typename STREAM_SOCK::addr_t>
addr_t sockpp::connector_tmpl< STREAM_SOCK, ADDR >::peer_address ( ) const
inline

Gets the address of the remote peer, if this socket is connected.

Returns
The address of the remote peer, if this socket is connected.
Exceptions
sys_erroron error

The documentation for this class was generated from the following file: