# HG changeset patch # User David Demelier # Date 1426063308 -3600 # Node ID 486767e1d165ed57d0d71a7b9a65913ff71fa68f # Parent 0b576ee64d4544f1e560c40b16b36142ae1fc3cd Socket: fix under Windows and add new style diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/Socket.cpp --- a/C++/modules/Socket/Socket.cpp Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/Socket.cpp Wed Mar 11 09:41:48 2015 +0100 @@ -105,14 +105,16 @@ Socket::Socket(int domain, int type, int protocol) { #if defined(_WIN32) && !defined(SOCKET_NO_WSA_INIT) - if (!s_initialized) + if (!s_initialized) { initialize(); + } #endif m_handle = ::socket(domain, type, protocol); - if (m_handle == Invalid) + if (m_handle == Invalid) { throw SocketError(SocketError::System, "socket"); + } m_state = SocketState::Opened; } @@ -122,8 +124,9 @@ const auto &sa = address.address(); const auto addrlen = address.length(); - if (::bind(m_handle, reinterpret_cast(&sa), addrlen) == Error) + if (::bind(m_handle, reinterpret_cast(&sa), addrlen) == Error) { throw SocketError(SocketError::System, "bind"); + } m_state = SocketState::Bound; } @@ -144,21 +147,25 @@ #if defined(O_NONBLOCK) && !defined(_WIN32) int flags; - if ((flags = fcntl(m_handle, F_GETFL, 0)) == -1) + if ((flags = fcntl(m_handle, F_GETFL, 0)) == -1) { flags = 0; + } - if (block) + if (block) { flags &= ~(O_NONBLOCK); - else + } else { flags |= O_NONBLOCK; + } - if (fcntl(m_handle, F_SETFL, flags) == Error) + if (fcntl(m_handle, F_SETFL, flags) == Error) { throw SocketError(SocketError::System, "setBlockMode"); + } #else unsigned long flags = (block) ? 0 : 1; - if (ioctlsocket(m_handle, FIONBIO, &flags) == Error) + if (ioctlsocket(m_handle, FIONBIO, &flags) == Error) { throw SocketError(SocketError::System, "setBlockMode"); + } #endif } diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/Socket.h --- a/C++/modules/Socket/Socket.h Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/Socket.h Wed Mar 11 09:41:48 2015 +0100 @@ -187,7 +187,7 @@ * to Windows. */ #if defined(_WIN32) - static constexpr const int Invalid = INVALID_SOCKET; + static constexpr const Handle Invalid = INVALID_SOCKET; static constexpr const int Error = SOCKET_ERROR; #else static constexpr const int Invalid = -1; @@ -214,7 +214,7 @@ WSACleanup(); } - static inline void init() noexcept + static inline void initialize() noexcept { std::lock_guard lock(s_mutex); @@ -307,7 +307,7 @@ inline void set(int level, int name, const Argument &arg) { #if defined(_WIN32) - if (setsockopt(m_handle, level, name, (Socket::ConstArg)&arg, sizeof (arg)) == SOCKET_ERROR) + if (setsockopt(m_handle, level, name, (Socket::ConstArg)&arg, sizeof (arg)) == Error) #else if (setsockopt(m_handle, level, name, (Socket::ConstArg)&arg, sizeof (arg)) < 0) #endif @@ -328,7 +328,7 @@ socklen_t size = sizeof (result); #if defined(_WIN32) - if (getsockopt(m_handle, level, name, (Socket::Arg)&desired, &size) == SOCKET_ERROR) + if (getsockopt(m_handle, level, name, (Socket::Arg)&desired, &size) == Error) #else if (getsockopt(m_handle, level, name, (Socket::Arg)&desired, &size) < 0) #endif diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/SocketAddress.cpp --- a/C++/modules/Socket/SocketAddress.cpp Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/SocketAddress.cpp Wed Mar 11 09:41:48 2015 +0100 @@ -55,8 +55,9 @@ hints.ai_family = domain; auto error = getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &res); - if (error != 0) + if (error != 0) { throw SocketError(SocketError::System, "getaddrinfo", gai_strerror(error)); + } std::memcpy(&m_addr, res->ai_addr, res->ai_addrlen); m_addrlen = res->ai_addrlen; @@ -77,8 +78,9 @@ sockaddr_un *sun = (sockaddr_un *)&m_addr; // Silently remove the file even if it fails - if (rm) + if (rm) { ::remove(path.c_str()); + } // Copy the path memset(sun->sun_path, 0, sizeof (sun->sun_path)); diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/SocketListener.cpp --- a/C++/modules/Socket/SocketListener.cpp Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/SocketListener.cpp Wed Mar 11 09:41:48 2015 +0100 @@ -163,10 +163,12 @@ { short result(0); - if (direction & SocketListener::Read) + if (direction & SocketListener::Read) { result |= POLLIN; - if (direction & SocketListener::Write) + } + if (direction & SocketListener::Write) { result |= POLLOUT; + } return result; } @@ -182,10 +184,12 @@ * At least, even if POLLHUP or POLLIN is set, recv() always * return 0 so we mark the socket as readable. */ - if ((event & POLLIN) || (event & POLLHUP)) + if ((event & POLLIN) || (event & POLLHUP)) { direction |= SocketListener::Read; - if (event & POLLOUT) + } + if (event & POLLOUT) { direction |= SocketListener::Write; + } return direction; } @@ -196,9 +200,9 @@ auto it = std::find_if(m_fds.begin(), m_fds.end(), [&] (const auto &pfd) { return pfd.fd == s.handle(); }); // If found, add the new direction, otherwise add a new socket - if (it != m_fds.end()) + if (it != m_fds.end()) { it->events |= topoll(direction); - else { + } else { m_lookup.insert({s.handle(), s}); m_fds.push_back({ s.handle(), topoll(direction), 0 }); } @@ -216,8 +220,9 @@ } else { ++i; } - } else + } else { ++i; + } } } @@ -240,10 +245,12 @@ SocketStatus select(int ms) override { auto result = poll(m_fds.data(), m_fds.size(), ms); - if (result == 0) + if (result == 0) { throw SocketError(SocketError::Timeout, "select", "Timeout while listening"); - if (result < 0) + } + if (result < 0) { throw SocketError(SocketError::System, "poll"); + } for (auto &fd : m_fds) { if (fd.revents != 0) { @@ -289,8 +296,9 @@ SocketListener::SocketListener(std::initializer_list, int>> list) : SocketListener() { - for (const auto &p : list) + for (const auto &p : list) { set(p.first, p.second); + } } SocketListener::SocketListener(SocketMethod method) diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/SocketSsl.cpp --- a/C++/modules/Socket/SocketSsl.cpp Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/SocketSsl.cpp Wed Mar 11 09:41:48 2015 +0100 @@ -60,8 +60,9 @@ , m_ssl(ssl, SSL_free) { #if !defined(SOCKET_NO_SSL_INIT) - if (!s_sslInitialized) + if (!s_sslInitialized) { sslInitialize(); + } #endif } @@ -70,8 +71,9 @@ , m_options(std::move(options)) { #if !defined(SOCKET_NO_SSL_INIT) - if (!s_sslInitialized) + if (!s_sslInitialized) { sslInitialize(); + } #endif } diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/SocketTcp.cpp --- a/C++/modules/Socket/SocketTcp.cpp Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/SocketTcp.cpp Wed Mar 11 09:41:48 2015 +0100 @@ -26,8 +26,9 @@ void SocketAbstractTcp::listen(int max) { - if (::listen(m_handle, max) == Error) + if (::listen(m_handle, max) == Error) { throw SocketError(SocketError::System, "listen"); + } } Socket SocketAbstractTcp::standardAccept(SocketAddress &info) @@ -45,13 +46,15 @@ #if defined(_WIN32) int error = WSAGetLastError(); - if (error == WSAEWOULDBLOCK) + if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "accept", error); + } throw SocketError(SocketError::System, "accept", error); #else - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "accept"); + } throw SocketError(SocketError::System, "accept"); #endif @@ -64,8 +67,9 @@ void SocketAbstractTcp::standardConnect(const SocketAddress &address) { - if (m_state == SocketState::Connected) + if (m_state == SocketState::Connected) { return; + } auto &sa = address.address(); auto addrlen = address.length(); @@ -78,13 +82,15 @@ #if defined(_WIN32) int error = WSAGetLastError(); - if (error == WSAEWOULDBLOCK) + if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "connect", error); + } throw SocketError(SocketError::System, "connect", error); #else - if (errno == EINPROGRESS) + if (errno == EINPROGRESS) { throw SocketError(SocketError::WouldBlockWrite, "connect"); + } throw SocketError(SocketError::System, "connect"); #endif @@ -116,8 +122,9 @@ void SocketTcp::waitConnect(const SocketAddress &address, int timeout) { - if (m_state == SocketState::Connected) + if (m_state == SocketState::Connected) { return; + } // Initial try try { @@ -168,18 +175,21 @@ #if defined(_WIN32) int error = WSAGetLastError(); - if (error == WSAEWOULDBLOCK) - throw SocketError(SocketError::WouldBlockRead, "recv", error) + if (error == WSAEWOULDBLOCK) { + throw SocketError(SocketError::WouldBlockRead, "recv", error); + } throw SocketError(SocketError::System, "recv", error); #else - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "recv"); + } throw SocketError(SocketError::System, "recv"); #endif - } else if (nbread == 0) + } else if (nbread == 0) { m_state = SocketState::Closed; + } return (unsigned)nbread; } @@ -202,13 +212,15 @@ #if defined(_WIN32) int error = WSAGetLastError(); - if (error == WSAEWOULDBLOCK) + if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "send", error); + } throw SocketError(SocketError::System, "send", error); #else - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "send"); + } throw SocketError(SocketError::System, "send"); #endif diff -r 0b576ee64d45 -r 486767e1d165 C++/modules/Socket/SocketUdp.cpp --- a/C++/modules/Socket/SocketUdp.cpp Sun Mar 08 14:26:33 2015 +0100 +++ b/C++/modules/Socket/SocketUdp.cpp Wed Mar 11 09:41:48 2015 +0100 @@ -41,13 +41,15 @@ #if defined(_WIN32) int error = WSAGetLastError(); - if (error == WSAEWOULDBLOCK) + if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "recvfrom", error); + } throw SocketError(SocketError::System, "recvfrom", error); #else - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "recvfrom"); + } throw SocketError(SocketError::System, "recvfrom"); #endif @@ -65,13 +67,15 @@ #if defined(_WIN32) int error = WSAGetLastError(); - if (error == WSAEWOULDBLOCK) + if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "sendto", error); + } throw SocketError(SocketError::System, "sendto", error); #else - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "sendto"); + } throw SocketError(SocketError::System, "sendto"); #endif diff -r 0b576ee64d45 -r 486767e1d165 CMakeLists.txt --- a/CMakeLists.txt Sun Mar 08 14:26:33 2015 +0100 +++ b/CMakeLists.txt Wed Mar 11 09:41:48 2015 +0100 @@ -313,12 +313,18 @@ # Sockets # --------------------------------------------------------- +if (WIN32) + set(SOCKET_LIBRARIES ws2_32) +endif () + define_module( TARGET socket NAME Socket DIRECTORY Socket INCLUDES ${OPENSSL_INCLUDE_DIR} - LIBRARIES ${OPENSSL_LIBRARIES} + LIBRARIES + ${SOCKET_LIBRARIES} + ${OPENSSL_LIBRARIES} SOURCES ${code_SOURCE_DIR}/C++/modules/Socket/SocketAddress.cpp ${code_SOURCE_DIR}/C++/modules/Socket/SocketAddress.h