First you should probably start using fixed-size integers, like uint32_t
(from <cstdint>
) for the sizes and message types.
Then the receiving application know how much to read to get the message type, and how much to read to get the size. Then simply read the specified number of bytes into the correct structure.
The important thing is that all structures in the union must start with a size member.