Question

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?

Était-ce utile?

La solution

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, say messageB with tag tagB. To know, that the messageA was really sent, you get the notification via socket:aSocket didWriteDataWithTag:aTag. Here, aTaghas the value of tagA if messageAwas sent, and the value of tagB if messageB was 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:

  1. Know the sequence of sent data and receive it in the very same order.
  2. Use a message head that identifies the kind of data. Receive only the head and receive and parse the remains of your message in dependence of the head data.

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:

      typedef struct {
            NSUInteger type_id;
            NSUInteger size;
      } header_t;

      #define typeIdA   1
      #define typeIdB   2
      // ...

Once you want to send an object obj with objKey:

     NSMutableData *data = [NSMutableData data];
     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
     [archiver encodeObject:obj forKey: objKey];

     header_t head;
     if ([obj class] == [A class]) {
        head.type_id = typeIdA;
     } else if ([obj class] == [B class]) {
        head.type_id = typeIdB;
     } else ...

     // ....

     header.size = data.lengh;
     NSData* headData = [NSData dataWithBytes: &header length: sizeof(header)];

     dataWithBytes:length:
     header = NSData.length;
     [asyncSocket writeData:headData withTimeout:-1 tag:headTag];
     [asyncSocket writeData:data withTimeout:-1 tag:dataTag];

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:

    [receiveSocket readDataToLength:sizeof(header_t) withTimeout:-1 tag:rcvHdrTag];
    // rcvHdrTag must not match one of the typeIdX tags

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!)

   - (void)socket:(GCDAsyncSocket *)aSocket didReadData:(NSData *)data withTag:(long)tag {
        header_t head;
        id obj;
        id key;

       switch (tag) {
           case rcvHdrTag:
               [data getBytes:&head length:sizeof(header)];
               // now you know what to receive
               [aSocket readDataToLength:header.size withTimeout:-1 tag:header.type];
               return;
               break; // I know, redundancy :-)
          case typeIdA:
               objKey = objKeyA;   // whatever it is...
               break;
          case typeIdB:
               objKey = objKeyB;   
               // ....
       }
       NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
       obj = [unarchiver decodeObjectForKey:objKey];

       // store your object ...
   }

This is not the most elegant example, and it ignores object trees and inter-object dependencies in archives, but you should get the idea.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top