DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

protobuf的简单的使用,不过还留下了一个问题,那就是之前主要介绍的都是对简单数据的赋值,简单数据直接采用set_xx()即可,但是如果不是简单变量而是自定义的复合类型变量,就没有简单的set函数调用了,下面看一个简单的例子。

在网络游戏中,游戏玩家之间的同步是一个最基本的功能,而同步是通过对坐标的广播进行的,因此我们假设一个简单的模型,当一个玩家的位置发生变化时,将玩家的新位置发给地图内所有玩家,根据这个情况写出以下proto文件。

 

[cpp] view plain copy
 
  1. message PlayerPos  
  2. {     
  3.     required  uint32  playerID = 1;   
  4.     required  float   posX = 2 ;      
  5.     required  float   posY = 3 ;  
  6. };  
  7.   
  8. file  vector.protomessage  vector3D  
  9. {  
  10.     required float x = 1;  
  11.     required float y = 2;  
  12.     required float z = 3;  
  13. };  

这样就有一个问题,现在的游戏都是3D游戏,因此需要xyz来表示位置,还需要另一组xyz来表示朝向,如果用简单变量的话就会显的很乱,而且无论是位置还是朝向其实都是一组xyz,因此可以将xyz抽出来成为一个复合数据类型,单独放在一个文件中。这样就构成以下文件。

 

 

[cpp] view plain copy
 
  1. file  Player.protoimport "vector.proto";  
  2. message PlayerPos   
  3. {  
  4.     required uint32 playerID = 1;  
  5.     required vector3D  pos = 2;  
  6. };  

编译的时候先编译vector文件,采用import时需要注意路径,本例中两文件在同一目录下。

 

 

[cpp] view plain copy
 
  1. protoc --cpp_out=.  vector.proto  Player.proto  

 

proto对应的文件已经生成了,但是该怎么赋值呢,查API查了半天有点不知所以,干脆来看生成的类文件的源代码吧

 

[cpp] view plain copy
 
  1. // required uint32 playerID = 1;    
  2. inline bool has_playerid() const;    
  3. inline void clear_playerid();    
  4. static const int kPlayerIDFieldNumber = 1;   
  5. inline ::google::protobuf::uint32 playerid() const;    
  6. inline void set_playerid(::google::protobuf::uint32 value);    
  7. // required .vector3D pos = 2;    
  8. inline bool has_pos() const;    
  9. inline void clear_pos();    
  10. static const int kPosFieldNumber = 2;    
  11. inline const ::vector3D& pos() const;    
  12. inline ::vector3D* mutable_pos();    
  13. inline ::vector3D* release_pos();    
  14. inline void set_allocated_pos(::vector3D* pos);  

 

上面列出了生成的部分源代码,主要是PlayerPos的操作变量的函数,第一个playID很简单,可以看到直接使用set_playerid ( ) 即可,但是对于嵌套的pos 发现没有对应的set_pos方法,不过发现了一个set_allocated_pos() 函数,这个函数也是set开头的,看看这个函数是干嘛的。

[cpp] view plain copy
 
  1. inline void PlayerPos::set_allocated_pos(::vector3D* pos)   
  2. {    
  3.     delete pos_;    
  4.     pos_ = pos;    
  5.     if (pos)   
  6.     {      
  7.         set_has_pos();    
  8.     }   
  9.     else {     
  10.          clear_has_pos();    
  11.     }  
  12. }  

看上去可以赋值,直接调用set_allocated_pos() 进行赋值看一看

[cpp] view plain copy
 
  1. PlayerPos player;  
  2. vector3D  tmp;  
  3. tmp.x = 1;  
  4. tmp.y = 2;  
  5. tmp.z = 3;  
  6. player.set_allocated_pos(&tmp)  

编译没问题,但是运行时出现错误,而且是很奇怪的错误,仔细了查看一下PlayerPos的源码,发现一个问题

 

[cpp] view plain copy
 
  1. ::vector3D* pos_;  ::google::protobuf::uint32 playerid_;  

上面是PlayerPos中变量的保存形式,发现pos是作为一个指针存储的,如果按照之前的赋值 tmp 是一个局部变量,函数返回时局部变量自动销毁,而pos_保存的仍然是已被销毁的tmp的位置,因此会出错,如果采用new的话就可以解决这个问题,即赋值方法如下:

 

 

[cpp] view plain copy
 
  1. PlayerPos player;vector3D  *tmp = new Vector3D;  
  2. tmp->x = 1;  
  3. tmp->y = 2;  
  4. tmp->z = 3;  
  5. player.set_allocated_pos(tmp)  

 

这样即可,编译运行都没有问题。 
如此之外,还有一种赋值方法,就是调用mutable_pos()

 

[cpp] view plain copy
 
  1. inline ::vector3D* PlayerPos::mutable_pos()   
  2. {    
  3.     set_has_pos();    
  4.     if (pos_ == NULL)   
  5.         pos_ = new ::vector3D;    
  6.     return pos_;  
  7. }  

mutable_pos () 中自己new出了一个vector3D 对象,而vector3D中又实现了赋值的重载,因此可以这样解决:

 

 

[cpp] view plain copy
 
    1. PlayerPos player;  
    2. vector3D  *tmp = player.mutable_pos();  
    3. tmp->x = 1;  
    4. tmp->y = 2;  
    5. tmp->z = 3;  
posted on   DoubleLi  阅读(1978)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2013-04-18 SQL触发器实例讲解
2012-04-18 js获取触发事件元素的坐标
2012-04-18 JavaScript event对象clientX,offsetX,screenX异同 带图 .
2012-04-18 js拖动原理
2012-04-18 js事件列表
2012-04-18 javascript 中ondragstart ondrag实现拖动界面元素效果 .
点击右上角即可分享
微信分享提示