onbsd/include/onbsd.h
2024-11-16 14:37:49 -06:00

57 lines
2.2 KiB
C

#ifndef ONBSD_INCLUDED
#define ONBSD_INCLUDED
#include "exectd.h"
enum { // Events we care about
EV_ERROR, // Error char *error_message
EV_OPEN, // Connection created NULL
EV_POLL, // mg_mgr_poll iteration uint64_t *uptime_millis
EV_CONNECT, // Connection established NULL
EV_ACCEPT, // Connection accepted NULL
EV_READ, // Data received from socket long *bytes_read
EV_WRITE, // Data written to socket long *bytes_written
EV_CLOSE, // Connection closed NULL
EV_HTTP_MSG, // Full HTTP request/response struct mg_http_message *
EV_READY // Epoll interest list is ready.
};
struct Mgr { // Manager for all of our connections.
struct Conn *conns; // List of active connections
};
typedef struct Mgr Mgr;
typedef struct Conn Conn; // Connection forward declaration.
typedef void (*CbFn)(struct Conn *, int ev,void *fn_data);// Conn EV Callback
struct Conn {
struct Conn *next; // Linked list of connections
struct Mgr *mgr; // Our Context Manager
CbFn fn; // Our Connection Callback
void *fn_data; // Callback variable
struct sockaddr_in loc; // Local Address
struct sockaddr_in rem; // Remote Address
int fd; // Main File Descriptor
int epfd; // Epoll File Descriptor
unsigned long id; // Auto-incrementing unique connection ID
int len;
char buf[4096];
struct epoll_event ev; // Events we wait for
struct epoll_event events[MAX_EVENTS];// Event buffer
unsigned is_listening : 1; // Listening Server Connection
unsigned is_client : 1; // Outbound Client Connection
unsigned is_accepted : 1; // Accepted Server/Remote Connection
unsigned is_closing : 1; // Closing connection
unsigned is_working : 1; // Is busy
};
// Mgr and Connections
void init_mgr(Mgr *m);
Conn *mgr_listen(Mgr *m, int port, int interface, CbFn fn, void *fn_data);
Conn *mgr_accept(Conn *c, int fd, SA_IN addr);
Conn *mgr_connect(Mgr *m, CbFn fn, void *fn_data);
void handle_ev(Conn *c, int events);
void mgr_poll(Mgr *m, int timeout);
void use_fd(Mgr *m, Conn *c,int n);
#endif