Defining messages and handling with PROTOBUF
(Jin Qing's column)
Message definition:
Package MSGPB;
Message MSG {
Required String type = 1; Full type name of data.
Required bytes data = 2; Serialized bytes fo concrete msg.
}
Message Sending code:
void Msgsender::send (const std::string & sdest, const MSGPB::MSG & MSG)
{
std::string s;
BOOL Bsuc = Msg. Serializetostring (&s);
Boost_assert (BSUC);
Send (sdest, s);
}
void Msgsender::send (const std::string & Sdest,
Const Google::p rotobuf::message & msg) const
{
Msgpb::msg Msgsend;
Msgsend.set_type (Msg. Gettypename ());
BOOL Bsuc = Msg. Serializetostring (Msgsend.mutable_data ());
Boost_assert (BSUC);
Send (Sdest, msgsend);
}
Distribution after message receipt:
void Handleonemsg (const string & sfrom, const string & SMSG)
{
Msgpb::msg MSG;
BOOL Bsuc = Msg. Parsefromstring (SMSG);
if (!BSUC) return;
Msgdispatcher::D ispatch (Sfrom, msg);
}
void Msgdispatcher::D ispatch (const string & sfrom, const MSGPB::MSG & MSG)
{
Handlermap::const_iterator ITR = S_maphandlers.find (Msg.type ());
if (ITR = = S_maphandler.end ()) return;
Msgpb::googlemsgptr PMSG = msgpb::P arsemsg (Msg.type (), Msg.data ());
if (PMSG)
(*ITR). Second (Sfrom, *pmsg);
}
Message resolution:
typedef google::p rotobuf::message googlemsg;
typedef boost::shared_ptr<googlemsg> GOOGLEMSGPTR;
ParseMsg.cpp
#include "ParseMsg.h"
#include <google/protobuf/descriptor.h>
#include <googlt/protobuf/message.h>
namespace {
Using Msgpb::googlemsgptr;
Googlemsgptr greatemsg (const string & stypename)
{
Using namespace Google::p rotobuf;
CONST DESCRIPTOR * Pdescriptor = Descriptorpool::generated_pool ();
->findmessagetypebyname (Stypename);
if (NULL = = pdescriptor)
return Googlemsgptr ();
Const message * Pprototype = Messagefcatory::generated_factory ();
->getprototype (Pdescriptor);
if (NULL = = Pprototype)
return Googlemsgptr ();
Return Googlemsgptr (Pprototype->new ());
}
}//Namespace
Namespace MSGPB {
Googlemsgptr parsemsg (const string & stypename, const string & sData)
{
Googlemsgptr PMSG = createmsg (stypename);
if (!pmsg) return googlemsgptr ();
BOOL Bsuc = pmsg->parsefromstring (SData);
if (BSUC) return PMSG;
return Googlemsgptr ();
}
}//Namespce MSGPB
Registering the Processor:
void Msgdispatcher::init ()
{
using namespace MSGPB;
Inserthandler (Lobbyregistermsg (), Lobbyregistermsghandler ());
Inserthandler (Loginmsg (), Loginmsghandler ());
...
}
void Msgdispatcher::inserthandler (const MSGPB::GOOGLEMSG & msg,
Const Msghandler & H)
{
S_maphandlers[msg. Gettypename ()] = h;
}
Msgpb::loginmsg is a concrete message. MSGPB::MSG is a message encapsulation.
They are all a subclass of Google::p rotobuf::message.
Go from: http://blog.csdn.net/jq0123/article/details/7824168