iOS NSStream 连接似乎暂停

发布于 2024-10-21 10:15:33 字数 6756 浏览 1 评论 0原文

我正在尝试使用 http://code.google.com/p/iphonebits/source/browse/trunk/src/TicTacToeNew/

客户端连接良好,并且数据在两个设备之间传输得很好。客户端每秒发送约10次数据包。然而,我在接收数据之间遇到了非常奇怪的停顿。

在接收代码中,每次收到请求时我都会放置一条调试语句:

- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
     NSLog(@"stream: handleEvent : %d", eventCode);
         switch(eventCode) {
            // ... handle event and print out received data
         }
}

相关的发送代码位于此处的 sendQueuedData 函数中: http://code.google.com/p/iphonebits/source/browse/trunk/src/TicTacToeNew/Classes /OnlineSession.m?r=19

请求的第一秒左右按计划进行(尽管它似乎将请求一起批处理):

2011-03-13 12:28:08.759 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.98,"y":0.01}
[DEBUG] Fire Event: data: {"x":1.98,"y":0.00}
[DEBUG] Fire Event: data: {"x":2.11,"y":0.01}
2011-03-13 12:28:08.781 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":2.07,"y":0.03}
[DEBUG] Fire Event: data: {"x":2.02,"y":0.03}
2011-03-13 12:28:08.923 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.98,"y":0.01}
[DEBUG] Fire Event: data: {"x":1.86,"y":0.01}
2011-03-13 12:28:08.996 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.73,"y":0.01}
2011-03-13 12:28:09.171 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":2.38,"y":-0.56}
2011-03-13 12:28:09.182 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.332 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.496 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.501 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.598 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}

...但请注意后续调用的时间戳:

2011-03-13 12:28:10.163 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.770 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}    
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.772 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.777 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.868 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.016 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.177 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.340 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.424 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:15.065 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:16.338 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:18.740 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:18.741 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}

如您所见,handleEvent()回调被偶尔调用,显然不适合实时应用程序。有谁知道为什么?我是否应该从使用 NSStreams 切换到 http://code.google.com/p/cocoaasyncsocket/ 之类的东西 使用 UDP?

I'm trying to send data between two iOS devices using example code from http://code.google.com/p/iphonebits/source/browse/trunk/src/TicTacToeNew/

The client connects well, and data is transferred between the two devices just fine. The client sends packets of data about 10 times/second. However, I'm getting very odd pauses between data being received.

In the receiving code, I'm putting a debug statement every time a request is received :

- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
     NSLog(@"stream: handleEvent : %d", eventCode);
         switch(eventCode) {
            // ... handle event and print out received data
         }
}

The relevant send code is in the sendQueuedData function here: http://code.google.com/p/iphonebits/source/browse/trunk/src/TicTacToeNew/Classes/OnlineSession.m?r=19

The first second or so of requests goes as planned (although it seems to batch requests together):

2011-03-13 12:28:08.759 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.98,"y":0.01}
[DEBUG] Fire Event: data: {"x":1.98,"y":0.00}
[DEBUG] Fire Event: data: {"x":2.11,"y":0.01}
2011-03-13 12:28:08.781 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":2.07,"y":0.03}
[DEBUG] Fire Event: data: {"x":2.02,"y":0.03}
2011-03-13 12:28:08.923 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.98,"y":0.01}
[DEBUG] Fire Event: data: {"x":1.86,"y":0.01}
2011-03-13 12:28:08.996 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.73,"y":0.01}
2011-03-13 12:28:09.171 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":2.38,"y":-0.56}
2011-03-13 12:28:09.182 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.332 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.496 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.501 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.598 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}

... but then notice the time stamps on the subsequent calls:

2011-03-13 12:28:10.163 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.770 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}    
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.772 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.777 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.868 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.016 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.177 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.340 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.424 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:15.065 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:16.338 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:18.740 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:18.741 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}

As you can see, the handleEvent() callback is being called sporadically, and is obviously not suitable to a real-time application. Does anyone know why? Should I be switching from using NSStreams to something like http://code.google.com/p/cocoaasyncsocket/ with UDP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文