C++ 使用ProtoBuffer 心得
来源:互联网 发布:unity3d 城市夜景资源 编辑:程序博客网 时间:2024/06/03 18:10
最近要求使用ProtoBuffer来做作为服务器跟客户端传输数据的桥梁。
protobuffer比XML,JSON 好就不说了。
关键就说两个地方
为啥需要说着两个呢,因为他的序列化后在 最前面需要加上长度,而这个并不是真正的长度,是经过一个算法生成的长度。
1. 把本地的字符串转换成 protoBuffer
oCommonMsg.set_type(duolabo::CommonMessage_Type_LOGIN_REQUEST);
oCommonMsg.set_value(oLoginRequest.SerializeAsString());
string strCommonMsg = oCommonMsg.SerializeAsString();
int nLen = google::protobuf::io::CodedOutputStream::VarintSize32(strCommonMsg.length());
google::protobuf::uint8 *pLen = new google::protobuf::uint8[nLen];
google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(strCommonMsg.length(), pLen);
string st((const char *)pLen, nLen);
st += strCommonMsg;
delete [] pLen;
SendTcpSocket(st.c_str(), st.size());
oCommonMsg.set_value(oLoginRequest.SerializeAsString());
string strCommonMsg = oCommonMsg.SerializeAsString();
int nLen = google::protobuf::io::CodedOutputStream::VarintSize32(strCommonMsg.length());
google::protobuf::uint8 *pLen = new google::protobuf::uint8[nLen];
google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(strCommonMsg.length(), pLen);
string st((const char *)pLen, nLen);
st += strCommonMsg;
delete [] pLen;
SendTcpSocket(st.c_str(), st.size());
2. 把protoBuffer转换成 本地字符串。
google::protobuf::uint32 uiLength = 0;
google::protobuf::io::CodedInputStream is((google::protobuf::uint8 *)pResvBuf, nRecvBytes);
is.ReadVarint32(&uiLength);
char *pBuf = new char[uiLength];
is.ReadRaw(pBuf, uiLength);
duolabo::CommonMessage oCommonMsg;
string strCommonMsg(pBuf, uiLength);
delete [] pBuf;
if(oCommonMsg.ParseFromString(strCommonMsg))
google::protobuf::io::CodedInputStream is((google::protobuf::uint8 *)pResvBuf, nRecvBytes);
is.ReadVarint32(&uiLength);
char *pBuf = new char[uiLength];
is.ReadRaw(pBuf, uiLength);
duolabo::CommonMessage oCommonMsg;
string strCommonMsg(pBuf, uiLength);
delete [] pBuf;
if(oCommonMsg.ParseFromString(strCommonMsg))
0 0
- C++ 使用ProtoBuffer 心得
- protobuffer安装和使用
- ProtoBuffer 使用简要介绍
- go 使用protobuffer proto3
- protobuffer简介及使用
- go 使用protobuffer proto3
- protobuffer安装和使用
- protobuffer
- 使用PrintToString打印protobuffer包
- cocos2d-lua 下使用protobuffer
- win protobuffer使用简易教程
- Unity中使用Protobuffer探讨
- unity中ProtoBuffer的使用
- UltraWinGrid使用心得(C#)
- C#--BackgroundWorker使用心得
- UltraWinGrid使用心得(C#)
- 将protobuffer文件转换为objective-c
- Mac下安装使用Google ProtoBuffer
- 分时操作系统和多道程序操作系统的区别
- week8---10月27日 DIV布局定位(二)
- android 网络编程(一、URL)
- 20 issues of porting C++ code on the 64-bit platform
- 软件测试基础课程学习笔记1--软件测试简介
- C++ 使用ProtoBuffer 心得
- 14.4 Using Index-Organized Tables forPerformance 使用索引组织的表
- 前后端分离的思考与实践(五)
- jquery的$().each,$.each的区别
- response.setContentType()的作用及参数
- ActivityThread
- linux(ubuntu14.04)+GPU+cuda6.5+caffe+openCV2.4.9+matlab2014a+python的新手配置(修改版)
- 非哥指导思想
- nagios监控windows主机