<commit_before>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include "Network.hpp"
#include "utils/Log.hpp"

namespace ouzel
{
    namespace network
    {
        Network::Network()
        {
        }

        Network::~Network()
        {
            if (endpoint != NULL_SOCKET)
            {
#ifdef _WIN32
                int result = closesocket(endpoint);
#else
                int result = close(endpoint);
#endif

                if (result < 0)
                {
                    int error = getLastError();
                    Log(Log::Level::ERR) << "Failed to close socket, error: " << error;
                }
            }

#ifdef _WIN32
            WSACleanup();
#endif
        }

        int Network::getLastError()
        {
#ifdef _WIN32
            return WSAGetLastError();
#else
            return errno;
#endif
        }

        bool Network::getAddress(const std::string& address, uint32_t& result)
        {
            addrinfo* info;
            int ret = getaddrinfo(address.c_str(), nullptr, nullptr, &info);

#ifdef _WIN32
            if (ret != 0 && WSAGetLastError() == WSANOTINITIALISED)
            {
                if (!initWSA()) return false;

                ret = getaddrinfo(addressStr.c_str(), portStr.empty() ? nullptr : portStr.c_str(), nullptr, &info);
            }
#endif

            if (ret != 0)
            {
                int error = getLastError();
                Log(Log::Level::ERR) << "Failed to get address info of " << address << ", error: " << error;
                return false;
            }

            sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
            result = addr->sin_addr.s_addr;
            
            freeaddrinfo(info);
            
            return true;
        }

        bool Network::init()
        {
#ifdef _WIN32
            WORD sockVersion = MAKEWORD(2, 2);
            WSADATA wsaData;
            int error = WSAStartup(sockVersion, &wsaData);
            if (error != 0)
            {
                Log(Log::Level::ERR) << "Failed to start Winsock failed, error: " << error;
                return false;
            }
    
            if (wsaData.wVersion != sockVersion)
            {
                Log(Log::Level::ERR) << "Incorrect Winsock version";
                WSACleanup();
                return false;
            }
#endif

            return true;
        }

        bool Network::listen(const std::string& address, uint16_t port)
        {
            return true;
        }

        bool Network::connect(const std::string& address, uint16_t port)
        {
            return true;
        }

        bool Network::disconnect()
        {
            return true;
        }
    } // namespace network
} // namespace ouzel
<commit_msg>Convert the IP address to host endianness Don't call initWSA<commit_after>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include "Network.hpp"
#include "utils/Log.hpp"

namespace ouzel
{
    namespace network
    {
        Network::Network()
        {
        }

        Network::~Network()
        {
            if (endpoint != NULL_SOCKET)
            {
#ifdef _WIN32
                int result = closesocket(endpoint);
#else
                int result = close(endpoint);
#endif

                if (result < 0)
                {
                    int error = getLastError();
                    Log(Log::Level::ERR) << "Failed to close socket, error: " << error;
                }
            }

#ifdef _WIN32
            WSACleanup();
#endif
        }

        int Network::getLastError()
        {
#ifdef _WIN32
            return WSAGetLastError();
#else
            return errno;
#endif
        }

        bool Network::getAddress(const std::string& address, uint32_t& result)
        {
            addrinfo* info;
            int ret = getaddrinfo(address.c_str(), nullptr, nullptr, &info);

            if (ret != 0)
            {
                int error = getLastError();
                Log(Log::Level::ERR) << "Failed to get address info of " << address << ", error: " << error;
                return false;
            }

            sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
            result = ntohl(addr->sin_addr.s_addr);
            
            freeaddrinfo(info);
            
            return true;
        }

        bool Network::init()
        {
#ifdef _WIN32
            WORD sockVersion = MAKEWORD(2, 2);
            WSADATA wsaData;
            int error = WSAStartup(sockVersion, &wsaData);
            if (error != 0)
            {
                Log(Log::Level::ERR) << "Failed to start Winsock failed, error: " << error;
                return false;
            }
    
            if (wsaData.wVersion != sockVersion)
            {
                Log(Log::Level::ERR) << "Incorrect Winsock version";
                WSACleanup();
                return false;
            }
#endif

            return true;
        }

        bool Network::listen(const std::string& address, uint16_t port)
        {
            return true;
        }

        bool Network::connect(const std::string& address, uint16_t port)
        {
            return true;
        }

        bool Network::disconnect()
        {
            return true;
        }
    } // namespace network
} // namespace ouzel

{'fraction_non_alphanumeric': 0.11250645105797351, 'fraction_numerical': 0.009805608119731636, 'mean_word_length': 1.4952789699570816, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2851803', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1643, 'n_words': 408}