简体   繁体   English

GameKit多线程发送可能吗?

[英]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 (eg 10 mb) which whilst sending can delay the other small actions from being sent to connected users. 有时文件可能相当大(例如10 MB),而发送时可能会延迟其他小动作被发送给连接用户。 It seems that Gamekit has one thread for sending and a separate one for receiving. Gamekit似乎有一个用于发送的线程和一个用于接收的独立线程。

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. 我想有可能得到单独的较小的动作包来'中断'大文件发送,但是这将是相当复杂的,我想知道是否有一个更简单的方法。

GKSession does all of the actual networking on its own thread. GKSession在自己的线程上完成所有实际的网络连接。 Take a look in lldb / Xcode for more details. 有关详细信息,请查看lldb / Xcode。 If you want to send 10MB in chunks, just send the data. 如果要以块的形式发送10MB,只需发送数据即可。 GKSession will enqueue the writes for you and send when possible on the background thread. GKSession将为您排队写入并在后台线程上尽可能发送。 In other words, don't worry about starving the main thread, just write the data. 换句话说,不要担心主线程的饥饿,只需写入数据。

To be honest I don't have experience with GameKit, but in the end it must be a stream socket, right? 说实话,我没有使用GameKit的经验,但最终它必须是一个流套接字,对吧? Instead of trying to send a huge packet with the 10mb file, why not send it in smaller 100kb chunks. 而不是尝试使用10mb文件发送一个巨大的数据包,为什么不用较小的100kb块发送它。 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 这样你就能让它更具响应性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM