<commit_before>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <cstring>

#include "Socket.h"

Socket::Socket()
{
  _socketDescriptor = -1;
}

Socket::~Socket()
{
}

bool Socket::start(int port)
{
  int status;

  struct sockaddr_in serverAddress;
  std::memset(&serverAddress, 0, sizeof(serverAddress));
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_addr.s_addr = INADDR_ANY;  //TODO: set this properly
  serverAddress.sin_port = htons(port);

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tCreating socket" << std::endl;
  _socketDescriptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(_socketDescriptor < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError while opening socket: " << _socketDescriptor << std::endl;
    return false;
  }

  int on = 1;
  status = setsockopt(_socketDescriptor, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError setting socket options: " << status << std::endl;
    return false;
  }


  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tBinding socket" << std::endl;
  status = bind(_socketDescriptor, (struct sockaddr *) &serverAddress, sizeof(serverAddress));
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError binding socket to hostname: " << status << std::endl;
    return false;
  }

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tBeginning socket listen" << std::endl;
  status = listen(_socketDescriptor, 5);
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError listening socket: " << status << std::endl;
    return false;
  }

  return true;
}

bool Socket::accept(Socket &clientConnection)
{
  struct sockaddr_in clientAddress;
  socklen_t clientAddressSize = sizeof(clientAddress);

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tBeginning connection accept" << std::endl;
  clientConnection._socketDescriptor = ::accept(_socketDescriptor, (struct sockaddr *) &clientAddress, &clientAddressSize);
  if(clientConnection._socketDescriptor < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError while accepting connection: " 
	                                         << clientConnection._socketDescriptor << std::endl;
    return false;
  }
  
  return true;
}

bool Socket::close()
{
  ::close(_socketDescriptor);
  return true;
}

bool Socket::send(std::string str)
{
  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tSending data" << std::endl;

  uint32_t header = htonl((uint32_t)str.size());
  unsigned char headerArray[sizeof(header)];
  std::memcpy(headerArray, &header, sizeof(header));

  if(::send(_socketDescriptor, headerArray, sizeof(headerArray), MSG_NOSIGNAL) >= 0) {
    return ( ::send(_socketDescriptor, str.c_str(), str.size(), MSG_NOSIGNAL) >= 0 );
  }

  return false;
}

int Socket::recv(std::string &str)
{
  int retval = 0;
  str = "";

  int bufferSize = 64*1024;
  char buffer[bufferSize + 1];
  std::memset(buffer, 0, bufferSize + 1);

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tReceiving data" << std::endl;
  retval = ::recv(_socketDescriptor, buffer, bufferSize, 0);
  if(retval < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError while recieving -- more than likely, this was an immediate disconnect from the client" << std::endl;
    return 0;
  }

  str = buffer;
  return retval;
}

<commit_msg>Setting timeout on socket::recv to prevent hangs in server<commit_after>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <cstring>

#include "Socket.h"

Socket::Socket()
{
  _socketDescriptor = -1;
}

Socket::~Socket()
{
}

bool Socket::start(int port)
{
  int status;

  struct sockaddr_in serverAddress;
  std::memset(&serverAddress, 0, sizeof(serverAddress));
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_addr.s_addr = INADDR_ANY;  //TODO: set this properly
  serverAddress.sin_port = htons(port);

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tCreating socket" << std::endl;
  _socketDescriptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if(_socketDescriptor < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError while opening socket: " << _socketDescriptor << std::endl;
    return false;
  }

  int on = 1;
  status = setsockopt(_socketDescriptor, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError setting socket options: " << status << std::endl;
    return false;
  }
  
  struct timeval tv;
  tv.tv_sec = 10;
  status = setsockopt(_socketDescriptor, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError setting socket timeout options: " << status << std::endl;
    return false;
  }

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tBinding socket" << std::endl;
  status = bind(_socketDescriptor, (struct sockaddr *) &serverAddress, sizeof(serverAddress));
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError binding socket to hostname: " << status << std::endl;
    return false;
  }

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tBeginning socket listen" << std::endl;
  status = listen(_socketDescriptor, 5);
  if(status < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError listening socket: " << status << std::endl;
    return false;
  }

  return true;
}

bool Socket::accept(Socket &clientConnection)
{
  struct sockaddr_in clientAddress;
  socklen_t clientAddressSize = sizeof(clientAddress);

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tBeginning connection accept" << std::endl;
  clientConnection._socketDescriptor = ::accept(_socketDescriptor, (struct sockaddr *) &clientAddress, &clientAddressSize);
  if(clientConnection._socketDescriptor < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError while accepting connection: " 
	                                         << clientConnection._socketDescriptor << std::endl;
    return false;
  }
  
  return true;
}

bool Socket::close()
{
  ::close(_socketDescriptor);
  return true;
}

bool Socket::send(std::string str)
{
  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tSending data" << std::endl;

  uint32_t header = htonl((uint32_t)str.size());
  unsigned char headerArray[sizeof(header)];
  std::memcpy(headerArray, &header, sizeof(header));

  if(::send(_socketDescriptor, headerArray, sizeof(headerArray), MSG_NOSIGNAL) >= 0) {
    return ( ::send(_socketDescriptor, str.c_str(), str.size(), MSG_NOSIGNAL) >= 0 );
  }

  return false;
}

int Socket::recv(std::string &str)
{
  int retval = 0;
  str = "";

  int bufferSize = 64*1024;
  char buffer[bufferSize + 1];
  std::memset(buffer, 0, bufferSize + 1);

  std::cerr << __FILE__ << ":" << __LINE__ << "\t\tReceiving data" << std::endl;
  retval = ::recv(_socketDescriptor, buffer, bufferSize, 0);
  if(retval < 0) {
    std::cerr << __FILE__ << ":" << __LINE__ << "\t\tError while recieving -- more than likely, this was an immediate disconnect from the client" << std::endl;
    return 0;
  }

  str = buffer;
  return retval;
}
{'fraction_non_alphanumeric': 0.17018700390152025, 'fraction_numerical': 0.007937575676039284, 'mean_word_length': 4.116311080523055, 'pattern_counts': {'":': 25, '<': 308, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1420539', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2514, 'n_words': 576}