added SSL backend for IOMultiplexer
[TransparentIRC.git] / src / IOEngine_select.c
index dabe2e6b28ec94f788c19cc592c348b45736ec93..8303308f3876f2dcb68a85f76ed36f63987c5f58 100644 (file)
@@ -56,56 +56,62 @@ static void engine_select_loop(struct timeval *timeout) {
             FD_SET(iofd->fd, &read_fds);
             if(iohandler_wants_writes(iofd))
                 FD_SET(iofd->fd, &write_fds);
-        } else if(iofd->type == IOTYPE_TIMER) {
-            tdiff.tv_sec = iofd->timeout.tv_sec - now.tv_sec;
-            tdiff.tv_usec = iofd->timeout.tv_usec - now.tv_usec;
-            
-            if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
-                //exec timer
-                iofd->state = IO_CLOSED;
-                iohandler_events(iofd, 1, 0);
-                iohandler_close(iofd);
-                continue;
-            } else if(tdiff.tv_usec < 0) {
-                tdiff.tv_sec--;
-                tdiff.tv_usec += 1000000; //1 sec
-            }
-            if(timeval_is_smaler((&tdiff), timeout)) {
-                timeout->tv_sec = tdiff.tv_sec;
-                timeout->tv_usec = tdiff.tv_usec;
-            }
         }
     }
     
+    while(timer_priority) {
+        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)) {
+            iohandler_events(timer_priority, 0, 0);
+            iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+            continue;
+        } else if(tdiff.tv_usec < 0) {
+            tdiff.tv_sec--;
+            tdiff.tv_usec += 1000000; //1 sec
+        }
+        if(timeval_is_smaler((&tdiff), timeout)) {
+            timeout->tv_sec = tdiff.tv_sec;
+            timeout->tv_usec = tdiff.tv_usec;
+        }
+        break;
+    }
+    
     //select system call
     select_result = select(fds_size + 1, &read_fds, &write_fds, NULL, timeout);
     
     if (select_result < 0) {
         if (errno != EINTR) {
-            //hard fail
+            iohandler_log(IOLOG_FATAL, "select() failed with errno %d: %s", errno, strerror(errno));
             return;
         }
     }
     
+    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));
-            }
-        } else if(iofd->type == IOTYPE_TIMER) {
-            tdiff.tv_sec = iofd->timeout.tv_sec - now.tv_sec;
-            tdiff.tv_usec = iofd->timeout.tv_usec - now.tv_usec;
-            
-            if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
-                //exec timer
-                iofd->state = IO_CLOSED;
-                iohandler_events(iofd, 1, 0);
                 continue;
             }
         }
     }
+    
+    //check timers
+    while(timer_priority) {
+        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)) {
+            iohandler_events(timer_priority, 0, 0);
+            iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+            continue;
+        }
+        break;
+    }
+    
 }
 
 static void engine_select_cleanup() {