Skip to content

.proto文件 ​

shell
# 生成.h和.cc文件
protoc --cpp_out=. *.proto
c++
syntax = "proto3";

package hello; // 命名空间 hello::Player

message Player
{
   int32 id = 1;
   int32 x = 2;
   int32 y = 3;
   int32 speed_x = 4;
   int32 speed_y = 5;
   string room_name = 6;
}
c++
syntax = "proto3";

message Address {
    string street = 1;
    string city = 2;
    string state = 3;
}

message Person {
    string name = 1;
    int32 age = 2;
    Address address = 3;
}

序列化与反序列化 ​

c++
// 序列化
Player new_player;
std::string new_player_str;
new_player.SerializeToString(&new_player_str);

// 反序列化 
Player player_data;
player_data.ParseFromString(std::string(node->_buf,node->_size));

学 习 记 录