<commit_before>#include "arch/linux/network.hpp"
#include "arch/linux/thread_pool.hpp"
#include "concurrency/cond_var.hpp"   // For promise_t
#include "logger.hpp"
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

/* Actual definition of bool_promise_t */

struct bool_promise_t : public promise_t<bool> {
};

/* Network connection object */

linux_tcp_conn_t::linux_tcp_conn_t(const char *host, int port)
    : registration_thread(-1),
      read_cond(NULL), write_cond(NULL),
      read_was_shut_down(false), write_was_shut_down(false) {
    struct addrinfo *res;

    /* make a sacrifice to the elders honor by converting port to a string, why
     * can't we just sacrifice a virgin for them (lord knows we have enough
     * virgins in Silicon Valley) */
    char port_str[10]; /* god is it dumb that we have to do this */
    snprintf(port_str, 10, "%d", port);
    //fail_due_to_user_error("Port is too big", (snprintf(port_str, 10, "%d", port) == 10));

    /* make the connection */
    if (getaddrinfo(host, port_str, NULL, &res) != 0) {
        logERR("Failed to look up address %s:%d.\n", host, port);
        goto ERROR_BREAKOUT;
    }
    if((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
        logERR("Failed to create a socket\n");
        goto ERROR_BREAKOUT;
    }
    if (connect(sock, res->ai_addr, res->ai_addrlen) != 0) {
        /* for some reason the connection failed */
        logERR("Failed to make a connection with error: %s\n", strerror(errno));
        goto ERROR_BREAKOUT;
    }

    guarantee_err(fcntl(sock, F_SETFL, O_NONBLOCK) == 0, "Could not make socket non-blocking");

    freeaddrinfo(res);
    return;

ERROR_BREAKOUT:
    freeaddrinfo(res);
    throw connect_failed_exc_t();
}

linux_tcp_conn_t::linux_tcp_conn_t(const ip_address_t &host, int port)
    : registration_thread(-1),
      read_cond(NULL), write_cond(NULL),
      read_was_shut_down(false), write_was_shut_down(false) {
    sock = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr = host.addr;
    bzero(addr.sin_zero, sizeof(addr.sin_zero));

    if (connect(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) != 0) {
        /* for some reason the connection failed */
        logINF("Failed to make a connection with error: %s\n", strerror(errno));
        throw connect_failed_exc_t();
    }
}

linux_tcp_conn_t::linux_tcp_conn_t(fd_t sock) :
    sock(sock),
    registration_thread(-1),
    read_cond(NULL), write_cond(NULL),
    read_was_shut_down(false), write_was_shut_down(false)
{<commit_msg>Fixed bug where tcp_conn_t was not making its underlying socket nonblocking.<commit_after>#include "arch/linux/network.hpp"
#include "arch/linux/thread_pool.hpp"
#include "concurrency/cond_var.hpp"   // For promise_t
#include "logger.hpp"
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

/* Actual definition of bool_promise_t */

struct bool_promise_t : public promise_t<bool> {
};

/* Network connection object */

linux_tcp_conn_t::linux_tcp_conn_t(const char *host, int port)
    : registration_thread(-1),
      read_cond(NULL), write_cond(NULL),
      read_was_shut_down(false), write_was_shut_down(false) {
    struct addrinfo *res;

    /* make a sacrifice to the elders honor by converting port to a string, why
     * can't we just sacrifice a virgin for them (lord knows we have enough
     * virgins in Silicon Valley) */
    char port_str[10]; /* god is it dumb that we have to do this */
    snprintf(port_str, 10, "%d", port);
    //fail_due_to_user_error("Port is too big", (snprintf(port_str, 10, "%d", port) == 10));

    /* make the connection */
    if (getaddrinfo(host, port_str, NULL, &res) != 0) {
        logERR("Failed to look up address %s:%d.\n", host, port);
        goto ERROR_BREAKOUT;
    }
    if((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
        logERR("Failed to create a socket\n");
        goto ERROR_BREAKOUT;
    }
    if (connect(sock, res->ai_addr, res->ai_addrlen) != 0) {
        /* for some reason the connection failed */
        logERR("Failed to make a connection with error: %s\n", strerror(errno));
        goto ERROR_BREAKOUT;
    }

    guarantee_err(fcntl(sock, F_SETFL, O_NONBLOCK) == 0, "Could not make socket non-blocking");

    freeaddrinfo(res);
    return;

ERROR_BREAKOUT:
    freeaddrinfo(res);
    throw connect_failed_exc_t();
}

linux_tcp_conn_t::linux_tcp_conn_t(const ip_address_t &host, int port)
    : registration_thread(-1),
      read_cond(NULL), write_cond(NULL),
      read_was_shut_down(false), write_was_shut_down(false) {
    sock = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr = host.addr;
    bzero(addr.sin_zero, sizeof(addr.sin_zero));

    if (connect(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) != 0) {
        /* for some reason the connection failed */
        logINF("Failed to make a connection with error: %s\n", strerror(errno));
        throw connect_failed_exc_t();
    }

    guarantee_err(fcntl(sock, F_SETFL, O_NONBLOCK) == 0, "Could not make socket non-blocking");
}

linux_tcp_conn_t::linux_tcp_conn_t(fd_t sock) :
    sock(sock),
    registration_thread(-1),
    read_cond(NULL), write_cond(NULL),
    read_was_shut_down(false), write_was_shut_down(false)
{
{'fraction_non_alphanumeric': 0.13127413127413126, 'fraction_numerical': 0.006142506142506142, 'mean_word_length': 3.7333887043189367, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, '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': '2979952', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1963, 'n_words': 479}