I've got an enumeration in my game. A simple string message with an appended PacketType is being sent with the message (so it knows what to do with the message) over GameKit WIFI connection. I used Apple's GKRocket sample code as a starting point.
The code itself is working fantastically; I just want to understand what the line with CFSwapInt32HostToBig is doing. What on earth does that do? and why does it need to do it? My guess is that it's making sure the PacketType value can be converted to an unsigned integer so it can send it reliably, but that doesn't sound all that correct to me.
The documentation states "Converts a 32-bit integer from big-endian format to the host’s native byte order." but I don't understand what the means really.
typedef enum {
PacketTypeStart, // packet to notify games to start
PacketTypeRequestSetup, // server wants client info
PacketTypeSetup, // send client info to server
PacketTypeSetupComplete, // round trip made for completion
PacketTypeTurn, // packet to notify game that a turn is up
PacketTypeRoll, // packet to send roll to players
PacketTypeEnd // packet to end game
} PacketType;
// ....
- (void)sendPacket:(NSData *)data ofType:(PacketType)type
{
NSLog(@"sendPacket:ofType(%d)", type);
// create the data with enough space for a uint
NSMutableData *newPacket = [NSMutableData dataWithCapacity:([data length]+sizeof(uint32_t))];
// Data is prefixed with the PacketType so the peer knows what to do with it.
uint32_t swappedType = CFSwapInt32HostToBig((uint32_t)type);
// add uint to data
[newPacket appendBytes:&swappedType length:sizeof(uint32_t)];
// add the rest of the data
[newPacket appendData:data];
// Send data checking for success or failure
NSError *error;
BOOL didSend = [_gkSession sendDataToAllPeers:newPacket withDataMode:GKSendDataReliable error:&error];
if (!didSend)
{
NSLog(@"error in sendDataToPeers: %@", [error localizedDescription]);
}
}