changeset 271:3d37e1afec54

Irccd: the class is Irccd is now pollable, #561
author David Demelier <markand@malikania.fr>
date Fri, 23 Sep 2016 18:20:08 +0200
parents 90909cf677b1
children 8fb6bd57878c
files lib/irccd/irccd.cpp lib/irccd/irccd.hpp
diffstat 2 files changed, 26 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/irccd.cpp	Mon Sep 19 13:42:54 2016 +0200
+++ b/lib/irccd/irccd.cpp	Fri Sep 23 18:20:08 2016 +0200
@@ -58,44 +58,21 @@
 void Irccd::run()
 {
     while (m_running) {
-        poll();
+        poll(250);
         dispatch();
     }
 }
 
-void Irccd::poll()
+void Irccd::prepare(fd_set &in, fd_set &out, net::Handle &max)
 {
-    fd_set setinput;
-    fd_set setoutput;
-    net::Handle max = 0;
-
-    FD_ZERO(&setinput);
-    FD_ZERO(&setoutput);
-
     for (const auto &service : m_services)
-        service->prepare(setinput, setoutput, max);
-
-    // Do the selection.
-    struct timeval tv;
+        service->prepare(in, out, max);
+}
 
-    tv.tv_sec = 5;
-    tv.tv_usec = 250000;
-
-    int error = select(max + 1, &setinput, &setoutput, nullptr, &tv);
-
-    // Skip anyway if requested to stop
-    if (!m_running)
-        return;
-
-    // Skip on error.
-    if (error < 0 && errno != EINTR) {
-        log::warning() << "irccd: " << net::error(error) << endl;
-        return;
-    }
-
-    // Process after selection.
+void Irccd::sync(fd_set &in, fd_set &out)
+{
     for (const auto &service : m_services)
-        service->sync(setinput, setoutput);
+        service->sync(in, out);
 }
 
 void Irccd::dispatch()
--- a/lib/irccd/irccd.hpp	Mon Sep 19 13:42:54 2016 +0200
+++ b/lib/irccd/irccd.hpp	Fri Sep 23 18:20:08 2016 +0200
@@ -30,6 +30,7 @@
 #include <mutex>
 #include <vector>
 
+#include "pollable.hpp"
 #include "sysconfig.hpp"
 
 /**
@@ -50,7 +51,7 @@
  * \class Irccd
  * \brief Irccd main instance.
  */
-class Irccd {
+class Irccd : public Pollable {
 private:
     // Main loop stuff.
     std::atomic<bool> m_running{true};
@@ -151,6 +152,23 @@
     }
 
     /**
+     * Prepare the services for selection.
+     *
+     * \param in the input set
+     * \param out the output set
+     * \param max the maximum handle
+     */
+    IRCCD_EXPORT void prepare(fd_set &in, fd_set &out, net::Handle &max) override;
+
+    /**
+     * Synchronize the services.
+     *
+     * \param in the input set
+     * \param out the output set
+     */
+    IRCCD_EXPORT void sync(fd_set &in, fd_set &out) override;
+
+    /**
      * Add an event to the queue. This will immediately signals the event loop
      * to interrupt itself to dispatch the pending events.
      *
@@ -165,11 +183,6 @@
     IRCCD_EXPORT void run();
 
     /**
-     * Poll the next events without blocking (250 ms max).
-     */
-    IRCCD_EXPORT void poll();
-
-    /**
      * Dispatch the pending events, usually after calling poll().
      */
     IRCCD_EXPORT void dispatch();