简体   繁体   English

C ++ UDP接收类中的向量

[英]C++ UDP receiving a vector within a class

My program uses UDP to send data between two programs, it works great however I have added a new vector into the data I want to send, the vector type is another class which looks like this... 我的程序使用UDP在两个程序之间发送数据,效果很好,但是我在要发送的数据中添加了一个新的向量,向量类型是另一个看起来像这样的类...

class Bullet: public Sprite
{
public:
    float speed;
};

The DataPacket... DataPacket ...

typedef struct DataPacket
{
    int ID;                             //Player ID
    int elapsedTime;                    //Total elapsed player time
    float x, y;                         //X & Y pos of player
    std::vector<Bullet>* pBullets;      //Vector containing all the players bullets
};

Is there a way to send this data correctly? 有没有办法正确发送此数据? The expression cannot be evaluated once the server receives the data from the client, every other part is correct in the received packet. 一旦服务器从客户端接收到数据,该表达式就无法求值,接收到的数据包中的其他所有部分都是正确的。

Basically the server is receiving the positional data of the bullets on the screen which is contained in this Bullet class along with a few other items. 基本上,服务器正在接收此项目符号类中包含的项目符号在屏幕上的位置数据以及其他一些项目。

Just to note: pBullet never used to be a pointer but in an attempt to try and figure out what was wrong I changed it to a pointer...it never fixed the issue tho 只是要注意:pBullet以前从来都不是指针,但是为了尝试找出问题所在,我将其更改为指针...它从未解决过该问题,

You cannot do this. 你不可以做这个。 A vector is a class which is typically implemented with an internal pointer to dynamic memory which changes location as the vector grows. 向量是一个类,通常使用指向动态内存的内部指针来实现,该指针随向量的增长而改变位置。

When you try and serialize the vector by casting your whole structure, you just serialize the pointer to the memory holding the vector contents, you don't get the contents itself because it's not part of that structure. 当尝试通过强制转换整个结构来对向量进行序列化时,只需对指向包含向量内容的内存的指针进行序列化,就不会得到内容本身,因为它不是该结构的一部分。

You'll have to individually serialize all items in the vector one-by-one and individually add them back in when you deserialize it. 您必须分别对向量中的所有项目进行逐一序列化,并在反序列化时将其单独添加回去。

Note that you can change the vector of bullets to a statically sized array internal to the structure and then it would be contiguous in memory and you could just serialize the whole structure - and you can also make the last element of the array an array of one bullet, and then allocate memory for the structure size + (x-1)*sizeof(Bullet), allowing you to overwrite the array of 1 for all of the extra memory you added. 请注意,您可以将项目符号的矢量更改为结构内部的静态大小的数组,然后它将在内存中是连续的,并且可以序列化整个结构-您还可以使数组的最后一个元素为一个数组项目符号,然后为结构大小+(x-1)* sizeof(Bullet)分配内存,使您可以为添加的所有额外内存覆盖1的数组。 This would also be contiguous in memory allowing you to serialize the whole memory region pretty easily. 这在内存中也将是连续的,从而使您可以轻松地序列化整个内存区域。

You should also look in to htonl, ntohl, htons, ntohs and start network-byte-ordering your data as well if you're going to send it over a network to keep the byte endianness from being an issue on some other systems you might end up using. 如果还要通过网络发送数据,则还应该查看htonl,ntohl,htons,ntohs并开始对网络数据进行字节排序,以防止字节序在其他系统上成为问题最终使用。

Use Serialization whenever you have to send data across the network. 每当需要通过网络发送数据时,请使用序列化。 You may refer to Boost Serialization . 您可以参考Boost序列化 Your DataPacket is something whose size is dynamic. 您的DataPacket的大小是动态的。 Had it been an array than vector, it might have worked given the same endianness of the machines. 如果它不是向量的数组,那么考虑到机器的相同字节序,它可能会起作用。

For Game Development I would recommend to use Google ProtoBuf . 对于游戏开发,我建议使用Google ProtoBuf In comparison with Boost Serialization, it provides binary serialization (Boost Serialization has an example of binary serialization but it's not very portable) and is more convenient for complex data structures. 与Boost序列化相比,它提供了二进制序列化(Boost序列化提供了一个二进制序列化的示例,但它不是非常可移植的),并且对于复杂的数据结构更方便。

You need to serialize the data yourself. 您需要自己序列化数据。 You can't just send std::vector or pointer thereto. 您不能只向其发送std::vector或指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM