構造:
typedef struct
{
NSData *levelData;
int levelID;
} GameLevelInfo;
データの送信方法:
static GameLevelInfo gameLevelInfo;
gameLevelInfo.levelData = [[LevelManager sharedManager] levelDataFromName:@"XXXXXX"]];
gameLevelInfo.levelID = 1;
[sendPacket:NETWORK_SENDING_LEVEL
objectIndex:0
data:&gameLevelInfo
length:sizeof(gameLevelInfo)
reliable:YES];
「levelDataFromName」:
-(NSData *) levelDataFromName:(NSString *)levelName
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentPath stringByAppendingFormat:@"/%@", levelName];
return [[NSFileManager defaultManager] contentsAtPath:filePath];
}
そして「sendPacket」:
- (void)sendPacket:(int)packetID objectIndex:(int)objectIndex data:(void *)data length:(int)length reliable:(bool)howtosend {
// the packet we'll send is resued
static unsigned char networkPacket[MAX_PACKET_SIZE];
const unsigned int packetHeaderSize = 3 * sizeof(int); // we have two "ints" for our header
if(length < (MAX_PACKET_SIZE - packetHeaderSize)) { // our networkPacket buffer size minus the size of the header info
int *pIntData = (int *)&networkPacket[0];
// header info
pIntData[0] = packetNumber++;
pIntData[1] = packetID;
pIntData[2] = objectIndex;
// copy data in after the header
memcpy( &networkPacket[packetHeaderSize], data, length );
NSData *packet = [NSData dataWithBytes: networkPacket length: (length+packetHeaderSize)];
if(howtosend == YES) {
[session sendDataToAllPeers:packet withDataMode:GKSendDataReliable error:nil];
} else {
[session sendDataToAllPeers:packet withDataMode:GKSendDataUnreliable error:nil];
}
}
}
データは正常に送信されましたが、「受信側」:
GameLevelInfo *gameLevelInfo = (GameLevelInfo*)data;
NSData *content = gameLevelInfo->levelData;
「コンテンツ」は無効なオブジェクトでした。
どんな助けでも大歓迎です、ありがとう:)