Web/reactor/conn.h
2025-09-15 12:57:54 -05:00

73 lines
2.1 KiB
C

#ifndef CONN_H
#define CONN_H
#include <stddef.h>
#include <stdint.h>
#include "arena.h"
#include "epoll.h"
typedef struct conn conn_t;
typedef struct pool pool_t; // forward declaration
// Connection states
typedef enum {
CONN_IDLE, // in free list
CONN_READING, // reading request
CONN_WRITING, // writing response
CONN_CLOSING // pending close
} conn_state_t;
// HTTP connection
struct conn {
int fd; // socket file descriptor
conn_state_t state; // connection state
// Read buffer
char *rbuf; // read buffer
size_t rsize; // read buffer size
size_t rlen; // bytes in read buffer
size_t rpos; // parse position
// Write buffer
char *wbuf; // write buffer
size_t wsize; // write buffer size
size_t wlen; // bytes in write buffer
size_t wpos; // bytes written
// Request data
arena_t *arena; // per-request arena
char *method; // GET, POST, etc
char *path; // /path/to/resource
char *version; // HTTP/1.1
void *headers; // header storage (TBD)
char *body; // request body
size_t body_len; // body length
// Response building
int status_code; // 200, 404, etc
// Pool management
pool_t *pool; // owning pool
conn_t *next; // for free list
void *user_data; // app-specific data
};
// Connection operations
conn_t* conn_new(pool_t *pool, size_t rbuf_size, size_t wbuf_size, size_t arena_size);
void conn_free(conn_t *c);
void conn_reset(conn_t *c);
void conn_close(conn_t *c);
int conn_accept(pool_t *p, int listen_fd);
// Internal handlers
void conn_read_handler(runtime_t *rt, int fd, uint32_t events, void *data);
void conn_write_handler(runtime_t *rt, int fd, uint32_t events, void *data);
// Response building
int conn_write_status(conn_t *c, int code);
int conn_write_header(conn_t *c, const char *name, const char *value);
int conn_write_body(conn_t *c, const char *data, size_t len);
int conn_end_response(conn_t *c);
#endif // CONN_H