From 14eb75c7f5e90f3c1174cdf700753d74bbd358a8 Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 2 Dec 2022 01:04:51 +0800 Subject: Abstract accept and handle as event --- server.c | 102 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 38 deletions(-) (limited to 'server.c') diff --git a/server.c b/server.c index bc856f9..bab1edc 100644 --- a/server.c +++ b/server.c @@ -16,11 +16,13 @@ #define READ_BUFFER_SIZE 1024 -void handleEvent(struct tsocket *sock) +void handleReadEvent(struct event *ev) { char buf[READ_BUFFER_SIZE]; ssize_t n_read_bytes; + struct tsocket *sock = (struct tsocket *)ev->data; + for (;;) { n_read_bytes = read(sock->fd, buf, sizeof(buf)); if (n_read_bytes > 0) { @@ -28,7 +30,7 @@ void handleEvent(struct tsocket *sock) write(sock->fd, buf, sizeof(buf)); } else if (n_read_bytes == 0) { printf("conn %d disconnected\n", sock->fd); - tsocketDelete(sock); + eventLoopDel(ev->el, ev); break; } else if (n_read_bytes == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) @@ -37,51 +39,75 @@ void handleEvent(struct tsocket *sock) } } -int main() +void serverDeleteReadEvent(struct event *ev) +{ + tsocketDelete(ev->data); + free(ev); +} + +struct event *serverNewReadEvent(struct tsocket *sock) +{ + struct event *ev = malloc(sizeof(*ev)); + ev->data = sock; + ev->handle = handleReadEvent; + ev->fd = sock->fd; + ev->delete = serverDeleteReadEvent; + + return ev; +} + +void handleNewClientConnection(struct event *ev) +{ + struct tsocket *conn_sock = tsocketAccept(ev->data); + if (conn_sock == NULL) { + perror("socket accept"); + return; + } + + if (setblocking(conn_sock->fd, false) == -1) { + perror("setblocking"); + tsocketDelete(conn_sock); + return; + } + + struct event *read_ev = serverNewReadEvent(conn_sock); + if (eventLoopAdd(ev->el, read_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); +} + +struct event *serverNewAcceptEvent(const char *addr, int port) { struct tsocket *sock = tsocketNew(); - if (sock == NULL) - panic("socket creation error"); + if (sock == NULL + || tsocketBind(sock, addr, port) == -1 + || tsocketListen(sock) == -1) + return NULL; - if (tsocketBind(sock, "127.0.0.1", 8888) == -1) - panic("socket bind error"); + struct event *ev = malloc(sizeof(*ev)); + ev->handle = handleNewClientConnection; + ev->data = sock; + ev->fd = sock->fd; - if (tsocketListen(sock) == -1) - panic("socket listen error"); + return ev; +} +int main() +{ struct eventLoop *el = eventLoopNew(); if (el == NULL) panic("eventloop creation"); - if (eventLoopAddSocket(el, sock, EPOLLIN) == -1) + struct event *acceptEvent = serverNewAcceptEvent("127.0.0.1", 8888); + if (acceptEvent == NULL) + panic("server client connection event"); + + if (eventLoopAdd(el, acceptEvent, EPOLLIN) == -1) panic("eventloop add fd"); - int nfds; - struct tsocket *conn_sock; - for (;;) { - nfds = eventLoopWait(el, -1); - if (nfds == -1) - panic("eventloop wait"); - - for (int i = 0; i < nfds; i++) { - if (eventLoopGetSocket(el, i) == sock) { - conn_sock = tsocketAccept(sock); - if (conn_sock == NULL) - panic("socket accept error"); - - if (setblocking(conn_sock->fd, false) == -1) { - tsocketDelete(conn_sock); - continue; - } - - if (eventLoopAddSocket(el, conn_sock, EPOLLIN | EPOLLET) == -1) - panic("eventloop add fd: conn_sockfd"); - - printf("New client fd %d, ip: %s, port: %d\n", - conn_sock->fd, conn_sock->addr, conn_sock->port); - } else { - handleEvent(eventLoopGetSocket(el, i)); - } - } - } + eventLoopLoop(el); } -- cgit v1.2.3