工具常用系列 3:消息创建和解析

废话不多说

前文的your_tool_cli_msg.proto 文件生成 your_tool_cli_msg.pb.h/your_tool_cli_msg.pb.cc

Message.h


#ifndef YOUR_TOOL_MESSAGE_H_
#define YOUR_TOOL_MESSAGE_H_

#include <zmq.hpp>
#include <string>
#include "your_tool_cli_msg.pb.h"

using TPLPROTO = your_tool_proto::your_tool_climsg;

class Message final
{
public:
    Message() = default;
    ~Message() = default;

    void createReqMessage(const std::string& cmd,
                          const your_tool_proto::CliCommand_Type cliCommandType,
                          const your_tool_proto::Request_ReqType reqType,
                          const std::string& file_content,
                          const std::string& module,
                          const std::string& filename);

    void createRspMessage(const your_tool_proto::CliCommand_Type cliCommandType,
                          const your_tool_proto::Response_Status respStatus,
                          const std::string& xmlData,
                          const std::string& errorInfo);

    // Used when receives
    void parseMessage(const zmq::message_t &zmsg);

    zmq::message_t convert2ZmqMessage() const;

    const your_tool_proto::climsg &getProtoMsg() const
    { return msg_; }

private:
    your_tool_proto::climsg msg_;
};


#endif //YOUR_TOOL_MESSAGE_H_

Message.cpp


#include <google/protobuf/text_format.h>
#include <zmq.hpp>
#include <iostream>
#include "Message.h"
//请求消息组装
void Message::createReqMessage(const your_tool_proto::CliCommand_Type cliCommandType,
                               const your_tool_proto::Request_ReqType reqType,
                               const std::string& file_content,
                               const std::string& module,
                               const std::string& filename)
{
    msg_.mutable_command()->set_type(cliCommandType);
    msg_.mutable_request()->set_reqtype(reqType);
    msg_.mutable_request()->set_file_content(file_content);
    msg_.mutable_request()->set_module(module);
    msg_.mutable_request()->set_filename(filename);

}
//响应消息组装
void Message::createRspMessage( const your_tool_proto::CliCommand_Type cliCommandType,
                                const your_tool_proto::Response_Status respStatus,
                                const std::string& xmlData,
                                const std::string& errorInfo)
{
    msg_.mutable_command()->set_type(cliCommandType);
    msg_.mutable_response()->set_status(respStatus);
    msg_.mutable_response()->set_xmldata(xmlData);
    msg_.mutable_response()->set_errorinfo(errorInfo);
}
//序列化这里很好用
zmq::message_t Message::convert2ZmqMessage() const
{
    zmq::message_t zmqmsg(msg_.ByteSizeLong());
    msg_.SerializeToArray(zmqmsg.data(), zmqmsg.size());

    return zmqmsg;
}
// parse也好用
void Message::parseMessage(const zmq::message_t &zmsg)
{
    if (!msg_.ParseFromArray(zmsg.data(), zmsg.size()))
    {
        std::cout <<"MSG parse failed, size="<< zmsg.size()<<std::endl;
    };
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值