From e339f2d269fcaffed7beed67c3995fe3b67393eb Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Sat, 17 Dec 2022 21:24:04 +0800 Subject: Support on_connect function in server lib --- src/server.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/server.c') diff --git a/src/server.c b/src/server.c index a13d525..e4a2182 100644 --- a/src/server.c +++ b/src/server.c @@ -22,6 +22,7 @@ struct server { tpool_t *tpool; evloop_t *evloop; evloop_t **subevloops; + conn_acceptor_t *acceptor; }; server_t *server_create(void) @@ -29,6 +30,7 @@ server_t *server_create(void) int nprocs = get_nprocs(); server_t *serv = (server_t *)malloc(sizeof(*serv)); tsocket_t *sock; + conn_acceptor_t *acceptor; event_t *acceptor_event; if (!(sock = tsocket_create()) @@ -46,7 +48,9 @@ server_t *server_create(void) for (int i = 0; i < nprocs; i++) serv->subevloops[i] = evloop_create(); - if (!(acceptor_event = conn_acceptor_event_create(sock, serv->subevloops, nprocs))) + serv->acceptor = conn_acceptor_create(sock, serv->subevloops, nprocs); + + if (!(acceptor_event = conn_acceptor_event_create(serv->acceptor))) return NULL; if (evloop_add(serv->evloop, acceptor_event, EPOLLIN) == -1) panic("evloop add fd"); @@ -67,6 +71,7 @@ void server_destroy(server_t *serv) evloop_destroy(serv->evloop); tpool_destroy(serv->tpool); + conn_acceptor_destroy(serv->acceptor); for (int i = 0; i < nprocs; i++) evloop_destroy(serv->subevloops[i]); @@ -75,13 +80,12 @@ void server_destroy(server_t *serv) free(serv); } -void server_run(server_t *serv) +void server_on_connect(server_t *serv, connection_callback_func_t func) { - evloop_loop(serv->evloop); + conn_acceptor_on_connect(serv->acceptor, func); } -int main() +void server_run(server_t *serv) { - server_t *serv = server_create(); - server_run(serv); + evloop_loop(serv->evloop); } -- cgit v1.2.3