LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
libwire::udp::socket Class Reference

Descriptor wrapper for UDP socket. More...

#include <socket.hpp>

Public Member Functions

 socket (ip ipver) noexcept
 Create new socket object. More...
 
 socket (const socket &)=delete
 
 socket (socket &&) noexcept=default
 
 ~socket ()
 Deallocate socket. More...
 
void associate (endpoint target, std::error_code &ec) noexcept
 Associate UDP socket with remote endpoint. More...
 
void close () noexcept
 Deallocate socket. More...
 
void disassociate () noexcept
 Undo previous call to associate(). More...
 
internal_::socket & implementation () noexcept
 
const internal_::socket & implementation () const noexcept
 
void listen (endpoint target, std::error_code &ec) noexcept
 Accept datagrams coming on specified endpoint. More...
 
internal_::socket::native_handle_t native_handle () const noexcept
 Get native handle/descriptor for socket. More...
 
socketoperator= (const socket &)=delete
 
socketoperator= (socket &&) noexcept=default
 
Socket options

Several aspects of socket behavior can be changes by setting flags.

See tcp::socket documentation for detailed explanation of socket options mechanism. There are no special options for UDP sockets.

template<typename Option >
auto option (const Option &) const noexcept
 Query socket option value specified by type tag Option. More...
 
template<typename Option , typename... Args>
void set_option (const Option &, Args &&...args) noexcept
 Set socket option value specified by type tag Option to value value. More...
 
Blocking I/O

I/O functions in this category usually block thread until operation is completed.

template<typename Buffer = std::vector<uint8_t>>
Buffer & read (size_t bytes_count, Buffer &, std::error_code &, endpoint *source=nullptr) noexcept
 Read pending datagram into buffer. More...
 
template<typename Buffer = std::vector<uint8_t>>
Buffer read (size_t bytes_count, std::error_code &, endpoint *source=nullptr) noexcept
 Same as overload with Buffer argument but return newly allocated buffer every time. More...
 
template<typename Buffer = std::vector<uint8_t>>
size_t write (const Buffer &, std::error_code &, const endpoint &dest=endpoint::invalid) noexcept
 Write contents of buffer to socket. More...
 

Detailed Description

Descriptor wrapper for UDP socket.

UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability of TCP isn't important.

Thread-safety

Definition at line 56 of file socket.hpp.

Constructor & Destructor Documentation

libwire::udp::socket::socket ( ip  ipver)
noexcept

Create new socket object.

If socket allocationf ailed we will set socket to closed state.

Note
As opposed to TCP you should specify IP protocol version here because we can't guess it from future connect() or bind() call because there may be none. And we should not (re-)allocate socket in I/O operations to prevent strange performance problems when you are jumping between IPv6 and IPv4 too often.
libwire::udp::socket::socket ( const socket )
delete
libwire::udp::socket::socket ( socket &&  )
defaultnoexcept
libwire::udp::socket::~socket ( )

Deallocate socket.

Member Function Documentation

void libwire::udp::socket::associate ( endpoint  target,
std::error_code &  ec 
)
noexcept

Associate UDP socket with remote endpoint.

This will allow you to omit target endpoint from further calls to write(). Also, socket will not receive datagrams sent from endpoints other than specified.

ec will be set to error::invalid_argument if passed address object have IP version different from one passed to socket constructor.

void libwire::udp::socket::close ( )
noexcept

Deallocate socket.

This makes this instance unusable.

void libwire::udp::socket::disassociate ( )
noexcept

Undo previous call to associate().

Have no effect if associate() is not used.

internal_::socket& libwire::udp::socket::implementation ( )
noexcept
const internal_::socket& libwire::udp::socket::implementation ( ) const
noexcept
void libwire::udp::socket::listen ( endpoint  target,
std::error_code &  ec 
)
noexcept

Accept datagrams coming on specified endpoint.

ec will be set if something went wrong.

internal_::socket::native_handle_t libwire::udp::socket::native_handle ( ) const
noexcept

Get native handle/descriptor for socket.

Returned value is undefined if is_open returns false.

socket& libwire::udp::socket::operator= ( const socket )
delete
socket& libwire::udp::socket::operator= ( socket &&  )
defaultnoexcept
template<typename Option >
auto libwire::udp::socket::option ( const Option &  ) const
inlinenoexcept

Query socket option value specified by type tag Option.

Example:

socket.option(tcp::no_delay); // => false by default.
// Some options May have multiple values returned in tuple.
auto [ enabled, timeout ] = socket.option(tcp::linger);

Definition at line 119 of file socket.hpp.

template<typename Buffer >
Buffer & libwire::udp::socket::read ( size_t  bytes_count,
Buffer &  output,
std::error_code &  ec,
endpoint source = nullptr 
)
noexcept

Read pending datagram into buffer.

If buffer is not large enough to fit entire datagram it will be truncated. If buffer is smaller than datagram it will be resized to datagram size.

If src contains non-null pointer, datagram source endpoint will be written to it.

Buffer type requirements:

Buffer must be container that encapsulates dynamic array, so it must have data, size and resize member functions with behavior as in std::vector.

Definition at line 256 of file socket.hpp.

template<typename Buffer >
Buffer libwire::udp::socket::read ( size_t  bytes_count,
std::error_code &  ec,
endpoint source = nullptr 
)
noexcept

Same as overload with Buffer argument but return newly allocated buffer every time.

Definition at line 274 of file socket.hpp.

template<typename Option , typename... Args>
void libwire::udp::socket::set_option ( const Option &  ,
Args &&...  args 
)
inlinenoexcept

Set socket option value specified by type tag Option to value value.

Setting option not supported on current platform or specifying invalid value results in undefined behavior. But usually new value just ignored and corresponding option(tag) will return old value.

Example:

socket.set_option(tcp::no_delay, true);
// Some options may have multiple values:
socket.set_option(tcp::linger, true, 20s);
// ^ enable linger with 20 seconds timeout.

Definition at line 141 of file socket.hpp.

template<typename Buffer >
size_t libwire::udp::socket::write ( const Buffer &  input,
std::error_code &  ec,
const endpoint dest = endpoint::invalid 
)
noexcept

Write contents of buffer to socket.

Error code will be set if anything went wrong.

dest argument will override destination specified using associate.

Buffer type requirements

Buffer must be container that encapsulates dynamic array, so it must have data and size member functions with behavior as in std::vector.

Definition at line 287 of file socket.hpp.