Serialization in C++

Nicko

Expert Member
Joined
Feb 16, 2010
Messages
1,048
Reaction score
1
Location
Cape Town
Hi guys,

I'm relatively new to C++ and have never worked with serialization so I am very much out of my programming comfort zone here, and been at this for a day with minimal progress.

So here is what I am trying to work towards. I want to send various structs containing data between the client and server programs. The connection is using TCP, and I am using the boost package and my code is completely based (for now) on the serialization example: here.

The issue is that I need to have multiple different types of messages (structs) not just one which they have in the example, so I am trying to find a way to identify the message so I know which struct to use when deserializing it. My original plan was to have the first variable be a unique id for the struct, so it could identify the struct and deserialize accordingly. But unfortunately, it looks like boost just deserializes the whole struct in one go.

I searched for a solution online to no avail, which must mean the solution is so simple that no-one has found the need to ask. The most annoying part of this is that my structs are so simple; consisting of a few char arrays and ints. Any suggestions will be appreciated.
 
This might be simplistic and stupid since I've never worked with boost, but have you tried simply casting the pointer to the fetched data to the identified type? If it reassembles the original data correctly as serialization should do, it might just work. Caveat, no idea what happens with pointers to assigned memory inside the struct with serialization.

struct newstruct *struct_type1 = (*struct_type) origstruct;
 
construct specific tcp packets with packet headers.

http://www.ece.ubc.ca/~teerawat/publications/NS2/15-Packets.pdf

I was hoping for something simpler but will look into this.

This might be simplistic and stupid since I've never worked with boost, but have you tried simply casting the pointer to the fetched data to the identified type? If it reassembles the original data correctly as serialization should do, it might just work. Caveat, no idea what happens with pointers to assigned memory inside the struct with serialization.

struct newstruct *struct_type1 = (*struct_type) origstruct;

The issue is that I need to know the struct to fetch the data in the first place.
 
Top
Sign up to the MyBroadband newsletter
X