<commit_before>#include <endian.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>

#include "ur_modern_driver/log.h"
#include "ur_modern_driver/tcp_socket.h"

TCPSocket::TCPSocket()
  : socket_fd_(-1)
  , state_(SocketState::Invalid)
{ 
}
TCPSocket::~TCPSocket()
{
  close();
}

void TCPSocket::setOptions(int socket_fd)
{
  int flag = 1;
  setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
}

bool TCPSocket::setup(std::string &host, int port)
{
  if(state_ == SocketState::Connected)
    return false;

  LOG_INFO("Setting up connection: %s:%d", host.c_str(), port);

  // gethostbyname() is deprecated so use getadderinfo() as described in:
  // http://www.beej.us/guide/bgnet/output/html/multipage/syscalls.html#getaddrinfo<commit_msg>socket improvment<commit_after>#include <endian.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>

#include "ur_modern_driver/log.h"
#include "ur_modern_driver/tcp_socket.h"

TCPSocket::TCPSocket()
  : socket_fd_(-1)
  , state_(SocketState::Invalid)
{ 
}
TCPSocket::~TCPSocket()
{
  close();
}

void TCPSocket::setOptions(int socket_fd)
{
  int flag = 1;
  setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  setsockopt(socket_fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(int));
}

bool TCPSocket::setup(std::string &host, int port)
{
  if(state_ == SocketState::Connected)
    return false;

  LOG_INFO("Setting up connection: %s:%d", host.c_str(), port);

  // gethostbyname() is deprecated so use getadderinfo() as described in:
  // http://www.beej.us/guide/bgnet/output/html/multipage/syscalls.html#getaddrinfo
{'fraction_non_alphanumeric': 0.16028708133971292, 'fraction_numerical': 0.0023923444976076554, 'mean_word_length': 5.56078431372549, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3782720', 'n_tokens_mistral': 648, 'n_tokens_neox': 649, 'n_words': 115}