From 49839c88a98d3798f7b18c58f54f26f36cacff38 Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 9 Dec 2022 16:46:49 +0800 Subject: Implement a simple thread pool and refactor Refactor --- acceptor.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'acceptor.c') diff --git a/acceptor.c b/acceptor.c index bb4fb88..1f28fcc 100644 --- a/acceptor.c +++ b/acceptor.c @@ -1,51 +1,58 @@ #include #include #include +#include #include "acceptor.h" #include "util.h" #include "connection.h" +#include "evloop.h" -int connAcceptorAccept(struct connAcceptor *ca) +struct conn_acceptor { + struct tsocket *sock; + evloop_t *el; +}; +typedef struct conn_acceptor conn_acceptor_t; + +static void conn_acceptor_accept(conn_acceptor_t *ca) { - struct tsocket *conn_sock = tsocketAccept(ca->sock); + struct tsocket *conn_sock = tsocket_accept(ca->sock); if (conn_sock == NULL) { perror("socket accept"); - return 0; + return; } if (setblocking(conn_sock->fd, false) == -1) { perror("setblocking"); - tsocketDelete(conn_sock); - return 0; + tsocket_destroy(conn_sock); + return; } - struct connection *conn = connectionNew(conn_sock); - struct event *conn_ev = connectionNewEvent(conn); + event_t *conn_ev = connection_create_event(conn_sock); - if (eventLoopAdd(ca->el, conn_ev, EPOLLIN | EPOLLET) == -1) { + if (evloop_add(ca->el, conn_ev, EPOLLIN | EPOLLET) == -1) { perror("eventloop add fd: conn_sock"); - return 0; + return; } printf("New client fd %d, ip: %s, port: %d\n", conn_sock->fd, conn_sock->addr, conn_sock->port); - - return 0; } -void connAcceptorDel(struct connAcceptor *ca) +static void conn_acceptor_destroy(conn_acceptor_t *ca) { - tsocketDelete(ca->sock); + tsocket_destroy(ca->sock); free(ca); } -struct event *connAcceptorNewEvent(struct tsocket *sock, struct eventLoop *el) +event_t *conn_acceptor_create_event(struct tsocket *sock, evloop_t *el) { - struct connAcceptor *ca = malloc(sizeof(*ca)); + conn_acceptor_t *ca = malloc(sizeof(*ca)); ca->sock = sock; ca->el = el; - struct event *ev = eventNew(ca, sock->fd, connAcceptorAccept, connectionDel); + struct event *ev = event_create(ca, sock->fd, + (evloop_process_func_t) conn_acceptor_accept, + (evloop_destroy_func_t) conn_acceptor_destroy); return ev; } -- cgit v1.2.3