Friday, April 8, 2011

Packed and Serialized!

I just completed Assignment 2- Serialization and am very happy. It feels great to finish something so early and not have to worry about it hanging over your head. My goal this quarter is to really stay on top of my work, and this is a step in the right direction.


Overall, this assignment took me just under three hours to complete. At first, I wasn't quite sure about serialization in practice, because I didn't understand if we had to manually create "headers" and block the data off in the buffer, or if that was the larger picture used to describe it. In the first examples, with Dog and Cat, serialization was very straightforward and simple. Even though my tests for those passed, I thought that I was misunderstanding something because it seemed so easy, so I debugged and stepped through it, making sure that everything made sense. Then, I began working on Bird, and this is where things got ugly. A little.


I had to go back and look at the slides to make sure that we were indeed supposed to section off the data. Once I understood that we were allowed to, and in fact supposed to do that, I used placement new to create a temporary pointer to the buffer, and then incremented this pointer as I memcpy'd everything I needed in a step by step way. In Bird, the thing that gave me the most trouble was the char * because I knew how to serialize it, but when it came to deserializing it, I could not get a hold of its length. Then, I remembered the "header" suggestion, and in serializing, placed the strlen of the char * into the buffer at the very beginning. At that point, I could easily pull it out in my deserialize the same way and use it. After I had coded all of this, the test was still failing on the strncmp. I debugged and saw the string in my data field had extra junk on its end, so I went back and memset it to 0x00 in my constructors. This solved the problem and the test passed. I also added print statements to my constructors and destructors to print out the pointers, in order to check that everything was working correctly. 


Finally, in Fish, I realized that I could serialize the apple and orange structs as wholes, rather than going inside of them and individually blocking out all of their data. The serialize worked off the bat, but the deserialize caused a crash. It turned out that I had forgotten to actually initialize my apple and orange, was trying to write to them when they were null. Similarly to the previous situation in Bird, I went back to the constructor and initialized them which solved the problem. 


I like this class because it's teaching me something I've never even broached in other classes. I've done data alignment before but networking is literally a new world for me-- like a programming expansion pack. I'm really excited about learning more and applying to making an actual networked game, but I'm also glad to be done with this assignment. I'd say that having 4 teeth drilled and then completing this assignment makes for a good day's work. Tomorrow: Independent Study and Engine 2.

No comments:

Post a Comment