Kodi Community Forum

Full Version: Separating responses on TCP stream?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

for fun, I am working on a little app that talks to the JSONRPC server of XBMC (Eden). I have picked TCP as the transport of choice, as it gives me a single connection to the server through which I can both receive notifications as well as execute methods.

All is working pretty much as expected, except that if I fire requests too quickly in succession, I'm getting some of the responses "glued" to one another in my receive buffer. Ie.

Code:
Sent:  "{ "id" : 476707713, "jsonrpc" : "2.0", "method" : "Player.GetActivePlayers" }"
Sent:  "{ "id" : 1186278907, "jsonrpc" : "2.0", "method" : "Player.GetItem", "params" : { "playerid" : 0 } }"
Sent:  "{ "id" : 505671508, "jsonrpc" : "2.0", "method" : "Player.GetProperties", "params" : { "playerid" : 0, "properties" : [ "percentage" ] } }"
Received:  "{"id":476707713,"jsonrpc":"2.0","result":[{"playerid":0,"type":"audio"}]}"
Received:  "{"id":1186278907,"jsonrpc":"2.0","result":{"item":{"id":2479,"label":"Phosphene","type":"song"}}}{"id":505671508,"jsonrpc":"2.0","result":{"percentage":32.47583770751953125}}"

If I add small delays (say 10ms) between the calls, it all executes as expected, all depending on client processing speed and network of course. But I am pretty sure this is more of race condition, and will also occur when I just receive a notification just when I receive a response as well.

So, I wonder if XBMC could somehow identify the discrete responses on the stream?

Thanks!

Ernst.
You are using TCP so you get a raw stream of data. Adding anything in between the responses and notifications will not happen. This has been discussed several times before. What you need to do is to count the { and } in the data you get (or use a JSON library which does that for you) to know when one response/notification ends and the next one starts.
Cool, thanks for the quick response. I did do some searching up front in this forum, but couldn't find anything related, hence my question!

Not sure if I can leverage my JSON parser here, but a curly brace counter shouldn't be too hard.

Thanks again!
By no means production code, but here's a little Qt snippet that works for now:

Code:
QByteArray data(tcpSocket->readAll());
    const QByteArray splitter("}{");
    while(data.contains(splitter)) {
        qDebug() << "MULTI-BALL!";
        // find position of the end of the object
        int endPosition = data.indexOf(splitter) + 1;
        // parse the object
        parseData(data.left(endPosition));
        // remove the parsed data
        data.remove(0, endPosition);
    }
    parseData(data);

I also added a small note to the wiki.

Cheers,

Ernst.
Hi Ernste,

Would you be willing to share the code you use to create the connection and send the commands to XBMC.

I am trying a similar project. I designed an interface in VB.Net a long while ago using the old HTTP API. I want to re-develop the project in c# using JSON and a TCP connection. I am having real problems getting started. I can establish a connection to XBMC without a problem. I get stuck from that point.

Thanks in advance

Andy