How to re-construct messages on the receiving side

jwatte's picture

Once you've gotten your message buffer sent to a receiving end (through a file or network interface), you can inflate and re-construct the same data structure using the MsgIn class. Note that the current marshaling code does not compensate for varying byte order across networks, but because all computers you'll be running this code on are little-endian, that probably doesn't matter to you. (If you have native development kits for systems like the PS/3 or Xbox 360, then you'll have to worry about it)

  MsgIn myMsg(theDataIReceived, sizeOfDataReceived);
  while (MsgBase *base = myMsg.Message()) { // the library allows multiple messages in one packet
    switch (base->GetCode()) {
    case Msg_SystemGreeting::Code:
      // do whatever with a SystemGreeting
      HandleSystemGreeting(*static_cast<Msg_SystemGreeting *>(base));
      break;
    case Msg_LoginRequest::Code:
      // do whatever with a LoginRequest
      HandleLoginRequest(*static_cast<Msg_LoginRequest *>(base));
      break;
    case Msg_LoginResult::Code:
      // do whatever with a LoginResult
      HandleLoginResult(*static_cast<Msg_LoginResult *>(base));
      break;
    }
  }

Hopefully, these pages have helped you understand how to structure and use a simple marshaling library to go between arrays of bytes (packets) and native data structures.

Comments

Thank you

Thank you for sharing this! Great system and code!