<commit_before>#include "fnet.h"
#include "flog.h"
#include "fevent.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int set_nonblocking(int fd)
{
    int flags;

    if ((flags = fcntl(fd, F_GETFL)) == -1) {
        LOG_WARN("fcntl(F_GETFL): %s", strerror(errno));
        return -1;
    }
    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
        LOG_WARN("fcntl(F_SETFL, O_NONBLOCK): %s", strerror(errno));
        return -1;
    }
    return 0;
}

int set_socket_option(int fd)
{
    int opt = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
        LOG_WARN("setsockopt SO_REUSEADDR: %s", strerror(errno));
        return -1;   
    }
    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) == -1) {
        LOG_WARN("setsockopt TCP_NODELAY: %s", strerror(errno));
        return -1;
    }

    return 1;
}

int fnet_create_and_bind(const char *addr, const char *port)
{
    struct sockaddr_in sa;

    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0) {
        LOG_WARN("can't create socket: %s", strerror(errno));
    }

    if (set_socket_option(sfd) < 0) {
        LOG_WARN("set socket option error");
    }

    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(atoi(port));
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
    
    if (addr && inet_pton(AF_INET, addr, &sa.sin_addr) != 1) {
        LOG_WARN("invalid bind address");
        close(sfd);
        return -1;
    }

    if (bind(sfd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        LOG_WARN("bind: %s", strerror(errno));
        close(sfd);
        return -1;
    }

    if (set_nonblocking(sfd) < 0) {
        LOG_WARN("set nonblocking error");
        return -1;
    }
    return sfd;
}

int fnet_create_and_connect(const char *addr, const char *port, int blocking)
{
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int listen_fd = 0;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;        /* IPv4 Only */ 
    hints.ai_socktype = SOCK_STREAM;  /* TCP Only */

    int err = getaddrinfo(addr, port, &hints, &result);
    if (err != 0) {
        LOG_WARN("%s:%s getaddrinfo: %s", addr, port, gai_strerror(err));
        return -1;
    }
    for (rp = result; rp != NULL; rp = rp->ai_next) {
        listen_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (listen_fd == -1)
            continue;
        
        if (!blocking) {
            if (set_nonblocking(listen_fd) < 0) {
                close(listen_fd);
                continue;
            }    
        }

        /* 以非阻塞模式 connect，防止阻塞其它请求，超时时间是默认的，大概75s左右,
         * 如果timeout ，则会自动中断 connect 
         */
        if (connect(listen_fd, rp->ai_addr, rp->ai_addrlen) < 0) {
            if (!blocking) {
                if (errno == EHOSTUNREACH) {
                    LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
                    close(listen_fd);
                    continue;
                } else if (errno == EINPROGRESS) {
                    goto done;
                } else {
                    LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
                    close(listen_fd);
                    continue;
                }   
            } else {
                LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
                continue;
            }    
        } else {
            goto done;
        }
    }

    if (rp == NULL) {
        LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
    }

done:
    freeaddrinfo(rp);
    return listen_fd;
}

void close_and_free_client(context *c)
{
    if (c == NULL) {
        return;
    }

    if (c->client_fd != 0) {
        delete_event(c->loop, c->client_fd, EV_WRABLE);
        delete_event(c->loop, c->client_fd, EV_RDABLE);
        close(c->client_fd);
        c->client_fd = 0;
    }

    if (c->client_fd || c->remote_fd) {
        return;<commit_msg>connect 非阻塞不成功返回值错误<commit_after>#include "fnet.h"
#include "flog.h"
#include "fevent.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int set_nonblocking(int fd)
{
    int flags;

    if ((flags = fcntl(fd, F_GETFL)) == -1) {
        LOG_WARN("fcntl(F_GETFL): %s", strerror(errno));
        return -1;
    }
    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
        LOG_WARN("fcntl(F_SETFL, O_NONBLOCK): %s", strerror(errno));
        return -1;
    }
    return 0;
}

int set_socket_option(int fd)
{
    int opt = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
        LOG_WARN("setsockopt SO_REUSEADDR: %s", strerror(errno));
        return -1;   
    }
    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) == -1) {
        LOG_WARN("setsockopt TCP_NODELAY: %s", strerror(errno));
        return -1;
    }

    return 1;
}

int fnet_create_and_bind(const char *addr, const char *port)
{
    struct sockaddr_in sa;

    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0) {
        LOG_WARN("can't create socket: %s", strerror(errno));
    }

    if (set_socket_option(sfd) < 0) {
        LOG_WARN("set socket option error");
    }

    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(atoi(port));
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
    
    if (addr && inet_pton(AF_INET, addr, &sa.sin_addr) != 1) {
        LOG_WARN("invalid bind address");
        close(sfd);
        return -1;
    }

    if (bind(sfd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        LOG_WARN("bind: %s", strerror(errno));
        close(sfd);
        return -1;
    }

    if (set_nonblocking(sfd) < 0) {
        LOG_WARN("set nonblocking error");
        return -1;
    }
    return sfd;
}

int fnet_create_and_connect(const char *addr, const char *port, int blocking)
{
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int listen_fd = 0;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;        /* IPv4 Only */ 
    hints.ai_socktype = SOCK_STREAM;  /* TCP Only */

    int err = getaddrinfo(addr, port, &hints, &result);
    if (err != 0) {
        LOG_WARN("%s:%s getaddrinfo: %s", addr, port, gai_strerror(err));
        return -1;
    }
    for (rp = result; rp != NULL; rp = rp->ai_next) {
        listen_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (listen_fd == -1)
            continue;
        
        if (!blocking) {
            if (set_nonblocking(listen_fd) < 0) {
                close(listen_fd);
                continue;
            }    
        }

        /* 以非阻塞模式 connect，防止阻塞其它请求，超时时间是默认的，大概75s左右,
         * 如果timeout ，则会自动中断 connect 
         */
        if (connect(listen_fd, rp->ai_addr, rp->ai_addrlen) < 0) {
            if (!blocking) {
                if (errno == EHOSTUNREACH) {
                    LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
                    close(listen_fd);
                    continue;
                } else if (errno == EINPROGRESS) {
                    goto end;
                } else {
                    LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
                    close(listen_fd);
                    continue;
                }   
            } else {
                LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
                continue;
            }    
        } else {
            goto end;
        }
    }

    if (rp == NULL) {
        LOG_WARN("connect %s:%s - %s", addr, port, strerror(errno));
        goto done;
    }

done:
    listen_fd = -1;

end:
    freeaddrinfo(rp);
    return listen_fd;
}

void close_and_free_client(context *c)
{
    if (c == NULL) {
        return;
    }

    if (c->client_fd != 0) {
        delete_event(c->loop, c->client_fd, EV_WRABLE);
        delete_event(c->loop, c->client_fd, EV_RDABLE);
        close(c->client_fd);
        c->client_fd = 0;
    }

    if (c->client_fd || c->remote_fd) {
        return;
{'fraction_non_alphanumeric': 0.14829538609806048, 'fraction_numerical': 0.008071316708830262, 'mean_word_length': 1.9820402298850575, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 51, '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': '5569733', 'n_tokens_mistral': 3135, 'n_tokens_neox': 3055, 'n_words': 635}