[IOMultiplexer] Added asynchronous DNS Lookups
[IOMultiplexer.git] / src / IOEngine_select.c
index aff5fb920f264e47caa8ce9a86772cb7fcf0a6dd..bdfee3358c8240e40df0fb73f577a50d9362b145 100644 (file)
@@ -33,10 +33,13 @@ static int engine_select_init() {
     return 1;
 }
 
-static void engine_select_add(struct IODescriptor *iofd) {
+static void engine_select_add(struct IOLowlevelDescriptor *iold) {
     #ifdef WIN32
-    if(iofd->type == IOTYPE_STDIN)
-        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT);
+    struct IODescriptor *iofd;
+    if((iofd = IOLOWLEVEL_GET_IOFD(iold))) {
+        if(iofd->type == IOTYPE_STDIN)
+            SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT);
+    }
     #endif
     /* empty */
 }
@@ -53,7 +56,8 @@ static void engine_select_loop(struct timeval *timeout) {
     fd_set read_fds;
     fd_set write_fds;
     unsigned int fds_size = 0;
-    struct IODescriptor *iofd, *tmp_iofd;
+    struct IOLowlevelDescriptor *iold, *tmp_iold;
+    struct IODescriptor *iofd;
     struct timeval now, tdiff;
     int select_result;
     int timer_fix;
@@ -65,9 +69,10 @@ static void engine_select_loop(struct timeval *timeout) {
     FD_ZERO(&write_fds);
     
     select_result = 0;
-    for(iofd = first_descriptor; iofd; iofd = tmp_iofd) {
-        tmp_iofd = iofd->next;
-        if(iofd->type == IOTYPE_STDIN) {
+    for(iold = first_descriptor; iold; iold = tmp_iold) {
+        tmp_iold = iold->next;
+        iofd = IOLOWLEVEL_GET_IOFD(iold);
+        if(iofd && iofd->type == IOTYPE_STDIN) {
             #ifdef WIN32
             //WIN32 doesn't support stdin within select
             //just try to read the single events from the console
@@ -98,20 +103,21 @@ static void engine_select_loop(struct timeval *timeout) {
                 timeout->tv_usec = 100000;
             }
             #else
-            if(iofd->fd > fds_size)
-                fds_size = iofd->fd;
-            FD_SET(iofd->fd, &read_fds);
+            if(iold->fd > fds_size)
+                fds_size = iold->fd;
+            FD_SET(iold->fd, &read_fds);
             select_result++;
             #endif
         }
-        else if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT) {
-            if(iofd->state == IO_CLOSED) 
+        else if(iold->fd >= 0) {
+            if(iofd && iofd->state == IO_CLOSED) 
                 continue;
-            if(iofd->fd > fds_size)
-                fds_size = iofd->fd;
-            FD_SET(iofd->fd, &read_fds);
+            if(iold->fd > fds_size)
+                fds_size = iold->fd;
             select_result++;
-            if(iohandler_wants_writes(iofd))
+            if(iold->flags & IOFLAGS_WANT_READ)
+                FD_SET(iold->fd, &read_fds);
+            if(iold->flags & IOFLAGS_WANT_WRITE)
                 FD_SET(iofd->fd, &write_fds);
         }
     }
@@ -120,10 +126,14 @@ static void engine_select_loop(struct timeval *timeout) {
         tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
         tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
         if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
-            if(timer_priority->constant_timeout) {
+            iofd = IOLOWLEVEL_GET_IOFD(iold);
+            if(iofd && iofd->constant_timeout) {
                 tdiff.tv_sec = 0;
-                iohandler_set_timeout(timer_priority, &tdiff);
-                iohandler_events(timer_priority, 0, 0);
+                iohandler_set_timeout(iofd, &tdiff);
+                if(iofd)
+                    iohandler_events(timer_priority, 0, 0);
+                else
+                    timer_priority->data.callback(timer_priority, 0, 0);
             } else {
                 iohandler_events(timer_priority, 0, 0);
                 iohandler_close(timer_priority); //also sets timer_priority to the next timed element
@@ -168,11 +178,15 @@ static void engine_select_loop(struct timeval *timeout) {
     gettimeofday(&now, NULL);
     
     //check all descriptors
-    for(iofd = first_descriptor; iofd; iofd = tmp_iofd) {
-        tmp_iofd = iofd->next;
-        if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN) {
-            if(FD_ISSET(iofd->fd, &read_fds) || FD_ISSET(iofd->fd, &write_fds)) {
-                iohandler_events(iofd, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds));
+    for(iold = first_descriptor; iold; iold = tmp_iold) {
+        tmp_iold = iold->next;
+        iofd = IOLOWLEVEL_GET_IOFD(iold);
+        if((iofd && iofd->type == IOTYPE_STDIN) || iold->fd >= 0) {
+            if(FD_ISSET(iold->fd, &read_fds) || FD_ISSET(iold->fd, &write_fds)) {
+                if(iofd)
+                    iohandler_events(iofd, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds));
+                else
+                    iold->data.callback(iold, FD_ISSET(iofd->fd, &read_fds), FD_ISSET(iofd->fd, &write_fds));
                 continue;
             }
         }