Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
CDatagramSocket.hpp
Go to the documentation of this file.
1#ifndef LIBS_COMM_CDATAGRAMSOCKET_HPP_
2#define LIBS_COMM_CDATAGRAMSOCKET_HPP_
3
5#include "core/assert.hpp"
6
7class CDatagramSocket : public CSocket
8{
9public:
10 static constexpr int32_t COMM_TYPE = SOCK_DGRAM;
11
13 {
14 }
15
16 explicit CDatagramSocket(socket_t handle) : CSocket(handle)
17 {
18 }
19
20 explicit CDatagramSocket(const CSocketAddress &address);
21
22 CDatagramSocket(CDatagramSocket &&other) : CSocket(std::move(other))
23 {
24 }
25
27 {
28 }
29
31 {
32 CSocket::operator=(std::move(rhs));
33
34 return *this;
35 }
36
37 virtual bool connect(const CSocketAddress &address)
38 {
39 return checkRetBool(::connect(getHandle(), address.getSocketAddress(), address.size()));
40 }
41
42 ssize_t sendTo(const void *buf, size_t n, int32_t flags, const CSocketAddress &address)
43 {
45 return checkRet(::sendto(getHandle(), buf, n, flags, address.getSocketAddress(), address.size()));
46 }
47
48 ssize_t sendTo(const void *buf, size_t n, const CSocketAddress &address)
49 {
50 return sendTo(buf, n, 0, address);
51 }
52
53 ssize_t sendTo(const std::string &s, int32_t flags, const CSocketAddress &address)
54 {
55 return sendTo(s.data(), s.length(), flags, address);
56 }
57
58 ssize_t sendTo(const std::string &s, const CSocketAddress &address)
59 {
60 return sendTo(s.data(), s.length(), 0, address);
61 }
62
63 ssize_t send(const void *buf, size_t n, int32_t flags = 0)
64 {
65 return checkRet(::send(getHandle(), buf, n, flags));
66 }
67
68 ssize_t send(const std::string &s, int32_t flags = 0)
69 {
70 return send(s.data(), s.length(), flags);
71 }
72
73 ssize_t recvFrom(void *buf, size_t n, int32_t flags, CSocketAddress *srcAddress = nullptr);
74
75 ssize_t recvFrom(void *buf, size_t n, CSocketAddress *srcAddress = nullptr)
76 {
77 return recvFrom(buf, n, 0, srcAddress);
78 }
79
80 ssize_t recv(void *buf, size_t n, int32_t flags = 0)
81 {
82 return checkRet(::recv(getHandle(), buf, n, flags));
83 }
84
85protected:
86 static socket_t createHandle(int32_t domain)
87 {
88 return socket_t(::socket(domain, COMM_TYPE, 0));
89 }
90
91private:
92 CDatagramSocket(const CDatagramSocket &other) = delete;
93 CDatagramSocket &operator=(const CDatagramSocket &other) = delete;
94};
95
96//---------------------------------------------------------------
97
98template<typename ADDR>
100{
101public:
102 static constexpr sa_family_t ADDRESS_FAMILY = ADDR::ADDRESS_FAMILY;
103
104 using addr_t = ADDR;
105
109
111 {
112 }
113
114 explicit CDatagramSocketTmpl(const ADDR &address) : CDatagramSocket(address)
115 {
116 }
117
119 {
120 }
121
123 {
124 CDatagramSocket::operator=(std::move(rhs));
125
126 return *this;
127 }
128
129 static std::tuple<CDatagramSocketTmpl, CDatagramSocketTmpl> pair(int32_t protocol = 0)
130 {
131 auto pr = CDatagramSocket::pair(addr_t::ADDRESS_FAMILY, COMM_TYPE, protocol);
132
133 return std::make_tuple<CDatagramSocketTmpl, CDatagramSocketTmpl>(
134 CDatagramSocketTmpl {std::get<0>(pr).release()}, CDatagramSocketTmpl {std::get<1>(pr).release()});
135 }
136
137 ssize_t sendTo(const void *buf, size_t n, int32_t flags, const ADDR &address)
138 {
139 return CDatagramSocket::sendTo(buf, n, flags, address);
140 }
141
142 ssize_t sendTo(const std::string &s, int32_t flags, const ADDR &address)
143 {
144 return CDatagramSocket::sendTo(s, flags, address);
145 }
146
147 ssize_t sendTo(const void *buf, size_t n, const ADDR &address)
148 {
149 return CDatagramSocket::sendTo(buf, n, 0, address);
150 }
151
152 ssize_t sendTo(const std::string &s, const ADDR &address)
153 {
154 return CDatagramSocket::sendTo(s, address);
155 }
156
157 ssize_t recvFrom(void *buf, size_t n, int32_t flags, ADDR *srcAddress)
158 {
159 return CDatagramSocket::recvFrom(buf, n, flags, srcAddress);
160 }
161
162 ssize_t recvFrom(void *buf, size_t n, ADDR *srcAddress = nullptr)
163 {
164 return CDatagramSocket::recvFrom(buf, n, srcAddress);
165 }
166};
167
168#endif /* LIBS_COMM_CDATAGRAMSOCKET_HPP_ */
int32_t socket_t
Definition ENetPort.hpp:13
#define IV_ASSERT_NULL_POINTER(ptr)
Definition assert.hpp:128
Definition CDatagramSocket.hpp:100
CDatagramSocketTmpl(const ADDR &address)
Definition CDatagramSocket.hpp:114
static constexpr sa_family_t ADDRESS_FAMILY
Definition CDatagramSocket.hpp:102
CDatagramSocketTmpl()
Definition CDatagramSocket.hpp:106
ssize_t sendTo(const void *buf, size_t n, const ADDR &address)
Definition CDatagramSocket.hpp:147
ADDR addr_t
Definition CDatagramSocket.hpp:104
ssize_t sendTo(const std::string &s, const ADDR &address)
Definition CDatagramSocket.hpp:152
CDatagramSocketTmpl(socket_t handle)
Definition CDatagramSocket.hpp:110
CDatagramSocketTmpl & operator=(CDatagramSocketTmpl &&rhs)
Definition CDatagramSocket.hpp:122
ssize_t recvFrom(void *buf, size_t n, ADDR *srcAddress=nullptr)
Definition CDatagramSocket.hpp:162
CDatagramSocketTmpl(CDatagramSocketTmpl &&other)
Definition CDatagramSocket.hpp:118
ssize_t sendTo(const std::string &s, int32_t flags, const ADDR &address)
Definition CDatagramSocket.hpp:142
ssize_t recvFrom(void *buf, size_t n, int32_t flags, ADDR *srcAddress)
Definition CDatagramSocket.hpp:157
static std::tuple< CDatagramSocketTmpl, CDatagramSocketTmpl > pair(int32_t protocol=0)
Definition CDatagramSocket.hpp:129
ssize_t sendTo(const void *buf, size_t n, int32_t flags, const ADDR &address)
Definition CDatagramSocket.hpp:137
Definition CDatagramSocket.hpp:8
CDatagramSocket & operator=(const CDatagramSocket &other)=delete
CDatagramSocket(socket_t handle)
Definition CDatagramSocket.hpp:16
ssize_t sendTo(const std::string &s, const CSocketAddress &address)
Definition CDatagramSocket.hpp:58
ssize_t recv(void *buf, size_t n, int32_t flags=0)
Definition CDatagramSocket.hpp:80
virtual ~CDatagramSocket()
Definition CDatagramSocket.hpp:26
static constexpr int32_t COMM_TYPE
Definition CDatagramSocket.hpp:10
CDatagramSocket(CDatagramSocket &&other)
Definition CDatagramSocket.hpp:22
static socket_t createHandle(int32_t domain)
Definition CDatagramSocket.hpp:86
ssize_t sendTo(const void *buf, size_t n, const CSocketAddress &address)
Definition CDatagramSocket.hpp:48
ssize_t sendTo(const void *buf, size_t n, int32_t flags, const CSocketAddress &address)
Definition CDatagramSocket.hpp:42
ssize_t recvFrom(void *buf, size_t n, CSocketAddress *srcAddress=nullptr)
Definition CDatagramSocket.hpp:75
CDatagramSocket & operator=(CDatagramSocket &&rhs)
Definition CDatagramSocket.hpp:30
ssize_t recvFrom(void *buf, size_t n, int32_t flags, CSocketAddress *srcAddress=nullptr)
Definition CDatagramSocket.cpp:19
ssize_t send(const std::string &s, int32_t flags=0)
Definition CDatagramSocket.hpp:68
virtual bool connect(const CSocketAddress &address)
Definition CDatagramSocket.hpp:37
CDatagramSocket()
Definition CDatagramSocket.hpp:12
ssize_t sendTo(const std::string &s, int32_t flags, const CSocketAddress &address)
Definition CDatagramSocket.hpp:53
ssize_t send(const void *buf, size_t n, int32_t flags=0)
Definition CDatagramSocket.hpp:63
CDatagramSocket(const CDatagramSocket &other)=delete
Definition CSocketAddress.hpp:91
virtual sockaddr * getSocketAddress()=0
virtual socklen_t size() const =0
Clase que representa un socket de comunicación. Se usa como base para la genereación de clases de com...
Definition CSocket.hpp:19
T checkRet(T ret) const
Definition CSocket.hpp:161
socket_t getHandle() const
Definition CSocket.cpp:170
bool checkRetBool(T ret) const
Definition CSocket.hpp:169
static std::tuple< CSocket, CSocket > pair(int32_t domain, int32_t type, int32_t protocol=0)
Definition CSocket.cpp:116
CSocket & operator=(const CSocket &other)=delete
Operador de asignación de copia, eliminado para evitar su uso.