<commit_before>#include "comm.hpp"

#include <stdint.h>
#include <errno.h>
#include <algorithm>
#include <assert.h>

#if defined(__unix__)
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#elif defined(_WIN32)
#define NOMINMAX
#include <winsock2.h>
#endif

#include "log.hpp"

namespace fcwt {

const char* const server_ipv4_addr = "192.168.0.1";

struct sock::impl
{
	native_socket sockfd;
	impl(native_socket fd) : sockfd(fd) {}
	impl(impl const&) = delete;
	impl& operator=(impl const&) = delete;

	~impl()
	{
		if (sockfd)
		{
#if defined(_WIN32)
			closesocket(sockfd);
#elif defined(__unix__)
			close(sockfd);
#endif
		}
	}
};

sock::sock(native_socket fd) : sockfd(new impl(fd)) {}
sock::~sock() {}
sock::sock(sock&& other) : sockfd(std::move(other.sockfd)) { }

sock::operator native_socket() const
{
	return sockfd ? sockfd->sockfd : 0;
}

sock& sock::operator=(sock&& other) {
  std::move(other.sockfd);
  return *this;
}

void sock::swap(sock& other) {
	using std::swap;
	swap(sockfd, other.sockfd);
}

sock connect_to_camera(int port) {
  // TODO: proper error handling

  const native_socket sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) fatal_error("Failed to create socket\n");

#if defined(__unix__)
  fcntl(sockfd, F_SETFL, O_NONBLOCK);  // for timeout

  sockaddr_in sa = {};
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  inet_pton(AF_INET, server_ipv4_addr, &sa.sin_addr);
  connect(sockfd, reinterpret_cast<sockaddr*>(&sa), sizeof(sa));

  // timeout handling
  fd_set fdset;
  FD_ZERO(&fdset);
  FD_SET(sockfd, &fdset);
  struct timeval tv = {};
  tv.tv_sec = 1;
  tv.tv_usec = 0;

  if (select(sockfd + 1, NULL, &fdset, NULL, &tv) == 1) {
    int so_error = 0;
    socklen_t len = sizeof so_error;
    getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len);

    if (so_error == 0) {
      printf("Connection esatablished %s:%d (%d)\n", server_ipv4_addr, port,
             sockfd);
      fcntl(sockfd, F_SETFL, 0);
      return sockfd;
    }
  }

  printf("Failed to connect\n");
  closesocket(sockfd);

#else defined(_WIN32)
  // TODO
#endif
  
  return 0;
}

size_t to_fuji_size_prefix(size_t sizeBytes) {<commit_msg>Windows socket implementation<commit_after>#include "comm.hpp"

#include <stdint.h>
#include <errno.h>
#include <algorithm>
#include <assert.h>

#if defined(__unix__)
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#elif defined(_WIN32)
#define NOMINMAX
#include <winsock2.h>
#include <Ws2tcpip.h>
#endif

#include "log.hpp"

namespace fcwt {

const char* const server_ipv4_addr = "192.168.0.1";

static void close_socket(native_socket sockfd)
{
	if (sockfd)
	{
#if defined(_WIN32)
		closesocket(sockfd);
#elif defined(__unix__)
		close(sockfd);
#endif
	}
}

struct sock::impl
{
	native_socket sockfd;
	impl(native_socket fd) : sockfd(fd) {}
	impl(impl const&) = delete;
	impl& operator=(impl const&) = delete;

	~impl()
	{
		close_socket(sockfd);
	}
};

sock::sock(native_socket fd) : sockfd(new impl(fd)) {}
sock::~sock() {}
sock::sock(sock&& other) : sockfd(std::move(other.sockfd)) { }

sock::operator native_socket() const
{
	return sockfd ? sockfd->sockfd : 0;
}

sock& sock::operator=(sock&& other) {
  std::move(other.sockfd);
  return *this;
}

void sock::swap(sock& other) {
	using std::swap;
	swap(sockfd, other.sockfd);
}

static void set_nonblocking_io(native_socket sockfd, bool nonblocking)
{
#if defined(__unix__)
	fcntl(sockfd, F_SETFL, nonblocking ? O_NONBLOCK : 0);  // for timeout
#elif defined(_WIN32)
	u_long arg = nonblocking ? 1 : 0;
	ioctlsocket(sockfd, FIONBIO, &arg);
#endif
}

sock connect_to_camera(int port) {
  // TODO: proper error handling

  const native_socket sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) fatal_error("Failed to create socket\n");

  set_nonblocking_io(sockfd, true);  // for timeout

  sockaddr_in sa = {};
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
#if defined(__unix__)
  inet_pton(AF_INET, server_ipv4_addr, &sa.sin_addr);
#elif defined(_WIN32)
  InetPton(AF_INET, server_ipv4_addr, &sa.sin_addr);
#endif
  connect(sockfd, reinterpret_cast<sockaddr*>(&sa), sizeof(sa));

  // timeout handling
  fd_set fdset;
  FD_ZERO(&fdset);
  FD_SET(sockfd, &fdset);
  struct timeval tv = {};
  tv.tv_sec = 1;
  tv.tv_usec = 0;

  if (select(static_cast<int>(sockfd + 1), NULL, &fdset, NULL, &tv) == 1) {
    int so_error = 0;
    socklen_t len = sizeof so_error;
    getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char*)&so_error, &len);

    if (so_error == 0) {
      printf("Connection esatablished %s:%d (%lld)\n", server_ipv4_addr, port,
             (long long) sockfd);
	  set_nonblocking_io(sockfd, false);
      return sockfd;
    }
  }

  printf("Failed to connect\n");
  close_socket(sockfd);
  
  return 0;
}

size_t to_fuji_size_prefix(size_t sizeBytes) {
{'fraction_non_alphanumeric': 0.15673114883537306, 'fraction_numerical': 0.012633241215949467, 'mean_word_length': 4.495661605206074, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '300205', 'n_tokens_mistral': 2086, 'n_tokens_neox': 2015, 'n_words': 431}