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

Base class for datagram sockets. More...

#include <datagram_socket.h>

Inheritance diagram for sockpp::datagram_socket_tmpl< ADDR >:
sockpp::datagram_socket sockpp::socket

Public Types

using addr_t = ADDR
 The type of address for the socket. More...
 

Public Member Functions

 datagram_socket_tmpl ()
 Creates an unbound datagram socket. More...
 
 datagram_socket_tmpl (socket_t handle)
 Creates a datagram socket from an existing OS socket handle and claims ownership of the handle. More...
 
 datagram_socket_tmpl (const ADDR &addr)
 Creates a UDP socket and binds it to the address. More...
 
 datagram_socket_tmpl (datagram_socket_tmpl &&other)
 Move constructor. More...
 
datagram_socket_tmploperator= (datagram_socket_tmpl &&rhs)
 Move assignment. More...
 
bool bind (const ADDR &addr)
 Binds the socket to the local address. More...
 
bool connect (const ADDR &addr)
 Connects the socket to the remote address. More...
 
ssize_t send_to (const void *buf, size_t n, int flags, const ADDR &addr)
 Sends a message to the socket at the specified address. More...
 
ssize_t send_to (const std::string &s, int flags, const ADDR &addr)
 Sends a string to the socket at the specified address. More...
 
ssize_t send_to (const void *buf, size_t n, const ADDR &addr)
 Sends a message to another socket. More...
 
ssize_t send_to (const std::string &s, const ADDR &addr)
 Sends a string to another socket. More...
 
ssize_t recv_from (void *buf, size_t n, int flags, ADDR *srcAddr)
 Receives a message on the socket. More...
 
ssize_t recv_from (void *buf, size_t n, ADDR *srcAddr=nullptr)
 Receives a message on the socket. More...
 
- Public Member Functions inherited from sockpp::datagram_socket
 datagram_socket ()
 Creates an uninitialized datagram socket.
 
 datagram_socket (socket_t handle)
 Creates a datagram socket from an existing OS socket handle and claims ownership of the handle. More...
 
 datagram_socket (const sock_address &addr)
 Creates a UDP socket and binds it to the address. More...
 
 datagram_socket (datagram_socket &&other)
 Move constructor. More...
 
datagram_socketoperator= (datagram_socket &&rhs)
 Move assignment. More...
 
bool connect (const sock_address &addr)
 Connects the socket to the remote address. More...
 
ssize_t send_to (const void *buf, size_t n, int flags, const sock_address &addr)
 Sends a message to the socket at the specified address. More...
 
ssize_t send_to (const std::string &s, int flags, const sock_address &addr)
 Sends a string to the socket at the specified address. More...
 
ssize_t send_to (const void *buf, size_t n, const sock_address &addr)
 Sends a message to another socket. More...
 
ssize_t send_to (const std::string &s, const sock_address &addr)
 Sends a string to another socket. More...
 
ssize_t send (const void *buf, size_t n, int flags=0)
 Sends a message to the socket at the default address. More...
 
ssize_t send (const std::string &s, int flags=0)
 Sends a string to the socket at the default address. More...
 
ssize_t recv_from (void *buf, size_t n, int flags, sock_address *srcAddr=nullptr)
 Receives a message on the socket. More...
 
ssize_t recv_from (void *buf, size_t n, sock_address *srcAddr=nullptr)
 Receives a message on the socket. More...
 
ssize_t recv (void *buf, size_t n, int flags=0)
 Receives a message on the socket. 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< datagram_socket_tmpl, datagram_socket_tmplpair (int protocol=0)
 Creates a pair of connected stream sockets. 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::datagram_socket
static constexpr int COMM_TYPE = SOCK_DGRAM
 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::datagram_socket
static socket_t create_handle (int domain)
 
- 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::datagram_socket_tmpl< ADDR >

Base class for datagram sockets.

Datagram sockets are normally connectionless, where each packet is individually routed and delivered.

Member Typedef Documentation

◆ addr_t

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

The type of address for the socket.

Constructor & Destructor Documentation

◆ datagram_socket_tmpl() [1/4]

template<typename ADDR >
sockpp::datagram_socket_tmpl< ADDR >::datagram_socket_tmpl ( )
inline

Creates an unbound datagram socket.

This can be used as a client or later bound as a server socket.

◆ datagram_socket_tmpl() [2/4]

