to myself 的分类学习日志

做自己想做的事
posts - 232, comments - 6, trackbacks - 0, articles - 0

导航

<2025年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

留言簿(278)

随笔分类

随笔档案

Internet Industry News

  • 《程序员》官网
  • 中国最具影响力、最权威IT专业技术期刊:《程序员》杂志
  • CSDN.NET
  • 全球最大中文IT社区,为IT专业技术人员提供最全面的信息传播和服务平台
  • InfoQ
  • 提供中立的、由技术实践者主导的会议、内容与在线社区
  • 爱范儿
  • 爱范儿(ifanr)全景关注移动互联网、集中报道创业团队,最潮的智能手持及最酷的互联网应用,对业界生态、智能产品及移动应用有着深刻的理解,致力于“独立,前瞻,深入”的原创报道和分析评论,将大量第一手新酷理念和信息传达到读者。
  • 博客 - 伯乐在线
  • 专注于分享职业相关的博客文章、业界资讯和职业相关的优秀工具和资源。
  • 博客园 - 新闻区
  • 面向软件开发者的高品质IT技术社区。
  • 酷壳 – CoolShell.cn
  • 分享技术见闻,知识,趋势的网站。
  • 外刊IT评论网
  • 以翻译外国IT方面的评论、访谈为主,没有确定的对象,偏重于软件方面,通过那些独特的海外IT视野,关注IT世界,关切IT民生,锐评IT世事。
  • 月光博客
  • 关注互联网和搜索引擎的IT科技博客

Internet Technology

Open Source

搜索

  •  

最新评论

阅读排行榜

评论排行榜

protobuf的使用

Posted on 2010-12-02 14:21 kongkongzi 阅读(12489) 评论(2)  编辑 收藏 引用 所属分类: network programming

使用protobuf定义消息

下载protobuf-2.3.0:
    http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
安装:
unzip protobuf-2.3.0.zip
cd protobuf-2.3.0
./configure
make
make check
make install

结果:
Libraries have been installed in:
   /usr/local/lib
Head files hava been installed in:
/usr/local/include/google/
protobuf/


开始写.proto文件:
BaseMessage.proto:
message MessageBase
{
    required int32 opcode = 1;
    // other: sendMgrId, sendId, recvMgrId, recvId, ...
}

message BaseMessage
{
    required MessageBase msgbase = 1;
}

BaseMessage.proto是其它消息proto文件的基础,以容器模块的C2S_GetContainerInfo为例:
ContainerMessage.proto:
import "BaseMessage.proto";

message C2SGetContainerInfoMsg
{
    required MessageBase msgbase = 1;
    optional int32 containerType = 2;
}

.proto文件编写规则
1)所有消息都需要包含msgbase这项,并编号都为1,即:
  required MessageBase msgbase = 1;
2)除了msgbase这项写成required外,其它所有项都写成optional。

编译 .proto 文件
protoc -I=. --cpp_out=. BaseMessage.proto
protoc -I=. --cpp_out=. ContainerMessage.proto
生成BaseMessage.pb.h、BaseMessage.pb.cc
    ContainerMessage.pb.h、ContainerMessage.pb.cc
将它们添加到工程文件中。

编写C++代码:
1)发送消息:
C2SGetContainerInfoMsg msg;
msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);
msg.set_containertype(1);
std::string out = msg.SerializeAsString();
send(sockfd, out.c_str(), out.size(), 0);
2)接收消息
char buf[MAXBUF + 1];
int len;
bzero(buf, MAXBUF + 1);
len = recv(new_fd, buf, MAXBUF, 0);
if (len > 0)
{
    printf("%d接收消息成功:'%s',共%d个字节的数据\n",
            new_fd, buf, len);

    BaseMessage baseMsg;
    std::string data = buf;
    baseMsg.ParseFromString(data);

    int opcode = baseMsg.mutable_msgbase()->opcode();

    printf("opcode=%d\n", opcode);

    switch (opcode)
    {
    case C2S_GetContainerInfo:
    {
        C2SGetContainerInfoMsg msg;
        msg.ParseFromString(data);

        printf("containerType=%d\n", msg.containertype());

        break;
    }
    default:
    {
        break;
    }
    }
}
else
{
    if (len < 0)
        printf("消息接收失败!错误代码是%d,错误信息是'%s'\n",
             errno, strerror(errno));
    close(new_fd);
    return -1;
}


编译C++代码:
Need to link lib:
protobuf
pthread


参考:
1, Google Protocol Buffer 的使用和原理: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html?ca=drs-
2,http://code.google.com/p/protobuf/

Feedback

# re: protobuf的使用[未登录]  回复  更多评论   

2011-04-29 15:43 by me
把MessageBase写到proto里未必是好事(虽然数据可以动态变化),这会导致Message到BaseMessage的强制转换(发送消息时需要修改MessageBase的数据),protoc生成的类里都有XXX::MessageBase这一项。可以定义一个C++消息类:
class NetMessage
{
public:
MessageBase msgBase;
std::string msgData;
};

.proto里只定义具体的具体,serialize成string后发送。


# re: protobuf的使用  回复  更多评论   

2011-06-17 11:56 by lishirong
http://shop66918493.taobao.com 网上充话费、Q币,各类游戏点卡 ,优惠多多!

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理