From 0457119acb36b89b6f2f4534fe8ad94b19540bbd Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 2 Dec 2022 12:18:57 +0800 Subject: Refactor --- eventloop.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'eventloop.c') diff --git a/eventloop.c b/eventloop.c index 970a521..ae4377c 100644 --- a/eventloop.c +++ b/eventloop.c @@ -3,6 +3,7 @@ #include "eventloop.h" #include "util.h" +#include "constant.h" struct eventLoop *eventLoopNew() { @@ -33,8 +34,6 @@ int eventLoopAdd(struct eventLoop *el, struct event *ev, int flag) epev.events = flag; epev.data.ptr = ev; - ev->el = el; - return epoll_ctl(el->epollfd, EPOLL_CTL_ADD, ev->fd, &epev); } @@ -55,7 +54,8 @@ void eventLoopLoop(struct eventLoop *el) for (int i = 0; i < nevents; i++) { ev = eventLoopGet(el, i); - ev->handle(ev); + if (ev->handle(ev->data) == -1) + eventLoopDel(el, ev); } } } @@ -66,9 +66,19 @@ int eventLoopDel(struct eventLoop *el, struct event *ev) return ERROR; if (ev->delete) - ev->delete(ev); - else - free(ev); + ev->delete(ev->data); + free(ev); return OK; } + +struct event *eventNew(void *data, int fd, void (*handle), void (*delete)) +{ + struct event *ev = malloc(sizeof(*ev)); + ev->data = data; + ev->handle = handle; + ev->fd = fd; + ev->delete = delete; + + return ev; +} -- cgit v1.2.3