<commit_before>#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#include <netinet/ip.h>
#include <sys/select.h>
#include <sys/socket.h>

#include "executor.h"
#include "input_parser.h"
#include "numeric_utils.h"


#define RECV_BUFSIZE 1638
#define UDP_PORT  4447
#define TCP_PORT 14447
#define TCP_LISTEN_QUEUE_SIZE 10

#define EXECUTOR_HT_SIZE 1024


/***** STATIC METHOD PROTOTYPES *****/
static int setupUDPSocketAndReturnFD();
static int setupTCPSocketAndReturnFD();
static void handleUDPPacket(int udp_fd, VARZExecutor_t *executor);
static void handleNewTCPConnection(int tcp_fd, VARZExecutor_t *executor);
static void sendall(int tcp_fd, void *buf, size_t length, int flags);


/***** IMPLEMENTATION *****/


int main(int argc, char **argv) {
  VARZExecutor_t executor;
  int udp_fd, tcp_fd;
  fd_set readfds;
  udp_fd = setupUDPSocketAndReturnFD();
  tcp_fd = setupTCPSocketAndReturnFD();


  VARZExecutorInit(&executor, EXECUTOR_HT_SIZE);

  while (1) {
    FD_ZERO(&readfds);
    FD_SET(udp_fd, &readfds);
    FD_SET(tcp_fd, &readfds);
    select(MAX(udp_fd, tcp_fd)+1, &readfds, NULL, NULL, NULL);

    if (FD_ISSET(udp_fd, &readfds)) {
      handleUDPPacket(udp_fd, &executor);
    } else if (FD_ISSET(tcp_fd, &readfds)) {
      handleNewTCPConnection(tcp_fd, &executor);
    }
  }

  VARZExecutorFree(&executor);
  return 0;
}


static int setupUDPSocketAndReturnFD() {
  int fd; 
  struct sockaddr_in myaddr;
  int reuse_optval;

  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    fprintf(stderr, "ERROR CANNOT OPEN UDP SOCKET\n");
    exit(1);
  }

  reuse_optval = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_optval, sizeof(reuse_optval)) != 0) {
    fprintf(stderr, "ERROR: Unable to set UDP socket to reuse\n");
  }

  memset(&myaddr, 0, sizeof(struct sockaddr_in));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(UDP_PORT);

  if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
    fprintf(stderr, "ERROR, CANNOT BIND UDP\n");
    exit(1);
  }
  return fd;
}


static int setupTCPSocketAndReturnFD() {
  int fd;
  struct sockaddr_in myaddr;
  int reuse_optval;

  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "ERROR: CANNOT OPEN TCP SOCKET\n");
    exit(1);
  }

  reuse_optval = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_optval, sizeof(reuse_optval)) != 0) {
    fprintf(stderr, "ERROR: Unable to set UDP socket to reuse\n");
  }

  memset(&myaddr, 0, sizeof(struct sockaddr_in));
  myaddr.sin_family = AF_INET;<commit_msg>RCVBUF set to max possible value for UDP<commit_after>#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#include <netinet/ip.h>
#include <sys/select.h>
#include <sys/socket.h>

#include "executor.h"
#include "input_parser.h"
#include "numeric_utils.h"


#define RECV_BUFSIZE 1638
#define UDP_PORT  4447
#define TCP_PORT 14447
#define TCP_LISTEN_QUEUE_SIZE 10

#define EXECUTOR_HT_SIZE 1024
#define DESIRED_UDP_RCV_BUF_SIZE (64*1024*1024) //Why not, let's aim for the stars


/***** STATIC METHOD PROTOTYPES *****/
static int setupUDPSocketAndReturnFD();
static int setupTCPSocketAndReturnFD();
static void handleUDPPacket(int udp_fd, VARZExecutor_t *executor);
static void handleNewTCPConnection(int tcp_fd, VARZExecutor_t *executor);
static void sendall(int tcp_fd, void *buf, size_t length, int flags);


/***** IMPLEMENTATION *****/


int main(int argc, char **argv) {
  VARZExecutor_t executor;
  int udp_fd, tcp_fd;
  fd_set readfds;
  udp_fd = setupUDPSocketAndReturnFD();
  tcp_fd = setupTCPSocketAndReturnFD();


  VARZExecutorInit(&executor, EXECUTOR_HT_SIZE);

  while (1) {
    FD_ZERO(&readfds);
    FD_SET(udp_fd, &readfds);
    FD_SET(tcp_fd, &readfds);
    select(MAX(udp_fd, tcp_fd)+1, &readfds, NULL, NULL, NULL);

    if (FD_ISSET(udp_fd, &readfds)) {
      handleUDPPacket(udp_fd, &executor);
    } else if (FD_ISSET(tcp_fd, &readfds)) {
      handleNewTCPConnection(tcp_fd, &executor);
    }
  }

  VARZExecutorFree(&executor);
  return 0;
}


static int setupUDPSocketAndReturnFD() {
  int fd; 
  struct sockaddr_in myaddr;
  int reuse_optval;
  unsigned int actual_rcv_buf_bytes_size;
  size_t desired_rcv_buf_size, actual_rcv_buf_bytes;

  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    fprintf(stderr, "ERROR CANNOT OPEN UDP SOCKET\n");
    exit(1);
  }

  reuse_optval = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_optval, sizeof(reuse_optval)) != 0) {
    fprintf(stderr, "ERROR: Unable to set UDP socket to reuse\n");
  }

  desired_rcv_buf_size = DESIRED_UDP_RCV_BUF_SIZE;
  actual_rcv_buf_bytes_size = sizeof(actual_rcv_buf_bytes);
  setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &desired_rcv_buf_size, sizeof(desired_rcv_buf_size));
  getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &actual_rcv_buf_bytes, &actual_rcv_buf_bytes_size);
  printf("Setting up UDP socket RCVBUF size = %lu bytes\n", actual_rcv_buf_bytes);

  memset(&myaddr, 0, sizeof(struct sockaddr_in));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(UDP_PORT);

  if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
    fprintf(stderr, "ERROR, CANNOT BIND UDP\n");
    exit(1);
  }
  return fd;
}


static int setupTCPSocketAndReturnFD() {
  int fd;
  struct sockaddr_in myaddr;
  int reuse_optval;

  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "ERROR: CANNOT OPEN TCP SOCKET\n");
    exit(1);
  }

  reuse_optval = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_optval, sizeof(reuse_optval)) != 0) {
    fprintf(stderr, "ERROR: Unable to set UDP socket to reuse\n");
  }

  memset(&myaddr, 0, sizeof(struct sockaddr_in));
  myaddr.sin_family = AF_INET;
{'fraction_non_alphanumeric': 0.12980769230769232, 'fraction_numerical': 0.01407967032967033, 'mean_word_length': 4.825, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, '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': '269259', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2298, 'n_words': 505}