[Libreoffice-commits] online.git: Branch 'private/hcvcastro/socket' - loolwsd/IoUtil.cpp loolwsd/IoUtil.hpp loolwsd/LOOLBroker.cpp

Henry Castro hcastro at collabora.com
Sun Apr 3 20:12:36 UTC 2016


 loolwsd/IoUtil.cpp     |   55 +++++++++++++++++++++++++++++++++++++++++++++++++
 loolwsd/IoUtil.hpp     |    8 +++++++
 loolwsd/LOOLBroker.cpp |   22 ++-----------------
 3 files changed, 66 insertions(+), 19 deletions(-)

New commits:
commit 7f0996ad04697f54a4c5de00231e7e13c5f72b71
Author: Henry Castro <hcastro at collabora.com>
Date:   Sun Apr 3 16:12:52 2016 -0400

    loolwsd: use SocketProcessor

diff --git a/loolwsd/IoUtil.cpp b/loolwsd/IoUtil.cpp
index ec07bb3..f01634f 100644
--- a/loolwsd/IoUtil.cpp
+++ b/loolwsd/IoUtil.cpp
@@ -192,6 +192,60 @@ void SocketProcessor(std::shared_ptr<WebSocket> ws,
     Log::info(name + "Finished Socket Processor.");
 }
 
+// Synchronously process DialogSocket requests and dispatch to handler.
+// Handler returns false to end.
+void SocketProcessor(std::shared_ptr<Poco::Net::DialogSocket> ds,
+                     std::function<bool(std::string&)> handler,
+                     std::function<bool()> stopPredicate,
+                     std::string name,
+                     const size_t pollTimeoutMs)
+{
+    Log::info(name + "Starting Socket Processor.");
+
+    // Timeout given is in microseconds.
+    const Poco::Timespan waitTime(pollTimeoutMs * 1000);
+    try
+    {
+        bool stop = false;
+
+        for (;;)
+        {
+            stop = stopPredicate();
+            if (stop)
+            {
+                Log::info(name + "Termination flagged. Finishing.");
+                break;
+            }
+
+            if (!ds->poll(waitTime, Poco::Net::Socket::SELECT_READ))
+            {
+                // Wait some more.
+                continue;
+            }
+
+            std::string message;
+            ds->receiveMessage(message);
+             // Call the handler.
+            if (!handler(message))
+            {
+                Log::info(name + "Socket handler flagged to finish.");
+                break;
+            }
+        }
+
+        Log::debug() << name << "Finishing SocketProcessor. TerminationFlag: " << stop << Log::end;
+    }
+    catch (const Poco::Exception& exc)
+    {
+        Log::error() << "CommandRunnable::run: Exception: " << exc.displayText()
+                     << (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
+                     << Log::end;
+    }
+
+    Log::info(name + "Finished Socket Processor.");
+}
+
+
 void shutdownWebSocket(std::shared_ptr<Poco::Net::WebSocket> ws)
 {
     try
@@ -302,6 +356,7 @@ int PipeReader::readLine(std::string& line,
         pipe.events = POLLIN;
         pipe.revents = 0;
         const int ready = poll(&pipe, 1, pollTimeoutMs);
+
         if (ready == 0)
         {
             // Timeout.
diff --git a/loolwsd/IoUtil.hpp b/loolwsd/IoUtil.hpp
index 016a808..96ac5bd 100644
--- a/loolwsd/IoUtil.hpp
+++ b/loolwsd/IoUtil.hpp
@@ -17,6 +17,7 @@
 #include <sys/poll.h>
 
 #include <Poco/Net/WebSocket.h>
+#include <Poco/Net/DialogSocket.h>
 #include <Poco/Logger.h>
 
 namespace IoUtil
@@ -30,6 +31,13 @@ namespace IoUtil
                          std::string name = std::string(),
                          const size_t pollTimeoutMs = POLL_TIMEOUT_MS);
 
+    void SocketProcessor(std::shared_ptr<Poco::Net::DialogSocket> ds,
+                         std::function<bool(std::string&)> handler,
+                         std::function<bool()> stopPredicate,
+                         std::string name = std::string(),
+                         const size_t pollTimeoutMs = POLL_TIMEOUT_MS);
+
+
     /// Call WebSocket::shutdown() ignoring Poco::IOException.
     void shutdownWebSocket(std::shared_ptr<Poco::Net::WebSocket> ws);
 
diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLBroker.cpp
index 5013736..4da203b 100644
--- a/loolwsd/LOOLBroker.cpp
+++ b/loolwsd/LOOLBroker.cpp
@@ -261,25 +261,9 @@ public:
 
         Log::debug("Thread [" + thread_name + "] started.");
 
-        try
-        {
-            while (!TerminationFlag)
-            {
-                if (_dlgWsd->poll(waitTime, Socket::SELECT_READ))
-                {
-                    std::string message;
-                    _dlgWsd->receiveMessage(message);
-                    handleInput(message);
-                }
-            }
-            _dlgWsd->shutdown();
-        }
-        catch (const Exception& exc)
-        {
-            Log::error() << "CommandRunnable::run: Exception: " << exc.displayText()
-                         << (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
-                         << Log::end;
-        }
+        IoUtil::SocketProcessor(_dlgWsd,
+                                [this](std::string& message) { handleInput(message); return true; },
+                                []() { return TerminationFlag; });
 
         Log::debug("Thread [" + thread_name + "] finished.");
     }


More information about the Libreoffice-commits mailing list