Strongly typed dynamic function calls

Started by
4 comments, last by SeanMiddleditch 10 years ago

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?

Advertisement

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.

Previously "Krohm"

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.

Sean Middleditch – Game Systems Engineer – Join my team!


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.

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.

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.

Sean Middleditch – Game Systems Engineer – Join my team!

This topic is closed to new replies.

Advertisement