简体   繁体   中英

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...

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

The 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

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. 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.

Use Serialization whenever you have to send data across the network. You may refer to Boost Serialization . Your DataPacket is something whose size is dynamic. 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 . 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.

You need to serialize the data yourself. You can't just send std::vector or pointer thereto.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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