Defining messages and handling with PROTOBUF

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.