lander/include/event_loop.h

88 lines
1.9 KiB
C
Raw Normal View History

#ifndef LANDER_EVENT_LOOP
#define LANDER_EVENT_LOOP
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "http_req.h"
// Size of the read and write buffers for each connection, in bytes
#define EVENT_LOOP_BUFFER_SIZE 1024
/**
* Represents an active connection managed by the event loop
*/
typedef struct event_loop_conn event_loop_conn;
typedef enum {
event_loop_conn_state_req = 0,
event_loop_conn_state_res = 1,
event_loop_conn_state_end = 2,
} event_loop_conn_state;
/*
* Main struct object representing the event loop
*/
typedef struct event_loop event_loop;
/*
* Initialize a new event loop
*/
event_loop *event_loop_init();
/*
* Run the event loop. This function never returns.
*/
void event_loop_run(event_loop *el, int port);
typedef struct event_loop_conn {
int fd;
event_loop_conn_state state;
// buffer for reading
size_t rbuf_size;
size_t rbuf_read;
uint8_t rbuf[EVENT_LOOP_BUFFER_SIZE];
// buffer for writing
size_t wbuf_size;
size_t wbuf_sent;
uint8_t wbuf[EVENT_LOOP_BUFFER_SIZE];
// If true, the server will close the connection after the final write buffer
// has been written
bool close_after_write;
/* void (*process_func)(struct event_loop_conn *); */
http_request req;
} event_loop_conn;
/*
* Initialize a new event_loop_conn struct
*/
event_loop_conn *event_loop_conn_init();
typedef struct event_loop {
event_loop_conn **connections;
size_t connection_count;
} event_loop;
/*
* Initialize a new event_loop struct
*/
event_loop *event_loop_init();
/*
* Place a new connection into the event loop's internal array.
*
* Returns -1 if the internal realloc failed
*/
int event_loop_put(event_loop *loop, event_loop_conn *conn);
/**
* Accept a new connection for the given file descriptor.
*/
int event_loop_accept(event_loop *loop, int fd);
void event_loop_conn_io(event_loop_conn *conn);
#endif