<commit_before>/* based on code from http://gnosis.cx/publish/programming/sockets.html */

/* bulkserver - forked from server.c */

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/sendfile.h>
#include <fcntl.h>

#define MAXPENDING 5    /* Max connection requests */
#define BUFFSIZE 32
#define BLOCKSIZE 0x10000
#define ZEROCOPY
void Die(char *mess) { perror(mess); exit(1); }

void HandleClient(int sock) {
  char buffer[BUFFSIZE];
  int received = -1;
  long long int respsize,sendsize;
  char* blockptr = malloc(BLOCKSIZE);
  memset(blockptr,0xaa,BLOCKSIZE);

  /* Receive message */
  if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) {
    Die("Failed to receive initial bytes from client");
  }
  /* get the client request - for now, just expect a number which is the size of the
     expected response message */
  respsize = strtoll(buffer, NULL,10);
  if (errno) {
      fprintf(stderr, "client request (\"%s\")",buffer);
      Die("Failed to parse client request");
    }
  /* Send bytes and check for more incoming data in loop */
#ifdef ZEROCOPY

  char tmpn[] = "/tmp/XXXXXX" ; 
  int   tmpfd = mkstemp(tmpn);
  ftruncate(tmpfd,respsize);
  long long int sent_bytes = sendfile(sock, tmpfd, 0, respsize);
  if (sent_bytes != respsize) {
    fprintf(stderr,"Failed to send bytes to client (%qd/%qd)\n",sent_bytes,respsize);
    if (errno) {
      perror(NULL);
    }
    exit(1);
  }
  close(tmpfd);
  unlink(tmpn);
  <commit_msg>fix limit on single write size (MAX_RW_COUNT)<commit_after>/* based on code from http://gnosis.cx/publish/programming/sockets.html */

/* bulkserver - forked from server.c */

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/sendfile.h>
#include <fcntl.h>

#define MAXPENDING 5    /* Max connection requests */
#define BUFFSIZE 32
#define BLOCKSIZE 0x10000
#define ZEROCOPY
/* MAX_RW_COUNT is defined in the linux headers in fs.h and is INT_MAX & PAGE_CACHE_MASK = 2 * 1024^3 - 4096 */
#define MAX_RW_COUNT 2147479552
void Die(char *mess) { perror(mess); exit(1); }

void HandleClient(int sock) {
  char buffer[BUFFSIZE];
  int received = -1;
  long long int respsize,sendsize;
  char* blockptr = malloc(BLOCKSIZE);
  memset(blockptr,0xaa,BLOCKSIZE);

  /* Receive message */
  if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) {
    Die("Failed to receive initial bytes from client");
  }
  /* get the client request - for now, just expect a number which is the size of the
     expected response message */
  respsize = strtoll(buffer, NULL,10);
  if (errno) {
      fprintf(stderr, "client request (\"%s\")",buffer);
      Die("Failed to parse client request");
    }
  /* Send bytes and check for more incoming data in loop */
#ifdef ZEROCOPY

  char tmpn[] = "/tmp/XXXXXX" ; 
  int   tmpfd = mkstemp(tmpn);
  ftruncate(tmpfd,respsize);

  while (respsize > 0) {
    sendsize = respsize > MAX_RW_COUNT ? MAX_RW_COUNT : respsize;
    respsize -= sendsize;
    /* Send the next block */
    if (sendfile(sock, tmpfd, 0, sendsize) != sendsize) {
      Die("Failed to send bytes to client");
    }
  }

  close(tmpfd);
  unlink(tmpn);
  
{'fraction_non_alphanumeric': 0.13740685543964232, 'fraction_numerical': 0.01669150521609538, 'mean_word_length': 3.6481994459833795, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '4571520', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1189, 'n_words': 343}