47 #ifndef __sockpp_datagram_socket_h 48 #define __sockpp_datagram_socket_h 72 static socket_t create_handle(
int domain) {
100 :
base(std::move(other)) {}
107 base::operator=(std::move(rhs));
136 return check_ret(::sendto(
handle(), reinterpret_cast<const char*>(buf),
int(n),
151 return send_to(s.data(), s.length(), flags, addr);
161 return send_to(buf, n, 0, addr);
170 return send_to(s.data(), s.length(), 0, addr);
180 ssize_t
send(
const void* buf,
size_t n,
int flags=0) {
195 ssize_t
send(
const std::string& s,
int flags=0) {
196 return send(s.data(), s.length(), flags);
226 ssize_t
recv(
void* buf,
size_t n,
int flags=0) {
244 template <
typename ADDR>
252 static constexpr sa_family_t ADDRESS_FAMILY = ADDR::ADDRESS_FAMILY;
277 :
base(std::move(other)) {}
284 base::operator=(std::move(rhs));
300 static std::tuple<datagram_socket_tmpl, datagram_socket_tmpl>
pair(
int protocol=0) {
302 return std::make_tuple<datagram_socket_tmpl, datagram_socket_tmpl>(
323 bool connect(
const ADDR& addr) {
return base::connect(addr); }
335 ssize_t
send_to(
const void* buf,
size_t n,
int flags,
const ADDR& addr) {
336 return base::send_to(buf, n, flags, addr);
345 ssize_t
send_to(
const std::string& s,
int flags,
const ADDR& addr) {
346 return base::send_to(s, flags, addr);
355 ssize_t
send_to(
const void* buf,
size_t n,
const ADDR& addr) {
356 return base::send_to(buf, n, 0, addr);
364 ssize_t
send_to(
const std::string& s,
const ADDR& addr) {
365 return base::send_to(s, addr);
376 ssize_t
recv_from(
void* buf,
size_t n,
int flags, ADDR* srcAddr) {
377 return base::recv_from(buf, n, flags, srcAddr);
387 ssize_t
recv_from(
void* buf,
size_t n, ADDR* srcAddr=
nullptr) {
388 return base::recv_from(buf, n, srcAddr);
396 #endif // __sockpp_datagram_socket_h Generic socket address.
Definition: sock_address.h:64
static std::tuple< socket, socket > pair(int domain, int type, int protocol=0)
Creates a pair of connected sockets.
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.
Definition: datagram_socket.h:335
ssize_t recv_from(void *buf, size_t n, int flags, sock_address *srcAddr=nullptr)
Receives a message on the socket.
T check_ret(T ret) const
Checks the value and if less than zero, sets last error.
Definition: socket.h:137
datagram_socket_tmpl(datagram_socket_tmpl &&other)
Move constructor.
Definition: datagram_socket.h:276
ssize_t send(const void *buf, size_t n, int flags=0)
Sends a message to the socket at the default address.
Definition: datagram_socket.h:180
datagram_socket_tmpl(socket_t handle)
Creates a datagram socket from an existing OS socket handle and claims ownership of the handle...
Definition: datagram_socket.h:266
virtual sockaddr * sockaddr_ptr()=0
Gets a pointer to this object cast to a sockaddr.
ssize_t send_to(const std::string &s, const ADDR &addr)
Sends a string to another socket.
Definition: datagram_socket.h:364
ssize_t send_to(const void *buf, size_t n, const sock_address &addr)
Sends a message to another socket.
Definition: datagram_socket.h:160
bool connect(const ADDR &addr)
Connects the socket to the remote address.
Definition: datagram_socket.h:323
datagram_socket_tmpl(const ADDR &addr)
Creates a UDP socket and binds it to the address.
Definition: datagram_socket.h:271
Base class for datagram sockets.
Definition: datagram_socket.h:245
ssize_t send_to(const std::string &s, int flags, const ADDR &addr)
Sends a string to the socket at the specified address.
Definition: datagram_socket.h:345
ssize_t recv(void *buf, size_t n, int flags=0)
Receives a message on the socket.
Definition: datagram_socket.h:226
Base class for datagram sockets.
Definition: datagram_socket.h:62
ssize_t recv_from(void *buf, size_t n, ADDR *srcAddr=nullptr)
Receives a message on the socket.
Definition: datagram_socket.h:387
datagram_socket(socket_t handle)
Creates a datagram socket from an existing OS socket handle and claims ownership of the handle...
Definition: datagram_socket.h:89
ssize_t send(const std::string &s, int flags=0)
Sends a string to the socket at the default address.
Definition: datagram_socket.h:195
datagram_socket_tmpl & operator=(datagram_socket_tmpl &&rhs)
Move assignment.
Definition: datagram_socket.h:283
bool bind(const sock_address &addr)
Binds the socket to the specified address.
ssize_t recv_from(void *buf, size_t n, int flags, ADDR *srcAddr)
Receives a message on the socket.
Definition: datagram_socket.h:376
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.
Definition: datagram_socket.h:134
bool check_ret_bool(T ret) const
Checks the value and if less than zero, sets last error.
Definition: socket.h:149
datagram_socket_tmpl()
Creates an unbound datagram socket.
Definition: datagram_socket.h:260
datagram_socket(datagram_socket &&other)
Move constructor.
Definition: datagram_socket.h:99
static std::tuple< datagram_socket_tmpl, datagram_socket_tmpl > pair(int protocol=0)
Creates a pair of connected stream sockets.
Definition: datagram_socket.h:300
ADDR addr_t
The type of address for the socket.
Definition: datagram_socket.h:254
socket()
Creates an unconnected (invalid) socket.
Definition: socket.h:183
Definition: acceptor.h:51
ssize_t recv_from(void *buf, size_t n, sock_address *srcAddr=nullptr)
Receives a message on the socket.
Definition: datagram_socket.h:216
Base class for socket objects.
Definition: socket.h:84
virtual socklen_t size() const =0
Gets the size of this structure.
int socket_t
The OS socket handle.
Definition: socket.h:60
bool connect(const sock_address &addr)
Connects the socket to the remote address.
Definition: datagram_socket.h:119
socket_t release()
Releases ownership of the underlying socket object.
Definition: socket.h:311
ssize_t send_to(const std::string &s, int flags, const sock_address &addr)
Sends a string to the socket at the specified address.
Definition: datagram_socket.h:150
ssize_t send_to(const std::string &s, const sock_address &addr)
Sends a string to another socket.
Definition: datagram_socket.h:169
ssize_t send_to(const void *buf, size_t n, const ADDR &addr)
Sends a message to another socket.
Definition: datagram_socket.h:355
datagram_socket & operator=(datagram_socket &&rhs)
Move assignment.
Definition: datagram_socket.h:106
Classes for TCP & UDP socket.
socket_t handle() const
Get the underlying OS socket handle.
Definition: socket.h:259
bool bind(const ADDR &addr)
Binds the socket to the local address.
Definition: datagram_socket.h:313
static constexpr int COMM_TYPE
The socket 'type' for communications semantics.
Definition: datagram_socket.h:78
datagram_socket()
Creates an uninitialized datagram socket.
Definition: datagram_socket.h:83