I'm trying to send a message with game center that contains a struct, roughly following Ray Wenderlich's guide. In general sending and receiving messages has been no problem, but once I throw anything with a pointer in I get a bit confused. The goal is to send a few strings and a BOOL over as one message. Relevant code follows:
//myfile.h
typedef enum {
kMessageTypeRandomNumber = 0,
kMessageTypeInterstitial
} MessageType;
typedef struct {
MessageType messageType;
} Message;
typedef struct {
Message message;
char *playerName;
char *scores;
BOOL correct;
char *stat;
} MessageInterstitial;
//myfile.m
- (void)sendData:(NSData *)data {
NSError *error;
BOOL success = [[GCHelper sharedInstance].match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
if (!success) {
NSLog(#"Error sending init packet:\n%#",error);
}
}
-(void)send{
MessageInterstitial message;
message.message.messageType = kMessageTypeInterstitial;
message.playerName="test";
NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageInterstitial)];
[self sendData:data];
MessageInterstitial * myMessage = (MessageInterstitial *) [data bytes];
printf("player: %s\n",myMessage->playerName); // prints 'player: test' as expected
}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
Message *message = (Message *) [data bytes];
if(message->messageType == kMessageTypeInterstitial)
{
MessageInterstitial * myMessage = (MessageInterstitial *) [data bytes];
printf("player: %s\n",myMessage->playerName); //prints 'player: ' not expected
}
}
It appears that the data is being saved properly, since I'm able to print it back out right away without issue, but my thinking is that the data is pointing to an address in memory, which would make it so it prints out the correct thing on the sending device, but not the receiving device. That being said, I've got no clue how to fix this, or how i should be sending strings within a struct if that is in fact the case.
Indeed this seems like the problem, the addresses are addresses in the sending device, not receiving device. You can use char arrays instead, and just allocate enough space from the beginning. This way they will be part of the struct:
// those are just examples of course...
typedef struct {
Message message;
char playerName[30];
char scores[30];
BOOL correct;
char stat[15];
} MessageInterstitial;
You have to use a char array, but one thing is that when you create your struct, this char array is randomly filled with all sorts of weird characters, so here's how got rid of those characters :
typedef struct {
char stringToSend[20];
int stringToSendLength;
} myStruct;
// create a Struct and an NSString
myStruct aStruct;
NSString *stringToConvert = #"stringToConvert";
// convert NSString into char array
for (int i = 0; i < stringToConvert.length; i++) {
aStruct.stringToSend[i] = [stringToConvert characterAtIndex:i];
}
aStruct.stringToSendLength = stringToConvert.length; // send string length
// store struct into NSData object
NSData *data = [NSData dataWithBytes:&aStruct length:sizeof(myStruct)];
// retrieve data
myStruct *anotherStruct = (myStruct *)[data bytes];
// convert char array into NSString and only keep part required (without some weird random characters)
NSString *receivedString = [[NSString alloc] initWithBytes:anotherStruct->stringToSend length:anotherStruct->stringToSendLength encoding:NSASCIIStringEncoding];
A little old, but I'll throw in my 2 cents.
When I changed from char * to char string[30] i had to increase the length when I initialised my NSData. In addition, direct assignment of a char array is not possible. You either have to iterate the string and insert the characters one by one, or use strcpy like I did below.
This is how it finally looked like for me:
- (void)sendRandomWord:(NSString*)randomWord{
MessageRandomWord message;
message.message.messageType = kMessageTypeRandomWord;
strcpy( message.randomWord, randomWord.UTF8String );
NSData *data = [NSData dataWithBytes:&message length:sizeof(kMessageTypeRandomWord) + strlen(message.randomWord)*sizeof(char)];
[self sendData:data];
}
Related
I want to send a struct to another player in GameCenter.
I have read the other questions about this, however, I cannot get any of them to work.
I need to get #"1234" into a char[4] (ex char[0] = '1', char[1] = '2', etc)
I have tried [NSString UTF8String], but it doesn't seem to do what I want.
It assigns fine, but when I pull it back into NSString * with [NSString stringWithUTF8String:], It returns blank.
If someone could show me the conversion to and from, it would be greatly appreciated.
Thanks.
EDIT:
I can't get it to work :/ Here is my code (the abridged version):
Matchmaker.h
enum { NChars = 4 };
typedef struct {
MessageType messageType;
} Message;
typedef struct {
Message message;
char code[NChars];
} MessageGameCode;
#interface Matchmaker : CCLayer <GameCenterMasterDelegate>{
NSString *_code;
}
#property (nonatomic,retain) NSString *_code;
Matchmaker.m
#synthesize _code;
-(void)viewDidLoad{
self._code = #"1234";
}
- (void)sendCode {
NSLog(#"Sending Code....");
MessageGameCode message;
message.message.messageType = kMessageTypeGameCode;
NSString * const source = self._code;
const char* const sourceAsUTF8 = source.UTF8String;
for (size_t idx = 0; idx < NChars; ++idx) {
message.code[idx] = sourceAsUTF8[idx];
}
NSData *data = [NSData dataWithBytes:&message length:NChars];
[self sendData:data];
}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
Message *message = (Message *) [data bytes];
if (message->messageType == kMessageTypeGameCode) {
MessageGameCode *codeMessage = (MessageGameCode *)[data bytes];
self._code = [[NSString alloc] initWithBytes:codeMessage->code length:NChars encoding:NSUTF8StringEncoding];
[self setGameState:kGameStateWaitingForStart];
NSLog(#"Game Code Recieved");
NSLog(#"Recieved Code: %#",self._code); //This always shows self._code as blank
}
}
Your attempt will fail because the cstring which you pass to + [NSString stringWithUTF8String:] is not terminated.
Try this:
NSString * result = [[NSString alloc] initWithBytes:bytes
length:4
encoding:NSUTF8StringEncoding];
Edit
A more complete demonstration:
enum { NChars = 4 };
/* requires error checking */
void transmit() {
NSString * const source = #"1234";
char tmp[NChars] = { 0 };
const char* const sourceAsUTF8 = source.UTF8String;
for (size_t idx = 0; idx < NChars; ++idx) {
tmp[idx] = sourceAsUTF8[idx];
}
/* .. */
}
/* requires error checking */
void receive(const char bytes[NChars]) {
NSString * result = [[NSString alloc] initWithBytes:bytes length:NChars encoding:NSUTF8StringEncoding];
/* ... */
}
One way is
char bytes[4];
NSData* data = [#"1234" dataUsingEncoding: NSUTF8StringEncoding];
if ([data length] <= 4)
{
memcpy(bytes, [data bytes], [data length]);
}
And to go the other way:
NSString* recodedString = [[NSString alloc] initWithBytes: bytes
length: savedLengthFromBefore
encoding: NSUTF8StringEncoding];
There are a couple of possible pitfalls here.
One is that with [NSString UTF8String] "The returned C string is automatically freed just as a returned object would be released; you should copy the C string if it needs to store it outside of the autorelease context in which the C string is created.". So, depending on how long you're expecting the value to stick around you may need to copy it (for example, using strcpy)
The other issue is that [NSString UTF8String] and [NSString stringWithUTF8String:] both expect NULL-terminated C strings, so you need a char[5], not a char[4] to hold #"1234".
I'm trying to save all the variables in my class into NSUserDefaults using objc/runtime. And below is the code I'm using.
NSUInteger count;
Ivar *iVars = class_copyIvarList([self class], &count);
for (NSUInteger i=0; i<count; i++)
{
Ivar var = iVars[i];
NSString *varName = [NSString stringWithCString:ivar_getName(var) encoding:NSUTF8StringEncoding];
NSString *varType = [NSString stringWithCString:ivar_getTypeEncoding(var) encoding:NSUTF8StringEncoding];
if([varType hasPrefix:#"["])
{
NSLog(#"Array");
id var1 = [_manager valueForKey:varName];
NSLog(#"--- %#", var1);
NSData *data = [NSData dataWithBytes:&([_manager valueForKey:varName]) length:sizeof([_manager valueForKey:varName])]
[[NSUserDefaults standardUserDefaults] setObject:[_manager valueForKey:varName] forKey:varName];
}
else
{
NSLog(#"NonArray");
NSLog(#"--- %#", [_manager valueForKey:varName]);
[[NSUserDefaults standardUserDefaults] setObject:[_manager valueForKey:varName] forKey:varName];
}
}
free(iVars);
The problem is that, when there are only primitive datatypes, the above code works just fine. But, when I try to access a array variable like int[], or float[], it gets crashed with SIGABRT. it is not showing any other messages.
valueForKey doesn't return any values for C arrays.
If anybody know how to load values for C-arrays in runtime, please help.
Thanks in advance,
Suran
Unless you always provide a paired length method, your program will never know the length of the array returned. So... you will need to do some work someplace to accomplish this without a crash.
If I really wanted to do what you're doing, I would make the class itself create the array, providing NSData. If this is common, you may want to use a convention:
- (int*)pixelBuffer;
- (NSData *)pixelBufferForSerialization; // << returns a deep copy of
// self.pixelBuffer as an
// NSData instance.
So your above implementation would see that the property defines a scalar array, and then request NSData * data = obj.pixelBufferForSerialization; instead of trying to produce the data itself.
Update
It's best to let the class do it. Here's how to create NSData using such an array:
#interface DataManager : NSObject
{
#private
int* things;
size_t nThings;
}
- (int*)things;
- (NSData *)thingsAsNSData;
#end
#implementation DataManager
- (int*)things
{
return things;
}
- (NSData *)thingsAsNSData
{
// note: you may need to choose an endianness for serialization
if (0 == nThings) return [NSData data];w
return [NSData dataWithBytes:things length:nThings * sizeof(things[0])];
}
#end
Again - you want the class to create the data because it knows its own structure best.
can any one provide me a good way to send a NSArray object over wifi network.
I have a code to sent a text message(string) over wifi network.
my code is
Send
- (void) sendText:(NSString *)string {
const uint8_t *message = (const uint8_t *)[string UTF8String];
if (_outStream && [_outStream hasSpaceAvailable])
if([_outStream write:message maxLength:strlen((char *)message)] == -1)
NSLog(#"Failed sending data to peer");
}
Receiver Side
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if (stream == _inStream) {
// read it in
unsigned int len = 0;
len = [_inStream read:buf maxLength:buffSize];
buf[len] = '\0';
if(!len) {
if ([stream streamStatus] != NSStreamStatusAtEnd)
NSLog(#"Failed reading data from peer");
} else {
NSString *message = [NSString stringWithUTF8String:(char *)buf]; }}
now the message contains my received message...
However i need to send a NSArray object over wifi.can any one help me to do it..
You can use the NSKeyedArchiver to get a NSData object out of your NSArray, the NSData object is just a wrapper for binary data so you can send it over your socket. When you received the data, first transform it back into an NSData object and then use the NSKeyedUnarchiver class to get the array back.
You need mechanism (protocol) that will send elements of array from sender end and in the same way it will receive at the reception end
Its y first time trying to use NSData and Gamekit. So was wondering am i packing the data properly?
- (void)sendNetworkPacket:(GKSession *)session packetID:(int)packetID
reliable:(BOOL)howtosend
{
// the packet we'll send is resued
static unsigned char networkPacket[kMaxTankPacketSize];
const unsigned int packetHeaderSize = 2 * sizeof(int); // we have two "ints" for our
header
int *pIntData = (int *)&networkPacket[0];
// header info
pIntData[0] = gamePacketNumber++;
pIntData[1] = packetID;
int theLength = 2 * sizeof(int);
for (int i=0; i<([theHands.player1Hand count]); i++)
{
pIntData[2+i] = [[theHands.player1Hand objectAtIndex:i] intValue];
theLenght += sizeof(int);
}
NSData *packet = [NSData dataWithBytes: networkPacket length: theLength];
[session sendData:packet toPeers:[NSArray arrayWithObject:gamePeerId]
withDataMode:GKSendDataReliable error:nil];
}
Will the data I put into NSData *packet be valid?
Many Thanks,
-Code
You create the NSData correctly, and it will contain what you expect. But this is rather more complicated than necessary. The following will do too:
enum { kHeaderLength = 2 };
NSMutableData *packet = [NSMutableData dataWithLength: (kHeaderLength + [theHands.player1Hand count]) * sizeof( int )];
int *pIntData = (int *)[packet mutableBytes];
*pIntData++ = gamePacketNumber++;
*pIntData++ = packetID;
for (id thing in theHands.player1Hand) {
*pIntData++ = [thing intValue];
}
[session sendData: packet toPeers: [NSArray arrayWithObject: gamePeerId] withDataMode: GKSendDataReliable error: NULL];
This will have some advantages:
The packet data will not be copied to the NSData object, it will be created directly in there.
You don’t have to depend on a maximum packet size. You used a constant, which is good since you could change that in one place if bigger packets are required some time. But this is not really needed.
Your version is not thread safe since it depends on the static buffer. This might be fine, since not every method has to be thread-safe. But this is something one has to look out for.
Using fast enumeration also helps keeping the overhead down and is more readable.
Ok Here is the problem. I am trying to send through a structure or anything through the apple bluetooth send network packet code. The code to send is below
-(void)sendMessagePacket:(GKSession *)session packetID:(int)packetID withData:(void *)data ofLength:(int)length reliable:(BOOL)howtosend {
static unsigned char networkPacket[maxPacketSize];
const unsigned int packetHeaderSize = 2 * sizeof(int); // we have two "ints" for our header
if(length < (maxPacketSize - packetHeaderSize)) { // our networkPacket buffer size minus the size of the header info
int *pIntData = (int *)&networkPacket[0];
// header info
pIntData[0] = 10;
pIntData[1] = packetID;
// copy data in after the header
memcpy( &networkPacket[packetHeaderSize], data, length );
NSData *packet = [NSData dataWithBytes: networkPacket length: (length+8)];
if(howtosend == YES) {
[session sendDataToAllPeers:packet withDataMode:GKSendDataReliable error:nil];
} else {
[session sendDataToAllPeers:packet withDataMode:GKSendDataUnreliable error:nil];
}
}
}
If anyone could go through this with a comb and explain what is going on that would be greatly appreciated. Next, the receive code.
- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context {
// Caller whenever data is received from the session
unsigned char *incomingPacket = (unsigned char *)[data bytes];
//EXPECTS THAT WHAT IS IN INCOMING PACKET [0] TO BE OF DATA TYPE INTEGER
int *pIntData = (int *)&incomingPacket[0];
int packetTime = pIntData[0];
int packetID = pIntData[1];
static int lastPacketTime = -1;
switch (packetID) {
case NETWORK_COINTOSS:
//DO SOMETHING
break;
case NETWORK_TEXT_EVENT:
NSString *fewMore = (NSString *)&incomingPacket[8];
fewThings *bitMore = &fewMore[peer];
//NSLog(#"Trace Bit More: %#",fewMore);
break;
default:
break;
}
NSInteger lengthBytes = [data length];
NSLog(#"Length of data : %i", lengthBytes);
}
What i am struggling to understnad being a newcome to objective c is how all this works, how do i access the string that i have send as all efforts to log it cause the program to crash.
Below is the code used to start the send:
NSString *bigWord = #"BigWordBigWord";
NSInteger len = [bigWord length];
[self sendMessagePacket:gameSession packetID:NETWORK_TEXT_EVENT withData:bigWord ofLength:(len) reliable:YES];
Any help would be so appreciated. Thank You.
You really need to change the NSString object into either a UTF8 representation to send, or to archive it as an NSData object you send with the full length - you can't just cast an NSString as a C string (either for sending or receiving!!!).
You can get a C string version of NSString using:
const char *sendString = [bigWord cStringUsingEncoding:NSUTF8StringEncoding]
And then get it out of your incoming NSData object with
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
This guy is sending and receiving the string over network just fine. But, I'm also still getting error on the receiving end.
http://discussions.apple.com/thread.jspa?messageID=10215848
At last got it. GKTank is transferring the data using struct, so I placed my string inside the struct and rest is the history.
typedef struct {
NSString *strInfo;
} myInfo;