Reformat: delete trailing whitespace
[yazpp-moved-to-github.git] / src / yaz-socket-manager.cpp
index a675f88..0a3797a 100644 (file)
@@ -1,8 +1,11 @@
 /* This file is part of the yazpp toolkit.
- * Copyright (C) 1998-2008 Index Data and Mike Taylor
+ * Copyright (C) 1998-2012 Index Data and Mike Taylor
  * See the file LICENSE for details.
  */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -29,13 +32,22 @@ SocketManager::SocketEntry **SocketManager::lookupObserver(
     ISocketObserver *observer)
 {
     SocketEntry **se;
-    
+
     for (se = &m_observers; *se; se = &(*se)->next)
         if ((*se)->observer == observer)
             break;
     return se;
 }
 
+int SocketManager::getNumberOfObservers()
+{
+    int i = 0;
+    SocketEntry *se;
+    for (se = m_observers; se; se = se->next, i++)
+        ;
+    return i;
+}
+
 void SocketManager::addObserver(int fd, ISocketObserver *observer)
 {
     SocketEntry *se;
@@ -69,7 +81,7 @@ void SocketManager::deleteObserver(ISocketObserver *observer)
 void SocketManager::deleteObservers()
 {
     SocketEntry *se = m_observers;
-    
+
     while (se)
     {
         SocketEntry *se_next = se->next;
@@ -112,10 +124,21 @@ void SocketManager::inspect_poll_result(int res, struct yaz_poll_fd *fds,
     time_t now = time(0);
     int i;
     int no_put_events = 0;
-    SocketEntry *p;
+    int no_lost_observers = 0;
 
-    for (i = 0, p = m_observers; p; p = p->next, i++)
+    for (i = 0; i < no_fds; i++)
     {
+        SocketEntry *p;
+        for (p = m_observers; p; p = p->next)
+            if (p->fd == fds[i].fd)
+                break;
+        if (!p)
+        {
+            // m_observers list changed since poll started
+            no_lost_observers++;
+            continue;
+        }
+
         enum yaz_poll_mask output_mask = fds[i].output_mask;
 
         int mask = 0;
@@ -127,7 +150,7 @@ void SocketManager::inspect_poll_result(int res, struct yaz_poll_fd *fds,
 
         if (output_mask & yaz_poll_except)
             mask |= SOCKET_OBSERVE_EXCEPT;
-        
+
         if (mask)
         {
             SocketEvent *event = new SocketEvent;
@@ -149,7 +172,7 @@ void SocketManager::inspect_poll_result(int res, struct yaz_poll_fd *fds,
             event->event = SOCKET_OBSERVE_TIMEOUT;
             putEvent (event);
             no_put_events++;
-            
+
         }
     }
     SocketEvent *event = getEvent();
@@ -160,12 +183,14 @@ void SocketManager::inspect_poll_result(int res, struct yaz_poll_fd *fds,
     }
     else
     {
-        // bug #2035
-        
-        yaz_log(YLOG_WARN, "unhandled socket event. yaz_poll returned %d", res);
-        yaz_log(YLOG_WARN, "no_put_events=%d no_fds=%d i=%d timeout=%d",
-                no_put_events, no_fds, i, timeout);
-        abort();
+        if (no_lost_observers == 0)
+        {
+            // bug #2035
+            yaz_log(YLOG_WARN, "unhandled socket event. yaz_poll returned %d",
+                    res);
+            yaz_log(YLOG_WARN, "no_put_events=%d no_fds=%d i=%d timeout=%d",
+                    no_put_events, no_fds, i, timeout);
+        }
     }
 }
 
@@ -216,7 +241,7 @@ int SocketManager::processEvent()
             if (timeout == -1 || timeout_this < timeout)
                 timeout = timeout_this;
             p->timeout_this = timeout_this;
-            yaz_log (m_log, "SocketManager::select timeout_this=%d", 
+            yaz_log (m_log, "SocketManager::select timeout_this=%d",
                      p->timeout_this);
         }
         else
@@ -225,21 +250,22 @@ int SocketManager::processEvent()
     }
 
     int pass = 0;
-    while ((res = yaz_poll(fds, no_fds, timeout, 0)) < 0)
+    while ((res = yaz_poll(fds, no_fds, timeout, 0)) < 0 && pass < 10)
     {
-        if (errno != EINTR)
+        if (errno == EINTR)
         {
-            yaz_log(YLOG_ERRNO|YLOG_WARN, "yaz_poll");
-            yaz_log(YLOG_WARN, "errno=%d timeout=%d", errno, timeout);
-            if (++pass > 10)
-                return -1;
+            delete [] fds;
+            return 1;
         }
+        yaz_log(YLOG_ERRNO|YLOG_WARN, "yaz_poll");
+        yaz_log(YLOG_WARN, "errno=%d timeout=%d", errno, timeout);
     }
 
-    inspect_poll_result(res, fds, no_fds, timeout);
+    if (res >= 0)
+        inspect_poll_result(res, fds, no_fds, timeout);
 
     delete [] fds;
-    return 1;
+    return res >= 0 ? 1 : -1;
 }
 
 
@@ -319,6 +345,7 @@ SocketManager::~SocketManager()
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab