sockpp
Modern C++ socket library wrapper
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
sockpp::stream_socket_tmpl< ADDR > Class Template Reference

Template for creating specific stream types (IPv4, IPv6, etc). More...

#include <stream_socket.h>

Inheritance diagram for sockpp::stream_socket_tmpl< ADDR >:
sockpp::stream_socket sockpp::socket

Public Types

using addr_t = ADDR
 The type of network address used with this socket. More...
 

Public Member Functions

 stream_socket_tmpl ()
 Creates an unconnected streaming socket.
 
 stream_socket_tmpl (socket_t handle)
 Creates a streaming socket from an existing OS socket handle and claims ownership of the handle. More...
 
 stream_socket_tmpl (stream_socket &&sock)
 Move constructor. More...
 
 stream_socket_tmpl (stream_socket_tmpl &&sock)
 Creates a stream socket by copying the socket handle from the specified socket object and transfers ownership of the socket.
 
stream_socket_tmploperator= (stream_socket_tmpl &&rhs)
 Move assignment. More...
 
stream_socket_tmpl create (int protocol=0)
 Cretates a stream socket. 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...
 
- 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...
 

Static Public Member Functions

static std::tuple< stream_socket_tmpl, stream_socket_tmplpair (int protocol=0)
 Creates a pair of connected stream sockets. More...
 
- 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

static constexpr sa_family_t ADDRESS_FAMILY = ADDR::ADDRESS_FAMILY
 The address family for this type of address.
 
- Static Public Attributes inherited from sockpp::stream_socket
static constexpr int COMM_TYPE = SOCK_STREAM
 The socket 'type' for communications semantics. More...
 

Additional Inherited Members

- 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 ADDR>
class sockpp::stream_socket_tmpl< ADDR >

Template for creating specific stream types (IPv4, IPv6, etc).

This just overrides methods that take a generic address and replace them with the address type for a specific family. This doesn't add any runtime functionality, but has compile-time checks that address types aren't accidentally being mixed for an object.

Member Typedef Documentation

◆ addr_t

template<typename ADDR >
using sockpp::stream_socket_tmpl< ADDR >::addr_t = ADDR

The type of network address used with this socket.

Constructor & Destructor Documentation

◆ stream_socket_tmpl() [1/2]

template<typename ADDR >
sockpp::stream_socket_tmpl< ADDR >::stream_socket_tmpl ( socket_t  handle)
inlineexplicit

Creates a streaming socket from an existing OS socket handle and claims ownership of the handle.

Parameters
handleA socket handle from the operating system.

◆ stream_socket_tmpl() [2/2]

template<typename ADDR >
sockpp::stream_socket_tmpl< ADDR >::stream_socket_tmpl ( stream_socket &&  sock)
inline

Move constructor.

Creates a stream socket by moving the other socket to this one.

Parameters
sockAnother stream socket.

Member Function Documentation

◆ address()

template<typename ADDR >
addr_t sockpp::stream_socket_tmpl< 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

◆ create()

template<typename ADDR >
stream_socket_tmpl sockpp::stream_socket_tmpl< ADDR >::create ( int  protocol = 0)
inline

Cretates a stream socket.

Parameters
protocolThe particular protocol to be used with the sockets
Returns
A stream socket

◆ operator=()

template<typename ADDR >
stream_socket_tmpl& sockpp::stream_socket_tmpl< ADDR >::operator= ( stream_socket_tmpl< ADDR > &&  rhs)
inline

Move assignment.

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

◆ pair()

template<typename ADDR >
static std::tuple<stream_socket_tmpl, stream_socket_tmpl> sockpp::stream_socket_tmpl< ADDR >::pair ( int  protocol = 0)
inlinestatic

Creates a pair of connected stream 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.

Parameters
protocolThe protocol to be used with the socket. (Normally 0)
Returns
A std::tuple of stream sockets. On error both sockets will be in an error state with the last error set.

◆ peer_address()

template<typename ADDR >
addr_t sockpp::stream_socket_tmpl< 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: