Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ann0see opened this issue Nov 10, 2024 · 0 comments
Labels
feature request Feature request JSON-RPC Related to the JSON-RPC API

Comments

@ann0see
Copy link
Member

ann0see commented Nov 10, 2024

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 ann0see added feature request Feature request JSON-RPC Related to the JSON-RPC API labels Nov 10, 2024
@ann0see ann0see added this to the Release 3.12.0 milestone Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request JSON-RPC Related to the JSON-RPC API
Projects
None yet
Development

No branches or pull requests

1 participant