Creation and use of cross-platform structure in C ++

I am writing a cross-platform game with network capabilities (using SFML and RakNet ), and I went to the point where I compiled the server on my Ubuntu server and got the client running on my Mac. All development is done on my Mac, so I tested the server first and it worked perfectly.

I send structover the network and then just drop them back from char *to (for example) inet::PlayerAdded. Now it works fine (for the most part), but my question is: will it always work? This seems like a very fragile approach. Will the structure always be laid out the same way, even on other platforms, such as Windows? What would you recommend?

#pragma pack(push, 1)
struct Player
{
    int dir[2];
    int left;
    float depth;
    float elevation;
    float velocity[2];
    char character[50];
    char username[50];
};

// I have been added to the game and my ID is back
struct PlayerAdded: Packet
{
    id_type id;
    Player player;
};
#pragma pack(pop)
+5
3

, , . - Boost serial Google Protobuf .

, , - , . "" . , , . "pack" - .

. stdint.h , uint32_t. , , 32- , , , . , endianess , , .

+5

, ( ) , int .

, . , 64- , 32-?

, Boost.Serialization Google, , ( ), .

, , , ZLIB, protobuf. , . , .

+9

"... ...", , . , , / .

( ) ; " ABI", , , 32-/64- Windows, 32-/64- Linux, MacOSX.

'# pragma pack' , . , "" 64- Windows 32-, 64- - Linux MacOSX.

, , , - (RPC) / "", . "XDR" ( eXternal Data). . RFC1832. , ; XML, XDR, google:: protobuf, boost Qt:: Variant, .

On the side of pure implementation: for simplicity, just assume that "unsigned int" is everywhere 32 bits aligned on a 32-bit boundary; if you can encode all your data in an array of 32-bit values, then the only externalization problem that you have to deal with is the statement.

+2
source

All Articles