JSONRPC over TCP/IP

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
mouser58907 Offline
Junior Member
Posts: 8
Joined: Apr 2012
Reputation: 0
Post: #1
I'm working on implementing a JSON RPC connection over TCP/IP in C# and I have one fundamental issue. Currently I'm using a synchronous approach, which works well.

I can send

Code:
{"id":1,"jsonrpc":"2.0","method":"Input.Home"}

and receive

Code:
{"id":1,"jsonrpc":"2.0","result":true}

This works with no problems. The issue arises when I receive notifications. These can arrive unpredictably and at any time. If a notification has been sent by XBMC, I receive multiple JSON requests at once. E.g.

{"jsonrpc":"2.0","method":"GUI.OnScreensaverActivated","params":{"data":null,"sender":"xbmc"}}{"jsonrpc":"2.0","method":"GUI.OnScreensaverDeactivated","params":{"data":null,"sender":"xbmc"}}

This causes a crash in JSON.NET, and understandably so. My first instinct is I need to asynchronously receive these notifications so that I don't have to wait until the next method is called to receive them. However this complicates the simple example I showed above because I can no longer utilize the synchronous calls. i.e.
Code:
SendJson(json);
result = ReceiveJson();

Is there a clean way to implement this without over complicating it? Any/All advice is appreciated.
find quote
Montellese Offline
Team-XBMC Developer
Posts: 3,672
Joined: Jan 2009
Reputation: 34
Location: Switzerland
Post: #2
I wrote a C# based JSON-RPC library a year or so ago and it was completely asynchronous over TCP and I didn't find it really complicating at all. If you don't want notifications you could use HTTP as a protocol. There you send a request and receive a response for that request.

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.

[Image: badge.gif]
find quote
mouser58907 Offline
Junior Member
Posts: 8
Joined: Apr 2012
Reputation: 0
Post: #3
Thanks Montellese, I saw your open source code based on HTTP. Would I need something along the lines of this for asynchronous?
Code:
public void Play()
{
    SendJson(PLAY_ID, Player.PlayPause(), activePlayer.id)
}

private void PlayResponse(JObject JSON)
{
    //Handle response here
}
find quote
mouser58907 Offline
Junior Member
Posts: 8
Joined: Apr 2012
Reputation: 0
Post: #4
Well, I switched to HTTP for now. I'd still like to implement a TCP based connection. If you'd be able to show me some of your code it would help out a lot. I think I have a pretty good idea of how to implement it, but I'd like to do it right the first time.My code will be open-source.
find quote
Montellese Offline
Team-XBMC Developer
Posts: 3,672
Joined: Jan 2009
Reputation: 34
Location: Switzerland
Post: #5
See https://github.com/Montellese/xbmc-jsonrpc-sharp but the API itself is very outdated but as that's not what you are interested in, feel free to take a look at the implementation of XbmcJsonRpcConnection.

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.

[Image: badge.gif]
find quote
mouser58907 Offline
Junior Member
Posts: 8
Joined: Apr 2012
Reputation: 0
Post: #6
Thanks, Montellese. That is very helpful. I hadn't considered using HTTP for function calls and TCP for Notifications.
find quote
willsmannar Offline
Junior Member
Posts: 1
Joined: Sep 2012
Reputation: 0
Post: #7
It is better go through TCP/IP basic socket communication http://vb.net-informations.com/communica...amming.htm hope this will help you in basic level.

wills.
find quote