在C ++中是否有表示数据包的通用模式?(Is there a common pattern to represent a packet in C++?)

编程入门 行业动态 更新时间:2024-10-09 05:13:50
在C ++中是否有表示数据包的通用模式?(Is there a common pattern to represent a packet in C++?)

我正在用C ++开发一个多人游戏,我想在服务器和客户端之间交换很多不同的数据包类型。 那些东西有某种标准吗? 例如,如果我有一个玩家位置数据包,其中包含玩家的客户端ID和世界3D空间中的位置。 哪种方法更常见/可接受/标准?

struct PacketPlayerPosition { char header; int clientID; float positionX; float positionY; float positionZ; }

或者这样:

class PacketPlayerPosition { public: PacketPlayerPosition(int id, float x, float y, float z); PacketPlayerPosition(char* rawData); ~PacketPlayerPosition(); char* SerializeData(); private: char header; int m_id; float m_posX; float m_posY; float m_posZ; }

我看到它的方式,以第一种方式创建它让我们可以轻松地在TCP流UDP数据包中传输数据包,只需将它来回转换为char数组,但另一方面,它很难管理并传递给它。更大的服务器架构 第二种方法甚至可以允许创建层次结构并使用多态来传递它并在更大的服务器图片中管理它,但是,每个数据包必须在类中实现序列化/反序列化功能,这看起来很脏。

有人可以给出一个开源项目的“真实世界”示例,或者只是解释为什么一种方式更喜欢另一种方式?

I'm developing a multiplayer game in C++ and I have a lot of different packet types I wish to exchange between the server and the clients. Is there some-kind of standard for those things? For example if I have a player position packet which contains the player's client id and position in the world's 3D space. Which method is more common/acceptable/standard?

struct PacketPlayerPosition { char header; int clientID; float positionX; float positionY; float positionZ; }

Or this way:

class PacketPlayerPosition { public: PacketPlayerPosition(int id, float x, float y, float z); PacketPlayerPosition(char* rawData); ~PacketPlayerPosition(); char* SerializeData(); private: char header; int m_id; float m_posX; float m_posY; float m_posZ; }

The way I see it, create it in the first way let transmit the packet easily in the TCP stream UDP packet by just casting it back and forth to char array, but on the other end, it's difficult to manage and pass it around in the bigger server architecture. The second way may even allow to create an hierarchy and use polymorphism to pass it around and manage it in the bigger server picture, but, each packet have to implement serialize/deserialize functions in the class, which seems dirty.

Can someone give a "real world" example of an opensource project or just explain why one way preferred on the other?

最满意答案

我想这可能取决于您将需要的不同数据包类型的数量。

如果您只需要一个小数字但需要层次结构,那么使用C ++对象。 在序列化功能的情况下,您将复制代码,但您将获得您提到的优势。 这使代码更易于管理。

这种方法需要大量的序列化函数,因此每次添加新类型的数据包时都会导致重复的代码,但如果该数据包从另一个数据包中下降,则会导致重复的代码。

如果要发送许多不同的数据包,请使用即时可序列化的c结构。 此方法避免重复序列化代码但不允许多态,因此每次添加另一个可能是另一个数据包的后代的数据包时,您将过度复制代码。

所以,对象:如果大多数数据包可以主要从另一个继承,则代码较少。

结构:如果大多数数据包无法继承,则代码较少。

因此问题是; 有多少数据包可以从其他数据包继承?

I'd say this is likely to depend on the number of different packet types you are going to need.

If you only need a small number but want hierarchy then use a C++ object. You will duplicate code in the case of the serialize function but you will gain the advantages you mentioned. This makes the code more manageable.

This method requires lots of serialize functions and so will lead to duplicated code each time you add a new type of packet but not so much if that packet descends from another.

If you have lots of different packets to send then use the instantly-serializable c struct. This method avoids duplicating serialize code but will not allow polymorphism and so each time you add another packet that could be a descendant of another packet, you will excessively duplicate code.

So, Objects: less code if most packets can inherit mostly from another.

Structs: less code if most packets cannot inherit.

The question therefore is; how many of your packets can inherit from other packets?

更多推荐

本文发布于:2023-08-07 13:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据包   模式   common   packet   represent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!