Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
CSocket.hpp
Go to the documentation of this file.
1#ifndef LIBS_COMM_CSOCKET_HPP_
2#define LIBS_COMM_CSOCKET_HPP_
3
4#include <chrono>
5#include <tuple>
6
7#include "CSocketAddress.hpp"
8#include "ENetPort.hpp"
9
19{
20public:
25 CSocket();
26
31 explicit CSocket(socket_t handle);
32
37 CSocket(const CSocket &other) = delete;
38
43 CSocket(CSocket &&other) noexcept;
44
48 virtual ~CSocket();
49
55 CSocket &operator=(const CSocket &other) = delete;
56
62 CSocket &operator=(CSocket &&other) noexcept;
63
68 bool operator!() const;
69
74 explicit operator bool() const;
75
81 virtual bool bind(const CSocketAddress &address);
82 bool bind(const sockaddr *address, socklen_t len) const;
83
88 bool isOpen() const;
89
97
102 void reset(socket_t handle = INVALID_SOCKET);
103
109 CSocket clone() const;
110
115 bool close();
116
117 static CSocket create(int32_t domain, int32_t type, int32_t protocol = 0);
118 static bool close(socket_t h);
119 static std::tuple<CSocket, CSocket> pair(int32_t domain, int32_t type, int32_t protocol = 0);
120
121 virtual CSocketAddressAny getAddress() const;
122 virtual CSocketAddressAny getPeerAddress() const;
123
124 [[maybe_unused]] virtual sa_family_t getFamily() const;
125
126 socket_t getHandle() const;
127
128 void setLastError(int32_t err = INT32_MAX) const;
129 std::string getLastErrorString() const;
130 void clearLastError(int32_t value = 0) const;
131
132 bool getOption(int32_t level, int32_t optName, void *optVal, socklen_t *optLength) const;
133
134 template<typename T>
135 bool getOption(const int32_t level, const int32_t optName, T *value) const
136 {
137 socklen_t len = sizeof(T);
138 return getOption(level, optName, reinterpret_cast<void *>(value), &len);
139 }
140
141 bool setOption(int32_t level, int32_t optName, const void *optVal, socklen_t optLength) const;
142
143 template<typename T>
144 bool setOption(const int32_t level, const int32_t optName, const T &value)
145 {
146 return setOption(level, optName, reinterpret_cast<const void *>(&value), sizeof(T));
147 }
148
149 bool setNonBlocking(bool on = true) const;
150
151protected:
158 static int32_t getErrno();
159
160 template<typename T>
161 T checkRet(T ret) const
162 {
163 m_lastError = (ret == -1) ? getErrno() : 0;
164
165 return ret;
166 }
167
168 template<typename T>
169 bool checkRetBool(T ret) const
170 {
171 m_lastError = (ret == -1) ? getErrno() : 0;
172
173 return ret >= 0;
174 }
175
177 {
178 m_lastError = (ret == INVALID_SOCKET) ? getErrno() : 0;
179
180 return ret;
181 }
182
183 bool checkSocketBool(socket_t ret) const
184 {
185 m_lastError = (ret == INVALID_SOCKET) ? getErrno() : 0;
186
187 return ret != INVALID_SOCKET;
188 }
189
191 {
192 close(release());
193
194 return false;
195 }
196
198
199private:
200 mutable int32_t m_lastError;
201};
202
203timeval toTimeval(const std::chrono::microseconds &duration);
204
205template<class Rep, class Period>
206timeval toTimeval(const std::chrono::duration<Rep, Period> &duration)
207{
208 return toTimeval(std::chrono::duration_cast<std::chrono::microseconds>(duration));
209}
210
211#endif /* LIBS_COMM_CSOCKET_HPP_ */
timeval toTimeval(const std::chrono::microseconds &duration)
Definition CSocket.cpp:228
const socket_t INVALID_SOCKET
Definition ENetPort.hpp:15
int32_t socket_t
Definition ENetPort.hpp:13
Definition CSocketAddress.hpp:107
Definition CSocketAddress.hpp:91
Clase que representa un socket de comunicación. Se usa como base para la genereación de clases de com...
Definition CSocket.hpp:19
virtual CSocketAddressAny getPeerAddress() const
Definition CSocket.cpp:152
T checkRet(T ret) const
Definition CSocket.hpp:161
bool setOption(int32_t level, int32_t optName, const void *optVal, socklen_t optLength) const
Definition CSocket.cpp:195
virtual ~CSocket()
Destructor. Debe ser virtual para permitir la modificación en las clases derivadas.
Definition CSocket.cpp:20
socket_t checkSocket(socket_t ret) const
Definition CSocket.hpp:176
bool setOption(const int32_t level, const int32_t optName, const T &value)
Definition CSocket.hpp:144
static int32_t getErrno()
Función que devuelve el valor actual de errno.
Definition CSocket.cpp:221
CSocket()
Constructor por defecto. Inicializa el socket con el valor INVALID_SOCKET y el error a 0.
Definition CSocket.cpp:6
bool operator!() const
Operador de negación.
Definition CSocket.cpp:32
socket_t m_handle
Definition CSocket.hpp:197
CSocket(const CSocket &other)=delete
Constructor de copia, eliminado para evitar su uso.
int32_t m_lastError
Definition CSocket.hpp:200
bool closeOnError()
Definition CSocket.hpp:190
CSocket clone() const
Crea un nuevo objeto CSocket con el mismo handle que el actual.
Definition CSocket.cpp:79
socket_t getHandle() const
Definition CSocket.cpp:170
socket_t release()
Pone a -1 el handle interno y devuelve el valor anterior. Al no invalidar el file descriptor del sock...
Definition CSocket.cpp:57
bool checkRetBool(T ret) const
Definition CSocket.hpp:169
virtual sa_family_t getFamily() const
Definition CSocket.cpp:165
void reset(socket_t handle=INVALID_SOCKET)
Asigna un nuevo handle al socket e invalida el anterior.
Definition CSocket.cpp:65
bool close()
Cierra el socket.
Definition CSocket.cpp:85
bool getOption(const int32_t level, const int32_t optName, T *value) const
Definition CSocket.hpp:135
std::string getLastErrorString() const
Definition CSocket.cpp:180
bool getOption(int32_t level, int32_t optName, void *optVal, socklen_t *optLength) const
Definition CSocket.cpp:190
void clearLastError(int32_t value=0) const
Definition CSocket.cpp:185
static std::tuple< CSocket, CSocket > pair(int32_t domain, int32_t type, int32_t protocol=0)
Definition CSocket.cpp:116
static CSocket create(int32_t domain, int32_t type, int32_t protocol=0)
Definition CSocket.cpp:99
virtual bool bind(const CSocketAddress &address)
Asigna una dirección a un socket.
Definition CSocket.cpp:42
bool checkSocketBool(socket_t ret) const
Definition CSocket.hpp:183
bool isOpen() const
Comprueba que el handle del socket sea válido.
Definition CSocket.cpp:52
bool setNonBlocking(bool on=true) const
Definition CSocket.cpp:200
CSocket & operator=(const CSocket &other)=delete
Operador de asignación de copia, eliminado para evitar su uso.
virtual CSocketAddressAny getAddress() const
Definition CSocket.cpp:139
void setLastError(int32_t err=INT32_MAX) const
Definition CSocket.cpp:175