feat: decouple event loop from http
This commit is contained in:
parent
f3fced908f
commit
68f9227436
12 changed files with 136 additions and 112 deletions
|
|
@ -5,27 +5,9 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "http_req.h"
|
||||
#include "trie.h"
|
||||
|
||||
// Size of the read and write buffers for each connection, in bytes
|
||||
#define EVENT_LOOP_BUFFER_SIZE 1024
|
||||
|
||||
typedef struct event_loop_gctx {
|
||||
Trie *trie;
|
||||
} event_loop_gctx;
|
||||
|
||||
event_loop_gctx *event_loop_gctx_init();
|
||||
|
||||
typedef struct event_loop_ctx {
|
||||
http_request req;
|
||||
event_loop_gctx *g;
|
||||
} event_loop_ctx;
|
||||
|
||||
event_loop_ctx *event_loop_ctx_init(event_loop_gctx *g);
|
||||
|
||||
void event_loop_ctx_free(event_loop_ctx *ctx);
|
||||
|
||||
/**
|
||||
* Represents an active connection managed by the event loop
|
||||
*/
|
||||
|
|
@ -50,46 +32,52 @@ typedef struct event_loop_conn {
|
|||
// If true, the server will close the connection after the final write buffer
|
||||
// has been written
|
||||
bool close_after_write;
|
||||
event_loop_ctx *ctx;
|
||||
void *ctx;
|
||||
} event_loop_conn;
|
||||
|
||||
/*
|
||||
* Initialize a new event_loop_conn struct
|
||||
*/
|
||||
event_loop_conn *event_loop_conn_init(event_loop_gctx *g);
|
||||
|
||||
void event_loop_conn_free(event_loop_conn *conn);
|
||||
|
||||
/*
|
||||
* Process data on a connection
|
||||
*/
|
||||
void event_loop_conn_io(event_loop_conn *conn);
|
||||
|
||||
/*
|
||||
* Main struct object representing the event loop
|
||||
*/
|
||||
typedef struct event_loop {
|
||||
event_loop_conn **connections;
|
||||
size_t connection_count;
|
||||
event_loop_gctx *gctx;
|
||||
// Global context passed to every connection
|
||||
void *gctx;
|
||||
// Function to initialize a context for a connection
|
||||
void *(*ctx_init)(void *gctx);
|
||||
// Function to free a context for a connection
|
||||
void (*ctx_free)(void *ctx);
|
||||
bool (*handle_data)(event_loop_conn *conn);
|
||||
} event_loop;
|
||||
|
||||
/*
|
||||
* Initialize a new event_loop_conn struct
|
||||
*/
|
||||
event_loop_conn *event_loop_conn_init(event_loop *el);
|
||||
|
||||
void event_loop_conn_free(event_loop *el, event_loop_conn *conn);
|
||||
|
||||
/*
|
||||
* Process data on a connection
|
||||
*/
|
||||
void event_loop_conn_io(event_loop *el, event_loop_conn *conn);
|
||||
|
||||
/*
|
||||
* Initialize a new event_loop struct
|
||||
*/
|
||||
event_loop *event_loop_init(event_loop_gctx *g);
|
||||
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);
|
||||
int event_loop_put(event_loop *el, event_loop_conn *conn);
|
||||
|
||||
/**
|
||||
* Accept a new connection for the given file descriptor.
|
||||
*/
|
||||
int event_loop_accept(event_loop *loop, int fd);
|
||||
int event_loop_accept(event_loop *el, int fd);
|
||||
|
||||
/*
|
||||
* Run the event loop. This function never returns.
|
||||
|
|
|
|||
33
include/http_loop.h
Normal file
33
include/http_loop.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef LANDER_HTTP_LOOP
|
||||
#define LANDER_HTTP_LOOP
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "http_req.h"
|
||||
#include "trie.h"
|
||||
|
||||
/*
|
||||
* Global context passed to every connection using the same pointer
|
||||
*/
|
||||
typedef struct http_loop_gctx {
|
||||
Trie *trie;
|
||||
} http_loop_gctx;
|
||||
|
||||
http_loop_gctx *http_loop_gctx_init();
|
||||
|
||||
/*
|
||||
* Invidivual context initialized for every connection
|
||||
*/
|
||||
typedef struct http_loop_ctx {
|
||||
http_request req;
|
||||
http_loop_gctx *g;
|
||||
} http_loop_ctx;
|
||||
|
||||
http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g);
|
||||
|
||||
void http_loop_ctx_free(http_loop_ctx *ctx);
|
||||
|
||||
bool http_loop_handle_request(event_loop_conn *conn);
|
||||
|
||||
event_loop *http_loop_init(http_loop_gctx *gctx);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue