[Libreoffice-commits] online.git: net/ServerSocket.hpp net/Socket.cpp net/Socket.hpp net/SslSocket.hpp net/WebSocketHandler.hpp wsd/ClientSession.cpp wsd/ClientSession.hpp wsd/LOOLWSD.cpp

Michael Meeks michael.meeks at collabora.com
Fri Mar 17 22:00:49 UTC 2017


 net/ServerSocket.hpp     |    3 ++-
 net/Socket.cpp           |    4 +++-
 net/Socket.hpp           |   33 ++++++++++++++++++---------------
 net/SslSocket.hpp        |   12 +++++++++---
 net/WebSocketHandler.hpp |    9 +++------
 wsd/ClientSession.cpp    |   11 ++++++++---
 wsd/ClientSession.hpp    |    5 +++--
 wsd/LOOLWSD.cpp          |   18 +++++++++---------
 8 files changed, 55 insertions(+), 40 deletions(-)

New commits:
commit 9381d4134fa26ec21d483ba6dcbf05e24125292b
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Fri Mar 17 21:59:09 2017 +0000

    Unify and simplify timeout and poll events setup.
    
    Both happen at the same time, and need support from the
    SocketHandlerInterface.
    
    Rename hasQueuedWrites to getPollEvents and merge updateTimeout.

diff --git a/net/ServerSocket.hpp b/net/ServerSocket.hpp
index a5112233..a0604e0b 100644
--- a/net/ServerSocket.hpp
+++ b/net/ServerSocket.hpp
@@ -80,7 +80,8 @@ public:
         return nullptr;
     }
 
-    int getPollEvents() override
+    int getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                      int & /* timeoutMaxMs */) override
     {
         return POLLIN;
     }
diff --git a/net/Socket.cpp b/net/Socket.cpp
index a0f67d0e..16deba99 100644
--- a/net/Socket.cpp
+++ b/net/Socket.cpp
@@ -136,7 +136,9 @@ void dump_hex (const char *legend, const char *prefix, std::vector<char> buffer)
 
 void StreamSocket::dumpState(std::ostream& os)
 {
-    os << "\t" << getFD() << "\t" << getPollEvents() << "\t"
+    int timeoutMaxMs = SocketPoll::DefaultPollTimeoutMs;
+    int events = getPollEvents(std::chrono::steady_clock::now(), timeoutMaxMs);
+    os << "\t" << getFD() << "\t" << events << "\t"
        << _inBuffer.size() << "\t" << _outBuffer.size() << "\t"
        << "\n";
     if (_inBuffer.size() > 0)
diff --git a/net/Socket.hpp b/net/Socket.hpp
index 3aaa3d52..268994a7 100644
--- a/net/Socket.hpp
+++ b/net/Socket.hpp
@@ -72,12 +72,11 @@ public:
         ::shutdown(_fd, SHUT_RDWR);
     }
 
-    /// Return a mask of events we should be polling for
-    virtual int getPollEvents() = 0;
-
-    /// Contract the poll timeout to match our needs
-    virtual void updateTimeout(std::chrono::steady_clock::time_point /* now */,
-                               int & /* timeoutMaxMs */) { /* do nothing */ }
+    /// Prepare our poll record; adjust @timeoutMaxMs downwards
+    /// for timeouts, based on current time @now.
+    /// @returns POLLIN and POLLOUT if output is expected.
+    virtual int getPollEvents(std::chrono::steady_clock::time_point now,
+                              int &timeoutMaxMs) = 0;
 
     /// Handle results of events returned from poll
     enum class HandleResult { CONTINUE, SOCKET_CLOSED };
@@ -448,8 +447,7 @@ private:
         for (size_t i = 0; i < size; ++i)
         {
             _pollFds[i].fd = _pollSockets[i]->getFD();
-            _pollFds[i].events = _pollSockets[i]->getPollEvents();
-            _pollSockets[i]->updateTimeout(now, timeoutMaxMs);
+            _pollFds[i].events = _pollSockets[i]->getPollEvents(now, timeoutMaxMs);
             _pollFds[i].revents = 0;
         }
 
@@ -520,8 +518,11 @@ public:
     /// Called after successful socket reads.
     virtual void handleIncomingMessage() = 0;
 
-    /// Is there queued up data that we want to write ?
-    virtual bool hasQueuedWrites() const = 0;
+    /// Prepare our poll record; adjust @timeoutMaxMs downwards
+    /// for timeouts, based on current time @now.
+    /// @returns POLLIN and POLLOUT if output is expected.
+    virtual int getPollEvents(std::chrono::steady_clock::time_point now,
+                              int &timeoutMaxMs) = 0;
 
     /// Do some of the queued writing.
     virtual void performWrites() = 0;
@@ -578,13 +579,15 @@ public:
         Socket::shutdown();
     }
 
