0

I'm working on an application which can send out files and actions to users. Users can request a file at any time from the server (client server model). Sometimes files can be fairly large (e.g 10 mb) which whilst sending can delay the other small actions from being sent to connected users. It seems that Gamekit has one thread for sending and a separate one for receiving.

I am wondering if it is possible to have an additional sending thread to ensure the smaller action packets do not have to wait for a large file to be sent. I have tried to create a queue with grand central dispatch but unfortunately the sending still only appeared to use one thread.

dispatch_async(pdfSendQ, ^{

    NSString *filePath = [_document.fileURL path] ;


    if (!_pdfNSData)
        _pdfNSData = [NSData dataWithContentsOfFile:filePath];

    for(NSData* packet in self.packets) {
        NSError* error = nil;
        [_session sendData:packet toPeers:[NSArray arrayWithObject:peerID] withDataMode:GKSendDataReliable error:&error];
        if(error != nil) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kPacketFailedNotification object:self userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]];
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:kPacketSentNotification object:self];
        }
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:kFileSentNotification object:self];

});

I guess it would be possible to get the separate smaller action packets to 'interrupt' the large file sending, however this would be fairly complex and I was wondering if there was a much simpler way.

bencallis
  • 3,478
  • 4
  • 32
  • 58

2 Answers2

2

GKSession does all of the actual networking on its own thread. Take a look in lldb / Xcode for more details. If you want to send 10MB in chunks, just send the data. GKSession will enqueue the writes for you and send when possible on the background thread. In other words, don't worry about starving the main thread, just write the data.

Mark Pauley
  • 1,445
  • 12
  • 11
0

To be honest I don't have experience with GameKit, but in the end it must be a stream socket, right? Instead of trying to send a huge packet with the 10mb file, why not send it in smaller 100kb chunks. that way, instead of interrupting mid-chunk, you can just wait for it to finish and queue a different type of packet (smaller action packets) you mention. On the other side it's just a matter of appending the pieces of the big file together, but if you get something else you'll be able to process it. That way you get it more responsive

Fernando Mazzon
  • 3,562
  • 1
  • 19
  • 21
  • I currently do split the file up into packets. These are then sent in a loop (I assume it adds them all the a queue). I guess what I need is to be able to add the action packets to the front of the queue, unsure if it is possible though. – bencallis Jan 03 '13 at 20:01
  • Don't do that(would be bad to mess with the queue order), but instead just queue the first packet and set some kind of callback to yourself when it's done going through. Queue the following one when it is done sending. – Fernando Mazzon Jan 03 '13 at 20:10