1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#ifndef POTD_SERVER_H #define POTD_SERVER_H 1 #include "socket.h" #define POTD_MAXFD 32 #define POTD_MAXEVENTS 64 typedef struct server_data { void *data; } server_data; typedef struct server_session { void *data; } server_session; typedef int (*on_connect_cb) (struct server_data *data, struct server_session *ses); typedef int (*on_disconnect_cb) (struct server_data *data, struct server_session *ses); typedef int (*on_data_cb) (struct server_data *data, struct server_session *ses); typedef int (*on_free_cb) (struct server_data *data); typedef int (*on_listen_cb) (struct server_data *data); typedef int (*on_shutdown_cb) (struct server_data *data); typedef struct server_callbacks { on_connect_cb on_connect; on_disconnect_cb on_disconnect; on_data_cb mainloop; on_free_cb on_free; on_listen_cb on_listen; on_shutdown_cb on_shutdown; } server_callbacks; typedef struct server_ctx { server_callbacks server_cbs; server_data server_dat; psocket sock; } server_ctx; typedef int (*init_cb) (struct server_ctx *ctx); server_ctx * server_init_ctx(server_ctx *ctx, init_cb init_fn); int server_validate_ctx(const server_ctx *ctx); int server_setup_epoll(const server_ctx ctx[], size_t siz); int server_mainloop_epoll(int epoll_fd, const server_ctx ctx[], size_t siz); #endif