How to use the generated code: MsgBase.h

jwatte's picture

The generated code makes use of the pre-defined visitors in MsgBase.h and MsgBase.cpp. It also needs you to generate a source file for your packets file, using perl -MSource Packets.pl. The generated source code is simple:

/*
 * This file was auto-generated on 2008-11-15 11:16:13
 */
 
#include "Packets.h"
 
MsgRegistration<Msg_SystemGreeting> Msg_SystemGreeting::reg_("SystemGreeting", 1);
 
MsgRegistration<Msg_LoginRequest> Msg_LoginRequest::reg_("LoginRequest", 2);
 
MsgRegistration<Msg_LoginResult> Msg_LoginResult::reg_("LoginResult", 3);

To create a new message to send, you simply create an instance of your message class, and fill it out with your data. For example, to create a LoginRequest:

#include "Packets.h"
 
  Msg_LoginRequest myRequest;
  myRequest.id = 3;
  myRequest.name = "some name";
  myRequest.password = "qwerty";

To pack the message into an array of bytes, you use the visitors declared in Packets.h, and apply them to the worker objects declared in MsgBase.h:

#include "Packets.h"
#include "MsgBase.h"
 
  MsgOut myOutputMessage;
  myOutputMessage.Add(&myRequest);
 
  void const *myMessageAsBytes = myOutputMessage.Data();
  size_t sizeOfMyMessageAsBytes = myOutputMessage.Size();

If you send the buffer in question, then the other end will be able to pick this message apart and dispatch it appropriately -- see the next page for more.