什么是设置NSStreamNetworkServiceTypeBackground"引擎盖&QUOT下做;? [英] What does setting NSStreamNetworkServiceTypeBackground "do under the hood"?

查看:373
本文介绍了什么是设置NSStreamNetworkServiceTypeBackground"引擎盖&QUOT下做;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内部的应用程序(不适用于iPhone专卖店分布,所以没有受到苹果的批准),它需要保持TCP一把,它在运行时绑定UDP套接字。

I have an in-house app (not for iPhone store distribution, so not subject to Apple approval) which needs to keep a handful of TCP and UDP sockets bound while it is running.

虽然测试中,我注意到我的绑定套接字一些奇怪的行为 - 只要设备进入睡眠状态,它被关闭。出现这种情况,例如,当你去preSS在手机顶部的电源按钮,我真的不希望它这样做。更何况,这似乎是一个严重违反BSD套接字API的合同。

While testing I have noticed some odd behavior with my bound socket - it gets closed whenever the device goes to sleep. This happens, for example, when you depress the power button on top of the phone, and I really don't want it to do that. Not to mention that it seems like a gross violation of the BSD sockets API contract.

插座code是所有用C写的,所以不使用Objective-C的<一个href=\"http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSStream_Class/Reference/Reference.html\"相对=nofollow> nsstream 库。

The socket code is all written in C, so isn't using the Objective-C nsstream libraries.

我注意到,有各种不同的方法来保持插座开放的东西像VOIP,音乐流媒体,和其他的东西。该nsstream文档本身列出这似乎是它可以帮助我的应用程序保持其套接字打开时,手机进入睡眠状态一个 NSStreamNetworkServiceTypeBackground 属性。苹果文档的底部列出以下属性:

I noticed that there are various ways to keep sockets open for things like VOIP, music streaming, and other things. The nsstream documentation itself lists a NSStreamNetworkServiceTypeBackground property which seems like it might help my app keep its socket open when the phone goes to sleep. The bottom of the apple docs list the following properties:

常量结果
  

NSStreamNetworkServiceTypeVoIP
  结果 指定流提供VoIP服务。
  

NSStreamNetworkServiceTypeVideo
  结果 指定流
  正在提供视频服务。
  

NSStreamNetworkServiceTypeBackground
  结果 指定
  流被提供后台服务。
  

NSStreamNetworkServiceTypeVoice
  结果 指定流提供语音服务。

Constants

NSStreamNetworkServiceTypeVoIP
Specifies that the stream is providing VoIP service.

NSStreamNetworkServiceTypeVideo
Specifies that the stream is providing video service.

NSStreamNetworkServiceTypeBackground
Specifies that the stream is providing a background service.

NSStreamNetworkServiceTypeVoice
Specifies that the stream is providing voice service.

但由于我的应用程序的网络code未使用的Objective-C API的,我需要知道:如何将上述参数来实现?也许有一个未公开的套接字选项,我可以用它来达到同样的效果?基本上我只需要不告诉内核来惹我的插座,当设备处于休眠状态。

But since my app's network code isn't using the Objective-C API's, I need to know: how are the above constants implemented? Maybe there is an undocumented socket option that I can use to achieve the same result? Basically I just need to tell the kernel not to mess with my socket when the device sleeps.

修改:作为一种替代逆向工程内核调用,也许有一种方式来传递我的套接字文件描述符的Objective-C code可以应用这些选项。

edit: as an alternative to reverse engineering kernel calls, maybe there is a way to pass my socket file descriptors to Objective-C code where these options can be applied?

推荐答案

ClientSocket的交流电插座,且必须已连接,然后使用对象C:

clientSocket c socket and must have already been connected, then use object c:

@property (nonatomic, strong) NSInputStream *inputStream ;
@property (nonatomic, strong) NSOutputStream *outputStream ;

CFReadStreamRef readStreamRef = nil ;
CFWriteStreamRef writeStreamRef = nil ;
CFStreamCreatePairWithSocket(NULL, clientSocket, &readStreamRef, &writeStreamRef) ; // the socket must have already been connected.
_inputStream = (__bridge_transfer NSInputStream *)readStreamRef ;
_outputStream = (__bridge_transfer NSOutputStream *)writeStreamRef ;
[_inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[_outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[_inputStream open] ;
[_outputStream open] 

这篇关于什么是设置NSStreamNetworkServiceTypeBackground&QUOT;引擎盖&QUOT下做;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