Skip to content

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

@ann0see

Description

@ann0see

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.

Metadata

Metadata

Assignees

Labels

No fields configured for Feature.

Projects

Status

Triage

Relationships

None yet

Development

No branches or pull requests

Issue actions