jamulus icon indicating copy to clipboard operation
jamulus copied to clipboard

Add non line by line JSON-RPC parsing for clients which do not send line by line

Open ann0see opened this issue 1 year ago • 0 comments

What is the current behaviour and why should it be changed?

Currently, a new JSON-RPC message is only interpreted if the JSON-RPC client sends a new line. However, there seem to be clients which do not send a new line per new message. An example would be e.g. https://github.com/tesseract-one/JsonRPC.swift

Describe possible approaches

in rpcserver.cpp, parse for "{" and "}" alongside "\n" - maybe we also need to check for windows line endings.

Something like:

-        while ( pSocket->canReadLine() )
+        while ( pSocket->bytesAvailable() > 0 )
         {
-            QByteArray line = pSocket->readLine();
+            // read line or until we reach the same number of closing "}" as opening "{" which represents one message.
+            // Some clients may not send line by line
+
+            QByteArray line;
+            int        numBrackets = 0;
+            while ( pSocket->bytesAvailable() > 0 )
+            {
+                QByteArray readByte = pSocket->read ( 1 );
+                if ( readByte.at ( 0 ) == '{' )
+                {
+                    numBrackets++;
+                }
+                else if ( readByte.at ( 0 ) == '}' && numBrackets > 0 )
+                {
+                    numBrackets--;
+                }
+               
+               if ( readByte.at ( 0 ) == '}' && numBrackets == 1 )
+                {
+                    // message most likely ended
+                    numBrackets = 0;
+                    line.append ( '}' );
+                    break;
+                }
+                else if ( readByte.at ( 0 ) == '\n' )
+                {
+                    // new line character means we are finished with this transmission by definition
+                    break;
+                }
+                line.append ( readByte );
+            }

Has this feature been discussed and generally agreed?

No.

ann0see avatar Nov 10 '24 22:11 ann0see