[IOMultiplexer] Added asynchronous DNS Lookups
[IOMultiplexer.git] / src / IOEngine_epoll.c
1 /* IOEngine_epoll.c - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "IOEngine.h"
18
19 #ifdef HAVE_SYS_EPOLL_H
20 #include <sys/epoll.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #define MAX_EVENTS 32
26
27 static int epoll_fd;
28
29 static int engine_epoll_init() {
30     epoll_fd = epoll_create(1024);
31     if (epoll_fd < 0)
32         return 0;
33     return 1;
34 }
35
36 static void engine_epoll_add(struct IOLowlevelDescriptor *iold) {
37     if(iold->fd != -1) return;
38     //add descriptor to the epoll queue
39     struct epoll_event evt;
40     int res;
41
42     evt.events = EPOLLHUP | ((iold->flags & IOFLAGS_WANT_READ) ? EPOLLIN : 0) | ((iold->flags & IOFLAGS_WANT_WRITE) ? EPOLLOUT : 0);
43     evt.data.ptr = iold;
44     res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, iold->fd, &evt);
45     if(res < 0) {
46         iohandler_log(IOLOG_ERROR, "could not add IOLowlevelDescriptor %d to epoll queue. (returned: %d)", iold->fd, res);
47     }
48 }
49
50 static void engine_epoll_remove(struct IOLowlevelDescriptor *iold) {
51     if(iold->fd != -1) return;
52     struct epoll_event evt;
53     epoll_ctl(epoll_fd, EPOLL_CTL_DEL, iold->fd, &evt);
54 }
55
56 static void engine_epoll_update(struct IOLowlevelDescriptor *iold) {
57     if(iold->fd != -1) return;
58     struct IODescriptor *iofd;
59     if((iofd = IOLOWLEVEL_GET_IOFD(iold))) {
60         if(iofd->state == IO_CLOSED) {
61             engine_epoll_remove(iold);
62             return;
63         }
64     }
65     
66     struct epoll_event evt;
67     int res;
68
69     evt.events = EPOLLHUP | ((iold->flags & IOFLAGS_WANT_READ) ? EPOLLIN : 0) | ((iold->flags & IOFLAGS_WANT_WRITE) ? EPOLLOUT : 0);
70     evt.data.ptr = iold;
71     res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, iold->fd, &evt);
72     if(res < 0) {
73         iohandler_log(IOLOG_ERROR, "could not update IOLowlevelDescriptor %d in epoll queue. (returned: %d)", iold->fd, res);
74     }
75 }
76
77 static void engine_epoll_loop(struct timeval *timeout) {
78     struct epoll_event evts[MAX_EVENTS];
79     struct timeval now, tdiff;
80     int msec;
81     int events;
82     int epoll_result;
83     struct IOLowlevelDescriptor *iold;
84     
85     gettimeofday(&now, NULL);
86     
87     while(timer_priority) {
88         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
89         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
90         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
91             if(timer_priority->constant_timeout) {
92                 tdiff.tv_sec = 0;
93                 iohandler_set_timeout(timer_priority, &tdiff);
94                 iohandler_events(timer_priority, 0, 0);
95             } else {
96                 iohandler_events(timer_priority, 0, 0);
97                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
98             }
99             continue;
100         } else if(tdiff.tv_usec < 0) {
101             tdiff.tv_sec--;
102             tdiff.tv_usec += 1000000; //1 sec
103         }
104         if(timeval_is_smaler((&tdiff), timeout)) {
105             timeout->tv_sec = tdiff.tv_sec;
106             timeout->tv_usec = tdiff.tv_usec;
107         }
108         break;
109     }
110     
111     msec = timeout ? ((timeout->tv_sec * 1000 + timeout->tv_usec / 1000) + (timeout->tv_usec % 1000 != 0 ? 1 : 0)) : -1;
112     
113     //select system call
114     epoll_result = epoll_wait(epoll_fd, evts, MAX_EVENTS, msec);
115     
116     if (epoll_result < 0) {
117         if (errno != EINTR) {
118             iohandler_log(IOLOG_FATAL, "epoll_wait() failed with errno %d: %s", errno, strerror(errno));
119             return;
120         }
121     } else {
122         int i;
123         for(i = 0; i < epoll_result; i++) {
124             events = evts[i].events;
125             iold = evts[i].data.ptr;
126             if(iold->flags & IOFLAGS_HAVE_IOFD)
127                 iohandler_events(iold->data.iofd, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
128             else
129                 iold->data.callback(iold, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
130         }
131     }
132     
133     //check timers
134     while(timer_priority) {
135         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
136         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
137         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
138             if(timer_priority->constant_timeout) {
139                 tdiff.tv_sec = 0;
140                 iohandler_set_timeout(timer_priority, &tdiff);
141                 iohandler_events(timer_priority, 0, 0);
142             } else {
143                 iohandler_events(timer_priority, 0, 0);
144                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
145             }
146             continue;
147         }
148         break;
149     }
150     
151 }
152
153 static void engine_epoll_cleanup() {
154     close(epoll_fd);
155 }
156
157 struct IOEngine engine_epoll = {
158     .name = "epoll",
159     .init = engine_epoll_init,
160     .add = engine_epoll_add,
161     .remove = engine_epoll_remove,
162     .update = engine_epoll_update,
163     .loop = engine_epoll_loop,
164     .cleanup = engine_epoll_cleanup,
165 };
166
167 #else
168
169 struct IOEngine engine_epoll = {
170     .name = "epoll",
171     .init = NULL,
172     .add = NULL,
173     .remove = NULL,
174     .update = NULL,
175     .loop = NULL,
176     .cleanup = NULL,
177 };
178
179 #endif