bencallis
bencallis

Reputation: 3668

GameKit Multithreaded sending possible?

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.

Upvotes: 0

Views: 197

Answers (2)

Mark Pauley
Mark Pauley

Reputation: 1485

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.

Upvotes: 2

Fernando Mazzon
Fernando Mazzon

Reputation: 3591

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

Upvotes: 0

Related Questions