<commit_before>/////
// lib-lifx.cpp
//! @file Library implementation
/////

#include <lib-lifx/lifx.h>

#include <array>
#include <chrono>
#include <thread>

#ifdef _WIN32
#include <WinSock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif

namespace
{
  constexpr int sockAddrLen = sizeof(sockaddr_in);
  struct sockaddr_in listen_addr = { 0, };
  struct sockaddr_in send_direct_addr = { 0, };
  struct sockaddr_in broadcast_addr = { 0, };
  int sock;
}

namespace lifx
{
  LifxClient::LifxClient(int sourceId)
    : m_sourceId(std::move(sourceId))
  {
    // TODO: Error checking

#ifdef _WIN32
    // Start WinSock
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);
#endif

    // Setup addresses
    listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    listen_addr.sin_port = htons(56700);
    listen_addr.sin_family = AF_INET;

    //send_direct_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    send_direct_addr.sin_port = htons(56700);
    send_direct_addr.sin_family = AF_INET;

    broadcast_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    broadcast_addr.sin_port = htons(56700);
    broadcast_addr.sin_family = AF_INET;

    // Create our actual socket
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    // Allow this socket to broadcast
    int yes = 1;
    setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&yes, sizeof(int));

    // Allow this socket to reuse addresses
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int));

    // Bind the socket to our listening address
    bind(sock, (struct sockaddr*)&listen_addr, sockAddrLen);
  }

  LifxClient::~LifxClient()
  {
    // Delete all callbacks
    for (auto iter : m_callbacks)
    {
      delete iter.second;
    }

#ifdef _WIN32
    // Close the socket
    closesocket(sock);
    // Stop WinSock
    WSACleanup();
#else
    // Close the socket
    close(client_socket);
#endif
  }

  int LifxClient::SendBuffer(const std::vector<char>& buffer)
  {
    if (buffer.empty())
      return 0;

    int ret = sendto(sock, buffer.data(), buffer.size(), 0,
      (struct sockaddr*)&broadcast_addr, sockAddrLen);
    return std::move(ret);
  }

  LifxClient::RunResult LifxClient::RunOnce(long seconds, long milliseconds)
  {
    struct timeval timeout;
    timeout.tv_sec = std::move(seconds);
    timeout.tv_usec = std::move(milliseconds);

    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(static_cast<unsigned int>(sock), &rfds);

    int ret = select(sock, &rfds, nullptr, nullptr, &timeout);
    if (ret == -1)
    {
      return RunResult::RUN_ERROR;
    }
    else if (ret && FD_ISSET(sock, &rfds))
    {
      std::array<char, MAX_LIFX_PACKET_SIZE> buffer;<commit_msg>Mixed wrong socket variable name.<commit_after>/////
// lib-lifx.cpp
//! @file Library implementation
/////

#include <lib-lifx/lifx.h>

#include <array>
#include <chrono>
#include <thread>

#ifdef _WIN32
#include <WinSock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif

namespace
{
  constexpr int sockAddrLen = sizeof(sockaddr_in);
  struct sockaddr_in listen_addr = { 0, };
  struct sockaddr_in send_direct_addr = { 0, };
  struct sockaddr_in broadcast_addr = { 0, };
  int sock;
}

namespace lifx
{
  LifxClient::LifxClient(int sourceId)
    : m_sourceId(std::move(sourceId))
  {
    // TODO: Error checking

#ifdef _WIN32
    // Start WinSock
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);
#endif

    // Setup addresses
    listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    listen_addr.sin_port = htons(56700);
    listen_addr.sin_family = AF_INET;

    //send_direct_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    send_direct_addr.sin_port = htons(56700);
    send_direct_addr.sin_family = AF_INET;

    broadcast_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    broadcast_addr.sin_port = htons(56700);
    broadcast_addr.sin_family = AF_INET;

    // Create our actual socket
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    // Allow this socket to broadcast
    int yes = 1;
    setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&yes, sizeof(int));

    // Allow this socket to reuse addresses
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int));

    // Bind the socket to our listening address
    bind(sock, (struct sockaddr*)&listen_addr, sockAddrLen);
  }

  LifxClient::~LifxClient()
  {
    // Delete all callbacks
    for (auto iter : m_callbacks)
    {
      delete iter.second;
    }

#ifdef _WIN32
    // Close the socket
    closesocket(sock);
    // Stop WinSock
    WSACleanup();
#else
    // Close the socket
    close(sock);
#endif
  }

  int LifxClient::SendBuffer(const std::vector<char>& buffer)
  {
    if (buffer.empty())
      return 0;

    int ret = sendto(sock, buffer.data(), buffer.size(), 0,
      (struct sockaddr*)&broadcast_addr, sockAddrLen);
    return std::move(ret);
  }

  LifxClient::RunResult LifxClient::RunOnce(long seconds, long milliseconds)
  {
    struct timeval timeout;
    timeout.tv_sec = std::move(seconds);
    timeout.tv_usec = std::move(milliseconds);

    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(static_cast<unsigned int>(sock), &rfds);

    int ret = select(sock, &rfds, nullptr, nullptr, &timeout);
    if (ret == -1)
    {
      return RunResult::RUN_ERROR;
    }
    else if (ret && FD_ISSET(sock, &rfds))
    {
      std::array<char, MAX_LIFX_PACKET_SIZE> buffer;
{'fraction_non_alphanumeric': 0.1307955980515966, 'fraction_numerical': 0.011546094172830597, 'mean_word_length': 3.4565916398713825, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7446641', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2014, 'n_words': 429}