菜鸟笔记
提升您的技术认知

protobuf repeated的用法-ag真人游戏

//user.proto
package protomessage;
message user
{
    required uint32 id = 1;
    required string name = 2;
}
message userlist
{
    repeated user user = 1;
}
message roomlist
{
    repeated string name = 1;
}

编译成user.pb.h和user.pb.cpp文件

cd protoc.exe所在的目录

protoc -i=. --cpp_out=. user.proto

配置vs2013环境变量

1、头文件目录

2、库目录

3、库文件名

测试:

// messages.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include 
#include "messages.pb.h"
#include "user.pb.h"
//using namespace messagesuites;
int _tmain(int argc, _tchar* argv[])
{
	messagesuites::roomchatrequest request;
	request.set_room("guangzhou");
	request.set_msg("hello world!");
	protomessage::userlist listuser;
	protomessage::user* puser;
	puser = listuser.add_user();
	puser->set_id(10001);
	puser->set_name("zhang wuji");
	puser = listuser.add_user();
	puser->set_id(10002);
	puser->set_name("zhou zhiruo");
	puser = listuser.add_user();
	puser->set_id(10003);
	puser->set_name("zhao ming");
	std::cout << listuser.user(0).id() << ": " << listuser.user(0).name() << std::endl;
	std::cout << listuser.user(1).id() << ": " << listuser.user(1).name() << std::endl;
	std::cout << listuser.user(2).id() << ": " << listuser.user(2).name() << std::endl;
	char szbuffer[1024];
	listuser.serializetoarray(szbuffer, sizeof(szbuffer));
	protomessage::userlist listuser2;
	listuser2.parsefromarray(szbuffer, strlen(szbuffer)   1);
	std::cout << std::endl;
	std::cout << listuser2.user(0).id() << ": " << listuser.user(0).name() << std::endl;
	std::cout << listuser2.user(1).id() << ": " << listuser.user(1).name() << std::endl;
	std::cout << listuser2.user(2).id() << ": " << listuser.user(2).name() << std::endl;
	protomessage::roomlist listroom;
	listroom.add_name("guangdong");
	listroom.add_name("shenzhen");
	listroom.add_name("dongguang");
	memset(szbuffer, 0, sizeof(szbuffer));
	listroom.serializetoarray(szbuffer, sizeof(szbuffer));
	protomessage::roomlist listroom2;
	listroom2.parsefromarray(szbuffer, strlen(szbuffer)   1);
	std::cout << std::endl;
	std::cout << listroom2.name(0) << " " << listroom2.name(1) << " " << listroom2.name(2) << std::endl;
	getchar();
	return 0;
}
网站地图