用protobuf定義訊息及處理

來源:互聯網
上載者:User
用protobuf定義訊息及處理

(金慶的專欄)

訊息定義:

package MsgPb;

message Msg {
  required string type = 1;  // Full type name of data.
  required bytes data = 2;  // Serialized bytes fo concrete msg.
}

訊息發送代碼:

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::protobuf::Message & msg) const
{
    MsgPb::Msg msgSend;
    msgSend.set_type(msg.GetTypeName());
    bool bSuc = msg.SerializeToString(msgSend.mutable_data());
    BOOST_ASSERT(bSuc);
    Send(sDest, msgSend);
}

訊息接收後分發:

void HandleOneMsg(const string & sFrom, const string & sMsg)
{
    MsgPb::Msg msg;
    bool bSuc = msg.ParseFromString(sMsg);
    if (!bSuc) return;
    MsgDispatcher::Dispatch(sFrom, msg);
}

void MsgDispatcher::Dispatch(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::ParseMsg(msg.type(), msg.data());
    if (pMsg)
        (*itr).second(sFrom, *pMsg);
}

訊息解析:

typedef google::protobuf::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::protobuf;
    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

註冊處理器:

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是個具體的訊息。MsgPb::Msg是個訊息封裝。
它們都是google::protobuf::Message的子類。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.