From 6c46cab599134c31e9514b2be0ab62368a78e644 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 18 Aug 2021 19:20:10 +0000 Subject: [PATCH] Use epoll ET for better performance --- w2u.c | 61 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/w2u.c b/w2u.c index 4ef9863..29adf78 100644 --- a/w2u.c +++ b/w2u.c @@ -77,11 +77,11 @@ int main(int argc, char* argv[]) ep = epoll_create(1024); struct epoll_event ev1; - ev1.events = EPOLLIN; + ev1.events = EPOLLIN | EPOLLET; ev1.data.fd = listener; struct epoll_event ev2; - ev2.events = EPOLLIN; + ev2.events = EPOLLIN | EPOLLET; ev2.data.fd = sender; epoll_ctl(ep, EPOLL_CTL_ADD, listener, &ev1); @@ -89,9 +89,27 @@ int main(int argc, char* argv[]) struct epoll_event events[1024]; + struct sockaddr_in* addrTargets = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in) * PORT_UR_SIZE); + memset(addrTargets, 0, sizeof(struct sockaddr_in) * PORT_UR_SIZE); + for(int i=0; i= PORT_UR_SIZE) { + rrOffset = 0; + } } } - else if (events[i].data.fd == sender) + else if (events[i].data.fd == sender) { - struct sockaddr_in addr; - socklen_t alen = sizeof(addr); + int nsize = recvfrom(sender, buffer, sizeof(buffer), 0, NULL, NULL); - char buffer[2048]; - int nsize = recvfrom(sender, &buffer, 2048, 0, (struct sockaddr*)&addr, &alen); - - if (nsize < 0) - { + if (nsize < 0) { perror("recvfrom"); + } else { + sendto(listener, buffer, nsize, 0, (const struct sockaddr*)&addrWg, sizeof(struct sockaddr_in)); } - else - { - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); - addr.sin_port = htons(PORT_WG); - sendto(listener, buffer, nsize, 0, (const struct sockaddr*)&addr, sizeof(addr)); - } } } }