Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Serialization


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Fezziiwig   Members   -  Reputation: 140

Like
0Likes
Like

Posted 20 October 2012 - 04:14 AM

Can anyone recommend a fast method to serialize a class or struct so it can be send over a socket, and a short example would be nice.

Sponsor:

#2 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 20 October 2012 - 11:55 AM

You can send struct or class directly, assuming they don't have any pointers inside, packing is right, byte order is right and they don't have any virtual methods.
To fix any of those problems, copy data you want to send into single buffer, ex. array, and send it.

#3 hplus0603   Moderators   -  Reputation: 5509

Like
0Likes
Like

Posted 20 October 2012 - 12:21 PM

Do you know how to serialize (save) that struct or class to a file?
If so, treat a block of memory as a "file" and "save" it to there. Then send that block as a message over the network.
enum Bool { True, False, FileNotFound };

#4 Fezziiwig   Members   -  Reputation: 140

Like
0Likes
Like

Posted 21 October 2012 - 05:49 AM

Ok thanks for the help. Because I'm sending quite large data structures, I decided to use the boost libraries (good choice?). I can send the structs like so;
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << myStruct;
sendto(Socket, archive_stream.str().c_str(), sizeof(archive_stream.str().c_str()), 0, (SOCKADDR *) & SockAddr, sizeof(SockAddr));
but how to I receive them on the other end?

Edited by Fezziwig, 21 October 2012 - 05:50 AM.


#5 flodihn   Members   -  Reputation: 253

Like
0Likes
Like

Posted 22 October 2012 - 04:42 PM

I would recommend that you take a look at messagepack, it is used to serialize/deserialize data (for sending over network or storing to file).
http://msgpack.org/

Edited by flodihn, 22 October 2012 - 04:47 PM.


#6 Khatharr   Crossbones+   -  Reputation: 3030

Like
0Likes
Like

Posted 24 October 2012 - 10:09 PM

If you're sending very large data you may want to consider using some light zlib compression as well (if your network performance is suffering), but that's something to do later, once you've got this working.

Remember that 'serialize' is just a fancy word for 'put in a specific order'. If you hold the relevant data in your classes as a member struct then you should have an easier time serializing, since you can just grab that struct from the object.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#7 rnlf   Members   -  Reputation: 1167

Like
0Likes
Like

Posted 25 October 2012 - 02:13 AM

If the objects you want to send are pure data, you may want to also take look at Google's Protocol Buffers.

my blog (German)


#8 wood_brian   Banned   -  Reputation: 197

Like
0Likes
Like

Posted 28 October 2012 - 07:59 PM

The C++ Middleware Writer serializes objects so they can be sent across a network. There are links to examples on the home page.

Edited by wood_brian, 28 October 2012 - 08:01 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS