data loss when sending across type NSData

I am currently in the middle of implementing the network layer of my game, i am making progress however i have come across something very odd which i was hoping someone could shed some might on:

Before sending my data across i am encoding it into type NSData (message.cards = [MovePlayer beginEncodeMyCards:myCards];) and then assigning it to a pointer (message.cards)

MessageMove message;
message.message.messageType = kMessageTypeMove;
/**/message.cards = [MovePlayer beginEncodeMyCards:myCards];/**/
NSData *data = [NSData dataWithBytes:&message length:sizeof(message)];

If i do all the decoding on the client side as follows :

MessageMove *myMessage  = (MessageMove *) [data bytes];
/**/myCards = [MovePlayer beginDecodeMyCards:myMessage->cards cardArray:myCards];/**/

everything works fine, i am able to decode myMessage->cards, however when i try doing the same after sending the object remotely i can see the correct message type (kMessageTypeMove) but not the cards data (myMessage->cards).

if anyone can please shed some light to this i would greatly appreciate it

many thanks

Answers


tl;dr, use NSCoding to serialise your objects.

You can't send pointers to a different process, because the pointers will all be wrong on the other side.

Lets say on the sending machine, you have this:

NSLog(@"cards: %p", message.cards);
//prints "cards: 0xDEADBEEF"

This says that the chunk of memory for the NSArray* object starts at the memory address 0xDEADBEEF. This is all correct at this point.

Now, you send that 0xDEADBEEF pointer to another computer. The computer gets the pointer, but the memory at 0xDEADBEEF does not have an NSArray in it on the other computer. There could be anything in that region of memory. You've sent a pointer, but you haven't actually sent any of the data with it.

Long story short, just use NSCoder for serialisation because it's very good.


Need Your Help

Form action with variable not valid after update to 1.2

angularjs

I was generating a form action in my previous version of AngularJS using this code:

Can I save the keep the absolute path of a file in database?

ios iphone

In my app I save some photos in sandbox and I keep its absolute path in database. Then I build and run again, I fetch the path and try to get the photo, it says the file doesn't exist.