-
Notifications
You must be signed in to change notification settings - Fork 1
Handling a Packet
Inevitably, you’ve seen the message! That dreaded “Unhandled packet: [blah blah blah]” that happens whenever we receive a packet that GoGo doesn’t understand how to handle yet. This section will walk you through getting rid of that message and actually handling the packet.
So you’ve seen the message, eh? Let’s pretend that message you just saw was referring to Match_Login. What do you do now? Well, lucky for you, in most cases MAIET tells you! If you look at the name, by adding a “Response” after the underscore you can usually figure out what packet the client expects back. In this case, that would be Match_ResponseLogin. By looking in include/cockpit/packet/protocol we see that, indeed, this is a valid packet. Therefore, the client is sending the Match_Login packet and is expecting Match_ResponseLogin packet in return. Fair enough.
To write a handler for Match_Login, you need to hook the function. This can be done by modifying GoGoClient::initialize() (in gogo/src/GoGoClient.cpp) to add a function to the registry. BUT WAIT. We don’t yet HAVE a function to add to the registry!
To write your handler, make a new method in GoGoClient (in gogo/src/GoGoClient.h). This must take the same parameters as dictated by the boost::function in the registry that you are overloading (check the definition of cockpit::packet::Registry::Match_Login). Name it something appropriate. Then, add a new file to gogo/src/modules with the same name as the method you just added. Place the definition of the method in that file, totally isolated from the other handlers. Just write a stub for now.
Now that you have your handler modify GoGoClient::initialize() to hook your method. For the purpose of this guide, the method name will be OnMatchLogin. If you don’t know how to use Boost.Bind, I suggest learning now by clicking that link. To set up the function, add the following line:
registry->Match_Login = bind(&GoGoClient::OnMatchLogin, this, _1, _2, _3, _4);
This can be translated as…
- Hook the
Match_Loginhandler - Set it up to redirect to
this->OnMatchLogin. -
OnMatchLogintakes 4 parameters, identical in position and number asregistry->Match_Login.
And finally, you’re done! Now you can get writing actual code in your brand new handler! But we still need to send a response back. This can be done with careful use of include/cockpit/packet/protocol/. In this directory, we have thousands of possible packets you can send back. Just #include the one(s) you need, and call transmitter->send() on it. In this case, it might look something like this:
// Other stuff. Everything went okay!
transmitter->send(cockpit::packet::protocol::Match_ResponseLogin(PEC_NONE, ..., ...));
Usually, the first parameter to the packet is the error code. If everything went fine, just pass in PEC_NONE. Otherwise, there’s usually an error to suit your needs in gogo/src/error_codes.h.
And now you’re really done. You have recieved the Match.Login packet, processed it accordingly, and sent a response back with Match_ResponseLogin. This might seem like a lot of work, but once you get the hang of it, you start to appreciate the beauty of the modularity it affords.