template<typename ADDR >
sockpp::datagram_socket_tmpl< ADDR >::datagram_socket_tmpl ( socket_t  handle)
inline

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

Parameters
handleA socket handle from the operating system.

◆ datagram_socket_tmpl() [3/4]

template<typename ADDR >
sockpp::datagram_socket_tmpl< ADDR >::datagram_socket_tmpl ( const ADDR &  addr)
inline

Creates a UDP socket and binds it to the address.

Parameters
addrThe address to bind.

◆ datagram_socket_tmpl() [4/4]

template<typename ADDR >
sockpp::datagram_socket_tmpl< ADDR >::datagram_socket_tmpl ( datagram_socket_tmpl< ADDR > &&  other)
inline

Move constructor.

Parameters
otherThe other socket to move to this one

Member Function Documentation

◆ bind()

template<typename ADDR >
bool sockpp::datagram_socket_tmpl< ADDR >::bind ( const ADDR &  addr)
inline

Binds the socket to the local address.

Datagram sockets can bind to a local address/adapter to filter which incoming packets to receive.

Parameters
addrThe address on which to bind.
Returns
true on success, false on failure

◆ connect()

template<typename ADDR >
bool sockpp::datagram_socket_tmpl< ADDR >::connect ( const ADDR &  addr)
inline

Connects the socket to the remote address.

In the case of datagram sockets, this does not create an actual connection, but rather specifies the address to which datagrams are sent by default and the only address from which packets are received.

Parameters
addrThe address on which to "connect".
Returns
true on success, false on failure

◆ operator=()

template<typename ADDR >
datagram_socket_tmpl& sockpp::datagram_socket_tmpl< ADDR >::operator= ( datagram_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<datagram_socket_tmpl, datagram_socket_tmpl> sockpp::datagram_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.

◆ recv_from() [1/2]

template<typename ADDR >
ssize_t sockpp::datagram_socket_tmpl< ADDR >::recv_from ( void *  buf,
size_t  n,
int  flags,
ADDR *  srcAddr 
)
inline

Receives a message on the socket.

Parameters
bufBuffer to get the incoming data.
nThe number of bytes to read.
flagsThe option bit flags. See send(2).
srcAddrReceives the address of the peer that sent the message
Returns
The number of bytes read or -1 on error.

◆ recv_from() [2/2]

template<typename ADDR >
ssize_t sockpp::datagram_socket_tmpl< ADDR >::recv_from ( void *  buf,
size_t  n,
ADDR *  srcAddr = nullptr 
)
inline

Receives a message on the socket.

Parameters
bufBuffer to get the incoming data.
nThe number of bytes to read.
srcAddrReceives the address of the peer that sent the message
Returns
The number of bytes read or -1 on error.

◆ send_to() [1/4]

template<typename ADDR >
ssize_t sockpp::datagram_socket_tmpl< ADDR >::send_to ( const void *  buf,
size_t  n,
int  flags,
const ADDR &  addr 
)
inline

Sends a message to the socket at the specified address.

Parameters
bufThe data to send.
nThe number of bytes in the data buffer.
flagsThe option bit flags. See send(2).
addrThe remote destination of the data.
Returns
the number of bytes sent on success or, -1 on failure.

◆ send_to() [2/4]

template<typename ADDR >
ssize_t sockpp::datagram_socket_tmpl< ADDR >::send_to ( const std::string &  s,
int  flags,
const ADDR &  addr 
)
inline

Sends a string to the socket at the specified address.

Parameters
sThe string to send.
flagsThe flags. See send(2).
addrThe remote destination of the data.
Returns
the number of bytes sent on success or, -1 on failure.

◆ send_to() [3/4]

template<typename ADDR >
ssize_t sockpp::datagram_socket_tmpl< ADDR >::send_to ( const void *  buf,
size_t  n,
const ADDR &  addr 
)
inline

Sends a message to another socket.

Parameters
bufThe data to send.
nThe number of bytes in the data buffer.
addrThe remote destination of the data.
Returns
the number of bytes sent on success or, -1 on failure.

◆ send_to() [4/4]

template<typename ADDR >
ssize_t sockpp::datagram_socket_tmpl< ADDR >::send_to ( const std::string &  s,
const ADDR &  addr 
)
inline

Sends a string to another socket.

Parameters
sThe string to send.
addrThe remote destination of the data.
Returns
the number of bytes sent on success or, -1 on failure.

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