diff --git a/include/opendht/peer_discovery.h b/include/opendht/peer_discovery.h index 2f2dd94b9bd50831ea6fb5080c107b4b530ac7b4..98bb349d80286f111e644b9c176eabed499ccc7d 100644 --- a/include/opendht/peer_discovery.h +++ b/include/opendht/peer_discovery.h @@ -22,8 +22,6 @@ #include "sockaddr.h" #include "infohash.h" -#include <unistd.h> - #include <thread> #include <mutex> #include <condition_variable> diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index 9356276ce6604a67dc3265a41733df5e20d7acd9..23e2782ccad816ea30edf1ee3fd736f6ed9466fd 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -27,6 +27,12 @@ #include "dht_proxy_client.h" #endif +#ifdef _WIN32 +#include <cstring> +#define close(x) closesocket(x) +#define write(s, b, f) send(s, b, (int)strlen(b), 0) +#endif + namespace dht { constexpr std::chrono::seconds DhtRunner::BOOTSTRAP_PERIOD; @@ -46,7 +52,6 @@ DhtRunner::DhtRunner() : dht_() #ifdef OPENDHT_PROXY_CLIENT , dht_via_proxy_() #endif //OPENDHT_PROXY_CLIENT -, peerDiscovery4_(), peerDiscovery6_() { #ifdef _WIN32 WSADATA wsd; @@ -482,6 +487,7 @@ int bindSocket(const SockAddr& addr, SockAddr& bound) #endif if (is_ipv6) setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&set, sizeof(set)); + net::set_nonblocking(sock); int rc = bind(sock, addr.get(), addr.getLength()); if(rc < 0) { rc = errno; diff --git a/src/network_engine.cpp b/src/network_engine.cpp index 27d80e9f3f5b813c0b55624cdbacbd5491a5c185..8a1197db51ad29d2fd9e484d4fd689011efc9514 100644 --- a/src/network_engine.cpp +++ b/src/network_engine.cpp @@ -17,27 +17,14 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ - #include "network_engine.h" #include "request.h" #include "default_types.h" #include "log_enable.h" #include "parsed_message.h" -#include "network_utils.h" #include <msgpack.hpp> -#ifndef _WIN32 -#include <arpa/inet.h> -#include <unistd.h> -#else -#include <ws2tcpip.h> -#include <io.h> -#endif -#include <fcntl.h> - -#include <cstring> - namespace dht { namespace net { @@ -56,7 +43,7 @@ constexpr std::chrono::seconds NetworkEngine::RX_TIMEOUT; const std::string NetworkEngine::my_v {"RNG1"}; constexpr size_t NetworkEngine::MAX_REQUESTS_PER_SEC; -static const uint8_t v4prefix[16] = { +static constexpr uint8_t v4prefix[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 }; @@ -138,16 +125,7 @@ NetworkEngine::NetworkEngine(InfoHash& myid, NetId net, const int& s, const int& onAnnounce(std::move(onAnnounce)), onRefresh(std::move(onRefresh)), myid(myid), network(net), dht_socket(s), dht_socket6(s6), DHT_LOG(log), scheduler(scheduler) -{ - if (dht_socket >= 0) { - if (!set_nonblocking(dht_socket)) - throw DhtException("Can't set socket to non-blocking mode"); - } - if (dht_socket6 >= 0) { - if (!set_nonblocking(dht_socket6)) - throw DhtException("Can't set socket to non-blocking mode"); - } -} +{} NetworkEngine::~NetworkEngine() { clear(); diff --git a/src/network_utils.cpp b/src/network_utils.cpp index 37533b53114db0b40942f793c7937a7dee9337b5..59ab926fb4baffbc43cf19eda2b26bd018811ba3 100644 --- a/src/network_utils.cpp +++ b/src/network_utils.cpp @@ -18,23 +18,17 @@ #include "network_utils.h" -#ifndef _WIN32 +#ifdef _WIN32 #include "utils.h" #include "sockaddr.h" - -#include <arpa/inet.h> -#include <unistd.h> -#include <sys/socket.h> -#else -#include <winsock2.h> -#include <ws2tcpip.h> #include <io.h> +#include <string> +#include <cstring> #define close(x) closesocket(x) #define write(s, b, f) send(s, b, (int)strlen(b), 0) -#endif +#else #include <fcntl.h> - -#include <string> +#endif namespace dht { namespace net { diff --git a/src/network_utils.h b/src/network_utils.h index b98ccac2a7367a20cbcfb20168c8d298a4653c49..bc63904cbc04d64ff5775b90d1f7ae830d70484d 100644 --- a/src/network_utils.h +++ b/src/network_utils.h @@ -18,9 +18,12 @@ #pragma once #ifdef _WIN32 +#include <ws2tcpip.h> #include <winsock2.h> -#define close(x) closesocket(x) -#define write(s, b, f) send(s, b, (int)strlen(b), 0) +#else +#include <sys/socket.h> +#include <netinet/in.h> +#include <unistd.h> #endif #ifndef IPV6_JOIN_GROUP @@ -34,7 +37,6 @@ bool set_nonblocking(int fd, bool nonblocking = true); #ifdef _WIN32 void udpPipe(int fds[2]); -// extern const char *inet_ntop(int, const void *, char *, socklen_t); #endif } diff --git a/src/peer_discovery.cpp b/src/peer_discovery.cpp index 167636c192c4f60689080482f759a33008f6d391..f59375b8057b9122ebd0b193ca081d0813c2b3c7 100644 --- a/src/peer_discovery.cpp +++ b/src/peer_discovery.cpp @@ -22,8 +22,12 @@ #ifdef _WIN32 #include <Ws2tcpip.h> // needed for ip_mreq definition for multicast #include <Windows.h> +#include <cstring> +#define close(x) closesocket(x) +#define write(s, b, f) send(s, b, (int)strlen(b), 0) #else #include <sys/types.h> +#include <unistd.h> #endif #include <fcntl.h> diff --git a/tests/peerdiscoverytester.cpp b/tests/peerdiscoverytester.cpp index 9c41c593e2ab28fdca834adabb595811d1c4309c..4f3b0535a760d0ace3d83148621aa11abad65b16 100644 --- a/tests/peerdiscoverytester.cpp +++ b/tests/peerdiscoverytester.cpp @@ -42,7 +42,7 @@ void PeerDiscoveryTester::testTransmission_ipv4(){ test_n.startPublish(data_n,port_n); - sleep(5); + std::this_thread::sleep_for(std::chrono::seconds(5)); test_n.stop(); test_s.stop(); test_n.join(); @@ -63,8 +63,8 @@ void PeerDiscoveryTester::testTransmission_ipv6(){ int port = 3333; in_port_t port_n = 50001; try{ - dht::PeerDiscovery test_n(AF_INET6,port); - dht::PeerDiscovery test_s(AF_INET6,port); + dht::PeerDiscovery test_n(AF_INET6, port); + dht::PeerDiscovery test_s(AF_INET6, port); try{ test_s.startDiscovery([&](const dht::InfoHash& node, const dht::SockAddr& addr){ @@ -74,19 +74,16 @@ void PeerDiscoveryTester::testTransmission_ipv6(){ test_n.startPublish(data_n,port_n); - sleep(5); + std::this_thread::sleep_for(std::chrono::seconds(5)); test_n.stop(); test_s.stop(); test_n.join(); test_s.join(); } catch(std::exception &exception){ - perror(exception.what()); CPPUNIT_ASSERT(false); } - } catch(std::exception &exception){ - perror(exception.what()); + } catch(std::exception &exception) { } - } void PeerDiscoveryTester::tearDown(){}