Can anyone help me? I intensively exchange data between two devices over TCP protocol by using GCDAsyncSocket. I send data like this:
NSMutableDictionary *packet = [[[NSMutableDictionary alloc] init] autorelease];
[packet setObject:[NSNumber numberWithInt:MultiPlayerTypeInfoNextRoundConfirm] forKey:@"type_info"];
[packet setObject:[NSNumber numberWithBool:YES] forKey:@"connection_confirmation"];
NSMutableData *data = [[NSMutableData alloc] initWithData:[NSKeyedArchiver archivedDataWithRootObject:packet]]; //[NSKeyedArchiver archivedDataWithRootObject:packet];
if (currentGameMode == GameModeServer)
[(ServerMultiplayerManager *)multiplayerManager sendNetworkPacket:data withTag:MultiPlayerTypeInfoNextRoundConfirm];
- (void)sendNetworkPacket:(NSData *)data withTag:(long)tag
{
[asyncSocket writeData:data withTimeout:-1 tag:tag];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSLog(@"DID WRITE DATA tag is %ld", tag);
[sock readDataWithTimeout:-1 tag:0];
}
I read data like this:
- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
NSString *receivedInfo = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[info_data setData:data];
NSLog(@"DID READ DATA WITH TAG %ld", tag);
if ([receivedInfo isEqualToString:ROOM_FILLED])
{
isMaster = (tcpRequest.identifier == MASTER_CHAR);
NSLog(@"IS MASTER SET %d", isMaster);
[multiplayerDelegate setGameModeServer];
[multiplayerDelegate startGame];
}
else
[self dataProcessing:info_data];
[sender readDataWithTimeout:-1 tag:0];
}
- (void)dataProcessing:(NSData *)data
{
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
MultiPlayerTypeInfo typeInfo = [[dict objectForKey:@"type_info"] intValue];
}
My issue that these packets of data get messed. Say a packet marked with tag 10 is read at the receiver device as packet marked with tag 11, which was sent immediately after packet 10, and when it comes to unarchiving of actual packet 11 NSKeyedUnarchiver throws exception Incomprehensible archive.
As far as i understand i should separate the packets somehow. What i tried was appending separatory symbols to the data being sent:
[data appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
and trying to read it like this:
[socket readDataToData:[GCDAsyncSocket CRLFData] timeout:-1 tag:some_tag];
But it didn’t help. What am i doing wrong and what should i do instead?
I guess, you misunderstood the role of the tag.
GCDAsyncSocketis (as the name suggests) an asynchrone socket. The tag helps you to match the received data with the receive order and the send succes with the send order, resp.E.g., if you want to send data, you use
writeData:messageA withTimeout:-1 tag: tagA(or something similar) to give your socket the order to send somewhen in the near future. It won’t be necessarily right now. And you can immediately give the next order to send another message, saymessageBwith tagtagB.To know, that the
messageAwas really sent, you get the notification viasocket:aSocket didWriteDataWithTag:aTag. Here,aTaghas the value oftagAifmessageAwas sent, and the value oftagBifmessageBwas sent. The tag is not sent with the message; it only helps you to identify your order.It is the very same thing at the receiving side. You give the order to receive (somewhen) some data and assign a tag to that very order. Once you did receive data, the notification (via
socket:didReadData:withTag:) shows you the tag to let you know, which order succeed.You may use the tag for some semantic information and put it in your message. But even then, the tag in the notification is the tag of the receive order, but never the one of the send order. If you want to use the tag you put in the message at the receiving side, you have to receive (at least parts of) the message first and parse it.
To come to the core of your issue: You have basically two possibilities to know, which kind of data is arriving:
EDIT
Here is an example for the 2nd approach. Assume you can sent a number of object of classes A, B, etc. Your header could include type and size of your data:
Once you want to send an object
objwith objKey:If you want, you can get notifications on successful sending or errors, but I skip this here.
At receiver side, you expect a header first:
In your
socket:didReadData:withTag:you have to distinguish, if you get the header or the remains (the receiving of the remains is initiated here!)This is not the most elegant example, and it ignores object trees and inter-object dependencies in archives, but you should get the idea.