Handlers - Pawn.RakNet
Other Pawn.RakNet tutorials: RPC - Pawn.RakNet | Packets - Pawn.RakNet
With handlers, you can assign a callback to handle packets and RPCs. Basically, it would be the same as using them in the main callbacks (OnIncomingRPC/Packet and OnOutcomingRPC/Packet), except only the RPC/Packet you assigned to that specific callback will reach it.
Handler types:-PR_INCOMING_RPC: RPCs sent by the client to the server.Function:
-PR_INCOMING_PACKET: Packets sent by client to the server.
-PR_OUTCOMING_RPC: RPCs sent by the server to the cliente.
-PR_OUTCOMING_PACKET: Packets sent by the server to the client.
id - The packet/RPC id you are going to create the handler for.Code:PR_RegHandler(id, const publicname[], PR_HandlerType:type)
publicname - Callback used by the handler.
type - Handler type.
Creating a handler
To create a handler is pretty simple. Take into account that the handler's callback must have two parameters, one for the player receiving/sending the packet/RPC (playerid) and another one for the BitStream (BitStream:bs).
The example above is pretty easy to understand, we can manipulate the packet 207 (on foot sync) through OnFootSync callback.PHP Code:
#define ID_PLAYER_SYNC 207
public OnGameModeInit()
{
PR_RegHandler(ID_PLAYER_SYNC, "OnFootSync", PR_INCOMING_PACKET);
return 1;
}
forward OnFootSync(playerid, BitStream:bs);
public OnFootSync(playerid, BitStream:bs)
{
new On_FootSync[PR_OnFootSync];
BS_IgnoreBits(bs, 8);
BS_ReadOnFootSync(bs, On_FootSync);
printf("My weapon id is: %i", On_FootSync[PR_weaponId]);
return 1;
}
I'm not going to explain why you should ignore 8 bits, that is something I explained in my last Pawn.RakNet tutorial about packets. I recommend you to read my last two tutorials about RPCs and Packets before reading this thread.
Errors related with handlers
These are two errors that might show up in your logs/console:
n_PR_RegHandler: Public "YourCallbackName" does not exist. - Pretty obvious, the callback you assigned to your handler does not exist.n_PR_RegHandler: invalid array(T, N) subscript - Your RPC/Packet ID is undefined. For example, your RPC id is above MAX_RPC_MAP_SIZE (254) which is undefined/invalid.
Considerations
- Take into account that OnIncomingPacket/RPC and OnOutcomingPacket/RPC callbacks will always be called before any handler's callback. So, if you return 0 in the main callbacks, handler callbacks won't be called. The same applies to other handlers, if you have two handlers for the same packet and return 0 in the one you created first, the other one's callback won't be called as well.
-If you create a handler and not assign any callback to it (leave the 2nd parameter empty/null), the server will crash.