I have many different structures containing enumeration members that I must pass over TCP / IP. While the endpoints are on different operating systems (Windows XP and Linux), which means different compilers (gcc 4.xx and MSVC 2008), both parts of the program use the same header files with type declarations.
For performance reasons, structures should be passed directly (see the code example below) without costly serialization or streaming of elements inside.
So the question is how to ensure that both compilers use the same internal memory representation for enumeration elements (i.e. both use 32-bit unsigned integers). Or if there is a better way to solve this problem ...
typedef enum
{
A = 1,
B = 2,
C = 3
} eParameter;
typedef enum
{
READY = 400,
RUNNING = 401,
BLOCKED = 402
FINISHED = 403
} eState;
#pragma pack(push,1)
typedef struct
{
eParameter mParameter;
eState mState;
int32_t miSomeValue;
uint8_t miAnotherValue;
...
} tStateMessage;
#pragma pack(pop)
tStateMessage msg;
send(iSocketFD,(void*)(&msg),sizeof(tStateMessage));
tStateMessage msg_received;
recv(iSocketFD,(void*)(&msg_received),sizeof(tStateMessage));
Additionally...
- Since both endpoints are small endmasks, endianess is not a problem here.
- And the #pragma package satisfactorily solves alignment problems.
thanks for your answers, Axel
source
share