我正在使用MPCF创建多人游戏。您可以使用iPhone在iPad上控制 spaceship 。
我在随机的时间和间隔经历各种延迟和延迟以及缓冲/暂停,现在进入了苹果公司的Technical Q&A NW26论文,该论文讨论了禁用Nagle算法。我正在尝试,但是我的程序不断崩溃,我不明白为什么。似乎CFWriteStreamCopyProperty总是返回NULL。
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventOpenCompleted:
// Trying to get a handle to the native socket
CFSocketNativeHandle rawsock;
// This always return NULL
CFDataRef socketData = CFWriteStreamCopyProperty((__bridge CFWriteStreamRef)(stream), kCFStreamPropertySocketNativeHandle);
// And this row always crash (coz of socketData being NULL i guess)
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), (UInt8 *)&rawsock);
CFRelease(socketData);
// Code example from Apple that need a handle to the native socket, that I am trying to get above
int err;
static const int kOne = 1;
err = setsockopt(rawsock, IPPROTO_TCP, TCP_NODELAY, &kOne, sizeof(kOne));
if (err < 0) {
err = errno;
}
break;
default:
break;
}
}
任何帮助表示赞赏。
最佳答案
您假设stream
是NSOutputStream
,但是也可以传递NSInputStream
。
因此它在那条线上崩溃了。
这是可以处理两种情况并修复崩溃的示例代码:
- (void)disableNaglesAlgorithmForStream:(NSStream *)stream {
CFDataRef socketData;
// Get socket data
if ([stream isKindOfClass:[NSOutputStream class]]) {
socketData = CFWriteStreamCopyProperty((__bridge CFWriteStreamRef)((NSOutputStream *)stream), kCFStreamPropertySocketNativeHandle);
} else if ([stream isKindOfClass:[NSInputStream class]]) {
socketData = CFReadStreamCopyProperty((__bridge CFReadStreamRef)((NSInputStream *)stream), kCFStreamPropertySocketNativeHandle);
}
// get a handle to the native socket
CFSocketNativeHandle rawsock;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), (UInt8 *)&rawsock);
CFRelease(socketData);
// Disable Nagle's algorythm
// Debug info
BOOL isInput = [stream isKindOfClass:[NSInputStream class]];
NSString * streamType = isInput ? @"INPUT" : @"OUTPUT";
int err;
static const int kOne = 1;
err = setsockopt(rawsock, IPPROTO_TCP, TCP_NODELAY, &kOne, sizeof(kOne));
if (err < 0) {
err = errno;
NSLog(@"Could Not Disable Nagle for %@ stream", streamType);
} else {
NSLog(@"Nagle Is Disabled for %@ stream", streamType);
}
}
应该在
case NSStreamEventOpenCompleted:
中调用它对于任何想知道为什么复制粘贴后无法编译的人:
#import <arpa/inet.h> // for IPPROTO_TCP
#include <netinet/tcp.h> // for TCP_NODELAY
但是,它不能解决周期性延迟问题。
我仍在寻找防止这种情况的方法。
我录制了一个演示该问题的简单视频,我相信您也遇到了同样的问题:
https://www.dropbox.com/s/omdqkbckph4b1y2/Multipeer%20Connectivity.mov?dl=0
编辑:我能够找到一种解决周期性延迟问题的方法。这是导致问题的
MCNearbyServiceAdvertiser
。您需要停止向同行发布广告,以摆脱滞后:
连接成功后,立即在
stopAdvertisingPeer
实例上调用MCNearbyServiceAdvertiser
方法。MCNearbyServiceAdvertiser *nearbyServiceAdvertiser;
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
switch (state) {
case MCSessionStateConnected: {
dispatch_async(dispatch_get_main_queue(), ^{
[self.nearbyServiceAdvertiser stopAdvertisingPeer];
});
...
}
...
}
但是,只有在 30秒之后,延迟才会关闭。我不知道如何使它立即消失。
关于ios - 禁用Nagle的NSOutputStream算法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23810373/