-    int getPollEvents() override
+    int getPollEvents(std::chrono::steady_clock::time_point now,
+                      int &timeoutMaxMs) override
     {
+        // cf. SslSocket::getPollEvents
         assert(isCorrectThread());
-        if (!_outBuffer.empty() || _socketHandler->hasQueuedWrites() || _shutdownSignalled)
-            return POLLIN | POLLOUT;
-        else
-            return POLLIN;
+        int events = _socketHandler->getPollEvents(now, timeoutMaxMs);
+        if (!_outBuffer.empty() || _shutdownSignalled)
+            events |= POLLOUT;
+        return events;
     }
 
     /// Send data to the socket peer.
diff --git a/net/SslSocket.hpp b/net/SslSocket.hpp
index 7dc11d82..6f134e89 100644
--- a/net/SslSocket.hpp
+++ b/net/SslSocket.hpp
@@ -115,8 +115,12 @@ public:
         return handleSslState(SSL_write(_ssl, buf, len));
     }
 
-    int getPollEvents() override
+    int getPollEvents(std::chrono::steady_clock::time_point now,
+                      int & timeoutMaxMs) override
     {
+        assert(isCorrectThread());
+        int events = _socketHandler->getPollEvents(now, timeoutMaxMs);
+
         if (_sslWantsTo == SslWantsTo::Read)
         {
             // Must read next before attempting to write.
@@ -128,8 +132,10 @@ public:
             return POLLOUT;
         }
 
-        // Do the default.
-        return StreamSocket::getPollEvents();
+        if (!_outBuffer.empty() || _shutdownSignalled)
+            events |= POLLOUT;
+
+        return events;
     }
 
 private:
diff --git a/net/WebSocketHandler.hpp b/net/WebSocketHandler.hpp
index feff583b..50c5d0e9 100644
--- a/net/WebSocketHandler.hpp
+++ b/net/WebSocketHandler.hpp
@@ -225,13 +225,10 @@ public:
             ; // can have multiple msgs in one recv'd packet.
     }
 
-    /// By default rely on the socket buffer.
-    bool hasQueuedWrites() const override
+    int getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                      int & /* timeoutMaxMs */) override
     {
-        auto socket = _socket.lock();
-        if (socket != nullptr)
-            LOG_TRC("#" << socket->getFD() << ": WebSocket - asked for queued writes");
-        return false;
+        return POLLIN;
     }
 
     /// By default rely on the socket buffer.
diff --git a/wsd/ClientSession.cpp b/wsd/ClientSession.cpp
index 268dc05c..2cb10ba9 100644
--- a/wsd/ClientSession.cpp
+++ b/wsd/ClientSession.cpp
@@ -437,11 +437,16 @@ void ClientSession::setReadOnly()
     sendTextFrame("perm: readonly");
 }
 
-bool ClientSession::hasQueuedWrites() const
+
+int ClientSession::getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                                 int & /* timeoutMaxMs */)
 {
-    LOG_DBG(getName() << " ClientSession: has queued writes? "
+    LOG_TRC(getName() << " ClientSession: has queued writes? "
             << _senderQueue.size());
-    return _senderQueue.size() > 0;
+    int events = POLLIN;
+    if (_senderQueue.size())
+        events |= POLLOUT;
+    return events;
 }
 
 void ClientSession::performWrites()
diff --git a/wsd/ClientSession.hpp b/wsd/ClientSession.hpp
index 794ca06d..46fd8c58 100644
--- a/wsd/ClientSession.hpp
+++ b/wsd/ClientSession.hpp
@@ -122,8 +122,9 @@ private:
 
     /// SocketHandler: disconnection event.
     void onDisconnect() override;
-    /// SocketHandler: have data to write.
-    bool hasQueuedWrites() const override;
+    /// Does SocketHandler: have data or timeouts to setup.
+    int getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                      int & /* timeoutMaxMs */) override;
     /// SocketHandler: write to socket.
     void performWrites() override;
 
diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index a4e34062..ea9a73b0 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -1550,10 +1550,10 @@ private:
                 LOOLProtocol::getAbbreviatedMessage(data) << "].");
     }
 
-    bool hasQueuedWrites() const override
+    int getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                      int & /* timeoutMaxMs */) override
     {
-        LOG_TRC("PrisonerRequestDispatcher - asked for queued writes");
-        return false;
+        return POLLIN;
     }
 
     void performWrites() override
@@ -1589,10 +1589,10 @@ private:
         LOG_ERR("handleIncomingMessage");
     }
 
-    bool hasQueuedWrites() const override
+    int getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                      int & /* timeoutMaxMs */) override
     {
-        LOG_ERR("hasQueuedWrites");
-        return true;
+        return POLLIN;
     }
 
     void performWrites() override
@@ -1759,10 +1759,10 @@ private:
         }
     }
 
-    bool hasQueuedWrites() const override
+    int getPollEvents(std::chrono::steady_clock::time_point /* now */,
+                      int & /* timeoutMaxMs */)
     {
-        LOG_TRC("ClientRequestDispatcher - asked for queued writes");
-        return false;
+        return POLLIN;
     }
 
     void performWrites() override


More information about the Libreoffice-commits mailing list