44** Author Francois Michaut
55**
66** Started on Sat Jan 15 01:27:40 2022 Francois Michaut
7- ** Last update Sat Jul 22 22:09:20 2023 Francois Michaut
7+ ** Last update Tue Nov 14 20:03:40 2023 Francois Michaut
88**
99** Socket.cpp : Protable C++ socket class implementation
1010*/
@@ -38,29 +38,29 @@ static constexpr int BUFF_SIZE = 4096;
3838// TODO throw custom exceptions on invalid status (eg: socket already connected)
3939namespace CppSockets {
4040 Socket::Socket (RawSocketType sockfd, bool connected) :
41- sockfd (sockfd), is_connected (connected)
41+ m_sockfd (sockfd), m_is_connected (connected)
4242 {
4343 socklen_t len = sizeof (int );
4444
45- Socket::getsockopt (sockfd, SOL_SOCKET, SO_DOMAIN, &domain , &len);
46- Socket::getsockopt (sockfd, SOL_SOCKET, SO_TYPE, &type , &len);
47- Socket::getsockopt (sockfd, SOL_SOCKET, SO_PROTOCOL, &protocol , &len);
45+ Socket::getsockopt (sockfd, SOL_SOCKET, SO_DOMAIN, &m_domain , &len);
46+ Socket::getsockopt (sockfd, SOL_SOCKET, SO_TYPE, &m_type , &len);
47+ Socket::getsockopt (sockfd, SOL_SOCKET, SO_PROTOCOL, &m_protocol , &len);
4848 }
4949
5050 // TODO: more error handling arround is_connected == false and sockfd == INVALID in IO calls
5151 Socket::Socket () :
52- sockfd (INVALID_SOCKET)
52+ m_sockfd (INVALID_SOCKET)
5353 {}
5454
5555 Socket::Socket (int domain, int type, int protocol) :
56- domain (domain), type (type), protocol (protocol), sockfd (::socket(domain, type, protocol))
56+ m_domain (domain), m_type (type), m_protocol (protocol), m_sockfd (::socket(domain, type, protocol))
5757 {
58- if (sockfd == INVALID_SOCKET)
58+ if (m_sockfd == INVALID_SOCKET)
5959 throw std::runtime_error (std::string (" Failed to create socket : " ) + std::strerror (errno));
6060 }
6161
6262 Socket::Socket (Socket &&other) noexcept :
63- sockfd (INVALID_SOCKET)
63+ m_sockfd (INVALID_SOCKET)
6464 {
6565 *this = std::move (other);
6666 }
@@ -70,19 +70,19 @@ namespace CppSockets {
7070 return *this ;
7171 this ->close ();
7272
73- sockfd = other.sockfd ;
74- domain = other.domain ;
75- other.sockfd = INVALID_SOCKET;
76- is_connected = other.is_connected ;
73+ m_sockfd = other.m_sockfd ;
74+ m_domain = other.m_domain ;
75+ other.m_sockfd = INVALID_SOCKET;
76+ m_is_connected = other.m_is_connected ;
7777 return *this ;
7878 }
7979
8080 void Socket::close () {
81- if (sockfd != INVALID_SOCKET) {
81+ if (m_sockfd != INVALID_SOCKET) {
8282#ifdef OS_WINDOWS
8383 closesocket (raw_socket);
8484#else
85- ::close (sockfd );
85+ ::close (m_sockfd );
8686#endif
8787 }
8888 }
@@ -136,13 +136,13 @@ namespace CppSockets {
136136 std::size_t Socket::read (char *buff, std::size_t size) {
137137 std::size_t ret;
138138
139- if (!is_connected )
139+ if (!m_is_connected )
140140 throw std::runtime_error (" Not connected" );
141- ret = ::read (sockfd , buff, size);
141+ ret = ::read (m_sockfd , buff, size);
142142 if (ret < 0 ) {
143143 throw std::runtime_error (std::string (" Failed to read from socket: " ) + Socket::strerror ());
144144 } else if (ret == 0 && size > 0 ) {
145- is_connected = false ;
145+ m_is_connected = false ;
146146 }
147147 return ret;
148148 }
@@ -154,9 +154,9 @@ namespace CppSockets {
154154 std::size_t Socket::write (const char *buff, std::size_t len) {
155155 std::size_t ret;
156156
157- if (!is_connected )
157+ if (!m_is_connected )
158158 throw std::runtime_error (" Not connected" );
159- ret = ::write (sockfd , buff, len);
159+ ret = ::write (m_sockfd , buff, len);
160160 if (ret < 0 ) {
161161 throw std::runtime_error (std::string (" Failed to write to socket: " ) + Socket::strerror ());
162162 }
@@ -170,11 +170,11 @@ namespace CppSockets {
170170 }
171171
172172 int Socket::getsockopt (int level, int optname, void *optval, socklen_t *optlen) {
173- return this ->getsockopt (sockfd , level, optname, optval, optlen);
173+ return this ->getsockopt (m_sockfd , level, optname, optval, optlen);
174174 }
175175
176176 int Socket::setsockopt (int level, int optname, const void *optval, socklen_t optlen) {
177- int ret = ::setsockopt (sockfd , level, optname, optval, optlen);
177+ int ret = ::setsockopt (m_sockfd , level, optname, optval, optlen);
178178
179179 if (ret < 0 ) {
180180 throw std::runtime_error (std::string (" Failed to set sock opt: " ) + Socket::strerror ());
@@ -195,10 +195,10 @@ namespace CppSockets {
195195 struct sockaddr_in addr = {};
196196 int ret = 0 ;
197197
198- addr.sin_family = domain ;
198+ addr.sin_family = m_domain ;
199199 addr.sin_addr .s_addr = s_addr;
200200 addr.sin_port = htons (port);
201- ret = ::bind (sockfd , (struct sockaddr *)&addr, sizeof (addr));
201+ ret = ::bind (m_sockfd , (struct sockaddr *)&addr, sizeof (addr));
202202 if (ret < 0 ) {
203203 throw std::runtime_error (std::string (" Failed to bind socket: " ) + Socket::strerror ());
204204 }
@@ -216,20 +216,20 @@ namespace CppSockets {
216216 addr.sin_addr .s_addr = endpoint.getAddr ().getAddress ();
217217 addr.sin_port = htons (endpoint.getPort ());
218218 addr.sin_family = endpoint.getAddr ().getFamily ();
219- ret = ::connect (sockfd , (const struct sockaddr *)&addr, sizeof (addr));
219+ ret = ::connect (m_sockfd , (const struct sockaddr *)&addr, sizeof (addr));
220220 if (ret < 0 ) {
221221 throw std::runtime_error (std::string (" Failed to connect socket to " ) + endpoint.toString () + " : " + Socket::strerror ());
222222 }
223- is_connected = ret == 0 ;
223+ m_is_connected = ret == 0 ;
224224 return ret;
225225 }
226226
227227 bool Socket::connected () const {
228- return is_connected ;
228+ return m_is_connected ;
229229 }
230230
231231 int Socket::listen (int backlog) {
232- int ret = ::listen (sockfd , backlog);
232+ int ret = ::listen (m_sockfd , backlog);
233233
234234 if (ret < 0 ) {
235235 throw std::runtime_error (std::string (" Failed to listen socket: " ) + Socket::strerror ());
@@ -238,7 +238,7 @@ namespace CppSockets {
238238 }
239239
240240 std::shared_ptr<Socket> Socket::accept (void *addr_out) {
241- int fd = ::accept (sockfd , nullptr , nullptr );
241+ int fd = ::accept (m_sockfd , nullptr , nullptr );
242242 int domain = 0 ;
243243 int type = 0 ;
244244 int protocol = 0 ;
@@ -253,14 +253,14 @@ namespace CppSockets {
253253 }
254254
255255 void Socket::set_blocking (bool val) {
256- int flags = fcntl (sockfd , F_GETFL, 0 );
256+ int flags = fcntl (m_sockfd , F_GETFL, 0 );
257257 int ret = flags;
258258
259259 if (ret >= 0 ) {
260260 if (val) {
261- ret = fcntl (sockfd , F_SETFL, flags | O_NONBLOCK); // NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg, hicpp-signed-bitwise)
261+ ret = fcntl (m_sockfd , F_SETFL, flags | O_NONBLOCK); // NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg, hicpp-signed-bitwise)
262262 } else {
263- ret = fcntl (sockfd , F_SETFL, (flags | O_NONBLOCK) ^ O_NONBLOCK); // NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg, hicpp-signed-bitwise)
263+ ret = fcntl (m_sockfd , F_SETFL, (flags | O_NONBLOCK) ^ O_NONBLOCK); // NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg, hicpp-signed-bitwise)
264264 }
265265 }
266266 if (ret < 0 ) {
@@ -269,18 +269,18 @@ namespace CppSockets {
269269 }
270270
271271 RawSocketType Socket::get_fd () const {
272- return sockfd ;
272+ return m_sockfd ;
273273 }
274274
275275 int Socket::get_domain () const {
276- return domain ;
276+ return m_domain ;
277277 }
278278
279279 int Socket::get_protocol () const {
280- return protocol ;
280+ return m_protocol ;
281281 }
282282
283283 int Socket::get_type () const {
284- return type ;
284+ return m_type ;
285285 }
286286}
0 commit comments