<i id='SFeXf'><tr id='SFeXf'><dt id='SFeXf'><q id='SFeXf'><span id='SFeXf'><b id='SFeXf'><form id='SFeXf'><ins id='SFeXf'></ins><ul id='SFeXf'></ul><sub id='SFeXf'></sub></form><legend id='SFeXf'></legend><bdo id='SFeXf'><pre id='SFeXf'><center id='SFeXf'></center></pre></bdo></b><th id='SFeXf'></th></span></q></dt></tr></i><div id='SFeXf'><tfoot id='SFeXf'></tfoot><dl id='SFeXf'><fieldset id='SFeXf'></fieldset></dl></div>
      • <bdo id='SFeXf'></bdo><ul id='SFeXf'></ul>

      <small id='SFeXf'></small><noframes id='SFeXf'>

    1. <legend id='SFeXf'><style id='SFeXf'><dir id='SFeXf'><q id='SFeXf'></q></dir></style></legend><tfoot id='SFeXf'></tfoot>

        禁用 NSOutputStream 的 Nagle 算法

        时间:2023-10-05

          <small id='4jren'></small><noframes id='4jren'>

              <bdo id='4jren'></bdo><ul id='4jren'></ul>
            • <legend id='4jren'><style id='4jren'><dir id='4jren'><q id='4jren'></q></dir></style></legend>
              <i id='4jren'><tr id='4jren'><dt id='4jren'><q id='4jren'><span id='4jren'><b id='4jren'><form id='4jren'><ins id='4jren'></ins><ul id='4jren'></ul><sub id='4jren'></sub></form><legend id='4jren'></legend><bdo id='4jren'><pre id='4jren'><center id='4jren'></center></pre></bdo></b><th id='4jren'></th></span></q></dt></tr></i><div id='4jren'><tfoot id='4jren'></tfoot><dl id='4jren'><fieldset id='4jren'></fieldset></dl></div>
            • <tfoot id='4jren'></tfoot>

                <tbody id='4jren'></tbody>

                  本文介绍了禁用 NSOutputStream 的 Nagle 算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 MPCF 创建多人游戏.您使用 iPhone 在 iPad 上控制宇宙飞船.

                  I am creating a multiplayer game using MPCF. You control a spacehip on the iPad using the iPhone.

                  我在随机时间和间隔遇到各种延迟和延迟以及缓冲/暂停,现在已经登陆 Apple 技术问答 NW26 论文讨论禁用 Nagle 算法.我尝试了一下,但我的程序不断崩溃,我不明白为什么.CFWriteStreamCopyProperty 似乎总是返回 NULL.

                  I am experiencing various amount of lag and latency and buffering/pauses at random times and intervals and have now landed on Apples Technical Q&A NW26 paper that talks about disabled the Nagle Algorithm. Im trying it out but my program keeps crashing and I dont understand why. It seem to be that CFWriteStreamCopyProperty always return 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也.所以它在那条线上崩溃了.

                  You assuming that stream is an NSOutputStream, but there could be passed NSInputStream also. So it crashes on that line.

                  这是一个可以处理这两种情况并修复崩溃的示例代码:

                  Here is a sample code that can handle both scenarios and fixes a crash:

                  - (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:

                  对于任何想知道为什么复制粘贴时无法编译的人:

                  For anyone wondering why it does not compile when copy-pasted:

                  #import <arpa/inet.h>       // for IPPROTO_TCP
                  #include <netinet/tcp.h>    // for TCP_NODELAY
                  

                  但是,它对周期性延迟问题没有帮助.我仍在寻找防止这种情况发生的方法.

                  However, it does NOT help with periodical latency issues. I'm still searching for a way to prevent that.

                  我录制了一个演示该问题的简单视频,我相信您也遇到了同样的问题:https://www.dropbox.com/s/omdqkbckph4b1y2/Multipeer%20Connectivity.mov?dl=0

                  I recorded a simple video that demonstrates the issue and I believe you are experiencing the same one: https://www.dropbox.com/s/omdqkbckph4b1y2/Multipeer%20Connectivity.mov?dl=0

                  我能够找到解决周期性延迟问题的方法.导致问题的是 MCNearbyServiceAdvertiser.您需要停止为对等点做广告以消除滞后:连接成功后立即在您的 MCNearbyServiceAdvertiser 实例上调用 stopAdvertisingPeer 方法.

                  I was able to find a way to fix a periodical latency issues. It is an MCNearbyServiceAdvertiser that causes the issues. You need to stop advertising a peer in order to get rid of lags: call a stopAdvertisingPeer method on your MCNearbyServiceAdvertiser instance right after the connection succeeded.

                  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 秒后关闭.不知道怎么让它马上消失.

                  BUT the delays will turn off only after 30 seconds. I don't know how to make it disappear right away.

                  这篇关于禁用 NSOutputStream 的 Nagle 算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <small id='DNkpl'></small><noframes id='DNkpl'>

                1. <legend id='DNkpl'><style id='DNkpl'><dir id='DNkpl'><q id='DNkpl'></q></dir></style></legend>
                      <tbody id='DNkpl'></tbody>

                      <i id='DNkpl'><tr id='DNkpl'><dt id='DNkpl'><q id='DNkpl'><span id='DNkpl'><b id='DNkpl'><form id='DNkpl'><ins id='DNkpl'></ins><ul id='DNkpl'></ul><sub id='DNkpl'></sub></form><legend id='DNkpl'></legend><bdo id='DNkpl'><pre id='DNkpl'><center id='DNkpl'></center></pre></bdo></b><th id='DNkpl'></th></span></q></dt></tr></i><div id='DNkpl'><tfoot id='DNkpl'></tfoot><dl id='DNkpl'><fieldset id='DNkpl'></fieldset></dl></div>
                      <tfoot id='DNkpl'></tfoot>

                        <bdo id='DNkpl'></bdo><ul id='DNkpl'></ul>