<commit_before>#include <poll.h>
#include <arpa/inet.h>
#include <assert.h>

#include "event.h"
#include "mtimer.h"
#include "vector.h"
#include "display.h"

typedef enum io_event_kind {
    IO_EVENT_KIND_LISTEN = 1,
    IO_EVENT_KIND_SOCKET,
} io_event_kind_t;

struct io_event {
    io_event_kind_t  kind;
    void            *data;
    union {
        struct {
            on_accept_f on_accept;
        } server;
        struct {
            on_data_f on_read;
            on_data_f on_write;
        } client;
    };
};


VECTOR_T(pollfd, struct pollfd);
VECTOR_T(event, io_event_t *);

vector_pollfd_t  *poll_sck;
vector_event_t   *events;

void * event_get_data(io_event_t *ev)
{
    return ev->data;
}

void event_set_data(io_event_t *ev, void *data)
{
    ev->data = data;
}


void event_init(void)
{
    poll_sck = vector_pollfd_new();
    events   = vector_event_new();
}

io_event_t * event_server_add(uint16_t port, on_accept_f on_accept, void *data)
{
    io_event_t          *event;
    int                  s;
    struct sockaddr_in   addr;
    int                  opt = 1;

    if(on_accept == NULL)
        return NULL;

    addr = (struct sockaddr_in) {
        .sin_family      = AF_INET,
        .sin_port        = htons(port),
        .sin_addr.s_addr = htonl(INADDR_ANY),
    };
    s    = socket (AF_INET, SOCK_STREAM, 0);

    if(bind (s, (struct sockaddr *) &addr, sizeof (addr)) == -1)
        return NULL;

    if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt)) == -1)
        return NULL;

    if(listen(s, 5) == -1)
        return NULL;

    event = (io_event_t *) malloc(sizeof(io_event_t));

    event->kind             = IO_EVENT_KIND_LISTEN;
    event->server.on_accept = on_accept;
    event->data             = data;

    vector_pollfd_push(poll_sck, &((struct pollfd) {
                .fd      = s,
                .events  = POLLIN,
                .revents = 0 }));

    vector_event_push(events, &event);

    return event;
}

io_event_t * event_client_add(int sck, void *data)
{
    io_event_t          *event;

    event = (io_event_t *) malloc(sizeof(io_event_t));

    event->kind             = IO_EVENT_KIND_SOCKET;
    event->client.on_read   = NULL;
    event->client.on_write  = NULL;
    event->data             = data;

    vector_pollfd_push(poll_sck, &((struct pollfd) {
                .fd      = sck,
                .events  = 0,
                .revents = 0 }));

    vector_event_push(events, &event);

    return event;
}

int event_client_set_on_read(io_event_t *ev, on_data_f on_read)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(on_read  == NULL)
        return -1;

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = VECTOR_GET_INDEX(events, &ev);

    ev->client.on_read = on_read;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events |= POLLIN;

    return 0;
}

int event_client_clr_on_read(io_event_t *ev)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = VECTOR_GET_INDEX(events, &ev);

    ev->client.on_read = NULL;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events &= ~POLLIN;

    return 0;
}

int event_client_set_on_write(io_event_t *ev, on_data_f on_write)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(on_write == NULL)
        return -1;

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = VECTOR_GET_INDEX(events, &ev);

    ev->client.on_write = on_write;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events |= POLLOUT;

    return 0;
}

int event_client_clr_on_write(io_event_t *ev)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = VECTOR_GET_INDEX(events, &ev);

    ev->client.on_write = NULL;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events &= ~POLLIN;

    return 0;
}

void event_delete(io_event_t *ev)
{
    io_event_t **v;
    int          i;

    VECTOR_EACH(events, v) {
        if(*v == ev) {
            i = VECTOR_GET_INDEX(events, v);
            free(ev);
            vector_pollfd_delete_by_index(poll_sck, i);
            vector_event_delete_by_index(events, i);
            break;
        }
    }
}

