[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/clientnb.cpp net/loolnb.cpp

Michael Meeks michael.meeks at collabora.com
Sat Feb 18 01:06:22 UTC 2017


 net/clientnb.cpp |   35 ++++++++++++++++++----------------
 net/loolnb.cpp   |   56 ++++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 56 insertions(+), 35 deletions(-)

New commits:
commit 84be891579089b29070c1506531aec004d69414e
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Sat Feb 18 00:58:49 2017 +0000

    WS: cleanup calculation, masking, short reads and other bits.

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 96fdc80..2dc7950 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -167,6 +167,11 @@ struct Client : public Poco::Util::Application
     {
         Session session("ws");
         std::shared_ptr<WebSocket> ws = session.getWebSocket();
+
+        std::string send = "hello there";
+        ws->sendFrame(&send[0], send.length(),
+                      WebSocket::SendFlags::FRAME_TEXT);
+
         for (size_t i = 0; i < 10; i++)
         {
             ws->sendFrame(&i, sizeof(i), WebSocket::SendFlags::FRAME_BINARY);
@@ -184,26 +189,24 @@ public:
         const bool https = (args.size() > 0 && args[0] == "ssl");
         std::cerr << "Starting " << (https ? "HTTPS" : "HTTP") << " client." << std::endl;
 
-        if (getenv("WS"))
-            testWebsocket();
-        else
-        {
-            Session first("init", https);
-            Session second("init", https);
+        testWebsocket();
 
-            int count = 42, back;
-            first.sendPing(count);
-            second.sendPing(count + 1);
+        Session first("init");
+        Session second("init");
 
-            back = first.getResponse();
-            assert (back == count + 1);
+        int count = 42, back;
+        first.sendPing(count);
+        second.sendPing(count + 1);
 
-            back = second.getResponse();
-            assert (back == count + 2);
+        back = first.getResponse();
+        assert (back == count + 1);
+
+        back = second.getResponse();
+        assert (back == count + 2);
+
+        testLadder();
+        testParallel();
 
-            testLadder();
-            testParallel();
-        }
         return 0;
     }
 };
diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index 2b5773c..b91e4d2 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -137,50 +137,59 @@ public:
         // websocket fun !
         size_t len = T::_inBuffer.size();
         char *p = &T::_inBuffer[0];
-        char *data, *mask;
         if (len < 2) // partial read
             return;
 
-        bool fin = *p & 0x80;
-        WSOpCode code = static_cast<WSOpCode>(*p & 0x0f);
-        p++;
-        bool hasMask = *p & 0x80;
-        size_t payloadLen = *p & 0x7f;
-        p++;
+        bool fin = p[0] & 0x80;
+        WSOpCode code = static_cast<WSOpCode>(p[0] & 0x0f);
+        bool hasMask = p[1] & 0x80;
+        size_t payloadLen = p[1] & 0x7f;
+        size_t headerLen = 2;
 
+        // normally - 7 bit length.
         if (payloadLen == 126) // 2 byte length
         {
             if (len < 2 + 2)
                 return;
             std::cerr << "Implement me 2 byte\n";
-            data = p + 2;
-            len -= 2;
+            headerLen += 2;
         }
         else if (payloadLen == 127) // 8 byte length
         {
             if (len < 2 + 8)
                 return;
             std::cerr << "Implement me 8 byte\n";
-            data = p + 8;
-            len -= 8;
+            // FIXME: crop read length to remove top / sign bits.
+            headerLen += 8;
         }
-        else
+
+        char *data, *mask;
+
+        if (hasMask)
         {
-            data = p;
+            mask = p + headerLen;
+            headerLen += 4;
+        }
+
+        if (payloadLen + headerLen > len)
+        { // partial read wait for more data.
+            return;
         }
 
+        data = p + headerLen;
+
         if (hasMask)
         {
-            mask = data;
-            data += 4;
-            len -= 4;
             for (size_t i = 0; i < len; ++i)
                 data[i] = data[i] ^ mask[i % 4];
 
             // FIXME: copy and un-mask at the same time ...
-            _wsPayload.insert(_wsPayload.end(), data, data + std::min(payloadLen, len));
+            _wsPayload.insert(_wsPayload.end(), data, data + payloadLen);
         } else
-            _wsPayload.insert(_wsPayload.end(), data, data + std::min(payloadLen, len));
+            _wsPayload.insert(_wsPayload.end(), data, data + payloadLen);
+
+        T::_inBuffer.erase(T::_inBuffer.begin(), T::_inBuffer.begin() + headerLen + payloadLen);
+
         // FIXME: fin, aggregating payloads into _wsPayload etc.
         handleWSMessage(fin, code, _wsPayload);
         _wsPayload.clear();
@@ -225,7 +234,16 @@ public:
 
     virtual void handleWSMessage( bool fin, WSOpCode code, std::vector<char> &data)
     {
-        std::cerr << "Message: fin? " << fin << " code " << code << " data size " << data.size() << "\n";
+        std::cerr << "Message: fin? " << fin << " code " << code << " data size " << data.size();
+        if (code == WSOpCode::Text)
+        {
+            std::string text(data.begin(), data.end());
+            std::cerr << " text is '" << text << "'\n";
+
+            return;
+        }
+        else
+            std::cerr << " binary\n";
 
         // ping pong test
         assert (data.size() >= sizeof(size_t));


More information about the Libreoffice-commits mailing list