From 7369505397cdfddf0883e2c24e1652df8bd488fe Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 16 Dec 2022 13:53:35 +0800 Subject: Refactor file structure --- acceptor.c | 58 ---------------------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 acceptor.c (limited to 'acceptor.c') diff --git a/acceptor.c b/acceptor.c deleted file mode 100644 index 1f28fcc..0000000 --- a/acceptor.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include -#include - -#include "acceptor.h" -#include "util.h" -#include "connection.h" -#include "evloop.h" - -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 = tsocket_accept(ca->sock); - if (conn_sock == NULL) { - perror("socket accept"); - return; - } - - if (setblocking(conn_sock->fd, false) == -1) { - perror("setblocking"); - tsocket_destroy(conn_sock); - return; - } - - event_t *conn_ev = connection_create_event(conn_sock); - - if (evloop_add(ca->el, conn_ev, EPOLLIN | EPOLLET) == -1) { - perror("eventloop add fd: conn_sock"); - return; - } - - printf("New client fd %d, ip: %s, port: %d\n", - conn_sock->fd, conn_sock->addr, conn_sock->port); -} - -static void conn_acceptor_destroy(conn_acceptor_t *ca) -{ - tsocket_destroy(ca->sock); - free(ca); -} - -event_t *conn_acceptor_create_event(struct tsocket *sock, evloop_t *el) -{ - conn_acceptor_t *ca = malloc(sizeof(*ca)); - ca->sock = sock; - ca->el = el; - - 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