void event_loop(void)
{
    int64_t              diff;
    struct timeval       now;
    struct pollfd       *v;
    struct sockaddr_in   addr;
    int                  addrlen =  sizeof (struct sockaddr_in);
    io_event_t          *e;
    int                  sck;
<commit_msg>[event] fix research and out flag<commit_after>#include <poll.h>
#include <arpa/inet.h>
#include <assert.h>

#include "event.h"
#include "mtimer.h"
#include "vector.h"
#include "display.h"

typedef enum io_event_kind {
    IO_EVENT_KIND_LISTEN = 1,
    IO_EVENT_KIND_SOCKET,
} io_event_kind_t;

struct io_event {
    io_event_kind_t  kind;
    void            *data;
    union {
        struct {
            on_accept_f on_accept;
        } server;
        struct {
            on_data_f on_read;
            on_data_f on_write;
        } client;
    };
};


VECTOR_T(pollfd, struct pollfd);
VECTOR_T(event, io_event_t *);

vector_pollfd_t  *poll_sck;
vector_event_t   *events;

void * event_get_data(io_event_t *ev)
{
    return ev->data;
}

void event_set_data(io_event_t *ev, void *data)
{
    ev->data = data;
}


void event_init(void)
{
    poll_sck = vector_pollfd_new();
    events   = vector_event_new();
}

io_event_t * event_server_add(uint16_t port, on_accept_f on_accept, void *data)
{
    io_event_t          *event;
    int                  s;
    struct sockaddr_in   addr;
    int                  opt = 1;

    if(on_accept == NULL)
        return NULL;

    addr = (struct sockaddr_in) {
        .sin_family      = AF_INET,
        .sin_port        = htons(port),
        .sin_addr.s_addr = htonl(INADDR_ANY),
    };
    s    = socket (AF_INET, SOCK_STREAM, 0);

    if(bind (s, (struct sockaddr *) &addr, sizeof (addr)) == -1)
        return NULL;

    if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt)) == -1)
        return NULL;

    if(listen(s, 5) == -1)
        return NULL;

    event = (io_event_t *) malloc(sizeof(io_event_t));

    event->kind             = IO_EVENT_KIND_LISTEN;
    event->server.on_accept = on_accept;
    event->data             = data;

    vector_pollfd_push(poll_sck, &((struct pollfd) {
                .fd      = s,
                .events  = POLLIN,
                .revents = 0 }));

    vector_event_push(events, &event);

    return event;
}

io_event_t * event_client_add(int sck, void *data)
{
    io_event_t          *event;

    event = (io_event_t *) malloc(sizeof(io_event_t));

    event->kind             = IO_EVENT_KIND_SOCKET;
    event->client.on_read   = NULL;
    event->client.on_write  = NULL;
    event->data             = data;

    vector_pollfd_push(poll_sck, &((struct pollfd) {
                .fd      = sck,
                .events  = 0,
                .revents = 0 }));

    vector_event_push(events, &event);

    return event;
}

static int event_search(io_event_t *ev)
{
    io_event_t **v;

    VECTOR_EACH(events, v) {
        if(*v == ev) {
            return VECTOR_GET_INDEX(events, v);
        }
    }
    return -1;
}

int event_client_set_on_read(io_event_t *ev, on_data_f on_read)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(on_read  == NULL)
        return -1;

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = event_search(ev);

    ev->client.on_read = on_read;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events |= POLLIN;

    return 0;
}

int event_client_clr_on_read(io_event_t *ev)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = event_search(ev);

    ev->client.on_read = NULL;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events &= ~POLLIN;

    return 0;
}

int event_client_set_on_write(io_event_t *ev, on_data_f on_write)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(on_write == NULL)
        return -1;

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = event_search(ev);

    ev->client.on_write = on_write;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events |= POLLOUT;

    return 0;
}

int event_client_clr_on_write(io_event_t *ev)
{
    int i;

    assert(ev);
    assert(ev->kind == IO_EVENT_KIND_SOCKET);

    if(ev->kind != IO_EVENT_KIND_SOCKET)
        return -1;

    i = event_search(ev);

    ev->client.on_write = NULL;

    VECTOR_GET_BY_INDEX(poll_sck, i)->events &= ~POLLOUT;

    return 0;
}

void event_delete(io_event_t *ev)
{
    io_event_t **v;
    int          i;

    VECTOR_EACH(events, v) {
        if(*v == ev) {
            i = VECTOR_GET_INDEX(events, v);
            free(ev);
            vector_pollfd_delete_by_index(poll_sck, i);
            vector_event_delete_by_index(events, i);
            break;
        }
    }
}

void event_loop(void)
{
    int64_t              diff;
    struct timeval       now;
    struct pollfd       *v;
    struct sockaddr_in   addr;
    int                  addrlen =  sizeof (struct sockaddr_in);
    io_event_t          *e;
    int                  sck;

{'fraction_non_alphanumeric': 0.11788705175005368, 'fraction_numerical': 0.0052608975735452005, 'mean_word_length': 2.156557099288377, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 59, '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': '3028205', 'n_tokens_mistral': 3646, 'n_tokens_neox': 3506, 'n_words': 650}