RPC Overview

Overview
Normally, when you send a network message you perform four steps:

1. Create a packet to hold your data
2. Write a function to encode and send that data
3. Create a packet identifier so the recipient knows which function to call
4. Write a function to decode and handle that data.

These four steps can be quite cumbersome and force you to focus on the process of networking rather than writing your game.
Since the focus of RakNet is to allow you, the end-user, to quickly develop applications, Remote Procedure Calls were built into RakNet to streamline this process down to what you actually need in your code:

1. Encode your data
2. Call a function on the remote system with that data

Here is the complete process of how to implement RPC in your game.
Step 1: Tell the network system to allow a function to be called using RPC:
You can't just call any function on the remote system with RPC, or else this would open up your servers to hacks. You must first tell the network system to allow a function, defined with a particular prototype, to be called. This is done as follows:
// Define a C function to be called
void MyFunction(char *input, int numberOfBitsOfData, PlayerID sender) {}
// A pointer to the client
RakClient *rakClient;
// A macro to assign the function to the client
REGISTER_AS_REMOTE_PROCEDURE_CALL(rakClient, MyFunction);
You use a similar process for the server.
Note that function names must be composed of a string with only characters from a-z and is not case sensitive. This is done for encryption and compression reasons.
Step 2: (Optional) Encode your data
The RPC method of your server or client can take either a (char*) with a length or a Bitstream. This is equivalent to creating your packet.
Step 3: Call the RPC method
The RPC method of your server or client can take either a (char*) with a length or a Bitstream. Note that the parameters for the server are the same as with Send and will handle timestamping for you the same way.
Step 4: Handle the call on the remote system
Assuming all went well, you will get a packet with the identifier ID_RPC. Pass this directly to your server or client method "HandleRPC." The function you specified on the remote system will now be called. If it wasn't called it could be several things:

- You didn't register the function as callable
- You called a different function, or mistyped the name
- One of the normal reasons a packet wouldn't arrive

See Samples/CodeSamples/RemoteProcedureCalls/RPC.cpp for a complete example.

RPC Function prototype

void functionName(char *input, int numberOfBitsOfData, PlayerID sender)
All RPC functions should be C functions that use the specified prototype. input will point to a stream of bytes that is whatever data you passed when you made the call. numberOfBitsOfData is just that - how many bits you sent. Note that if you parse this to a BitStream constructor, the BitStream constructor wants bytes of data. To convert bits of bytes, assuming you have at least one bit, use (numberOfBits-1)/8+1. The last parameter, sender, is UNASSIGNED_PLAYER_ID for the server or the PlayerID of a particular client.
As stated above, function names must be composed of a string with only characters from a-z and is not case sensitive.

See Also

Index Creating Packets Sending Packets Receiving Packets Bitstreams