Jump to content

  • Log In with Google      Sign In   
  • Create Account


Strongly typed dynamic function calls


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Sharsnik   Members   -  Reputation: 136

Like
0Likes
Like

Posted 30 April 2014 - 10:51 PM

I'm trying to come up with a nice system for syncing RPC calls on a client-server system (in C#).

 

Currently a call looks something like this:

static public void swapItems(int itemIndex1, int itemIndex2)
{
sendRPC("swapItems", Network.player, itemIndex1, itemIndex2);
}
 
And then the server interprets the data like so:

protected void swapItems(NetworkPlayer user, int itemIndex1, int itemIndex2)
{
User tempUser = mUserMap[user];
 
Item item1 = tempUser.getItem(itemIndex1);
Item item2 = tempUser.getItem(itemIndex2);
 
tempUser.swapItems(item1, item2);
}

 

This system works okay, since the call wrapper is strongly typed. But there are still a few problems. One is that the actual RPC call isn't strongly typed, so if I change the definition on the server side, the compiler doesn't flag the RPC call as invalid. The second, is that it basically doubles the code required to create all the call wrappers.

 

I'm wondering if there is an easier, cleaner way to create the call wrapper. One solution would be dynamically creating the code based on the server's call definition, but that seems like a bit of a hassle to work into the work-flow.

 

Anyone have a better idea, and/or a library that already does this?



Sponsor:

#2 Krohm   Crossbones+   -  Reputation: 3049

Like
0Likes
Like

Posted 01 May 2014 - 12:21 AM

A thing I did is to have a struct for each message and abuse template to properly load/parse/dispatch everything (C++). This however does not even consider the problem you're considering!

I'm keeping an eye on this thread.



#3 SeanMiddleditch   Members   -  Reputation: 5117

Like
2Likes
Like

Posted 01 May 2014 - 12:36 AM

I typically rely on a code genration approach. Define the RPC in an IDL-like file, have it generate the code, and go. Ensure client and server build versions match during the initial connection handshake to avoid accidental protocol errors.

There are many existing serialization + RPC protocols. You can build them with protocol buffers, cap'n proto, or quite a few others.

#4 Sharsnik   Members   -  Reputation: 136

Like
0Likes
Like

Posted 01 May 2014 - 01:28 AM


Define the RPC in an IDL-like file, have it generate the code, and go.

 

That sounds reasonable. I'm wondering though - just for the sake of the workflow - if it might make more sense to generate the files based on the initial class definition (I.E. C++ or C# code). Do you know of any libraries that can parse code into memory like that? Once the code is loaded, the generation should be reasonably trivial, I would think.



#5 Sharsnik   Members   -  Reputation: 136

Like
0Likes
Like

Posted 01 May 2014 - 05:52 AM

So, I've done some reading, and while this theory is not yet completely formed, it seems to me that using std::bind and/or closures may be a better way to go about this.

 

The closure encapsulates the function call, and is then serialized to some sort of stream to be sent over the network. In C# you can pretty easily serialize the closure with Reflection (I would assume), not 100% sure how you'd go about it in C++, though. The data in the closure is strongly typed though, so I imagine it is possible.

 

I shall continue working on the problem with this approach.



#6 SeanMiddleditch   Members   -  Reputation: 5117

Like
0Likes
Like

Posted 01 May 2014 - 09:30 AM

Define the RPC in an IDL-like file, have it generate the code, and go.

 
That sounds reasonable. I'm wondering though - just for the sake of the workflow - if it might make more sense to generate the files based on the initial class definition (I.E. C++ or C# code). Do you know of any libraries that can parse code into memory like that? Once the code is loaded, the generation should be reasonably trivial, I would think.


C# has tools for this. If your client and server are both in C#, you could just write some annotations that you attach to the structs that helps it work over the network. protobuf-net does this, for instance. This is much less useful if you are using multiple languages.

So, I've done some reading, and while this theory is not yet completely formed, it seems to me that using std::bind and/or closures may be a better way to go about this.


On the C++ side at least, std::bind does nothing useful for you. It lets you bind the find/closure, but it has nothing to do with actually serializing data over the network. There are serialization libraries for C++, but they don't work by binding a function in std::bind or a lambda. There is absolutely no way to automatically generate serialization data for C++ without some kind of external tool.

Generating the code from an RPC-IDL of some sort is a lot easier than any alternatives, especially if you use a pre-existing tool.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS