What does the NSStreamNetworkServiceTypeBackground "do under the hood" setting do?

I have my own application (not for the distribution of the iPhone store, so it is not subject to Apple approval), which needs to support a handful of TCP and UDP sockets during its operation.

During testing, I noticed some odd behavior with my associated socket - it closes whenever the device goes into sleep mode. This happens, for example, when you press the power button on the top of the phone, and I really do not want this to be done. Not to mention that this seems like a gross contract violation of the BSD socket API.

The socket code is all written in C, so it does not use the Objective-C nsstream libraries .

I noticed that there are various ways to save sockets for things like VOIP, music streaming and others. The nsstream documentation itself contains the NSStreamNetworkServiceTypeBackground property, which seems to be able to help my application open its socket when the phone goes into sleep mode. The following properties are listed at the bottom of the apple documents:

Constants

NSStreamNetworkServiceTypeVoIP
Indicates that the stream is providing a VoIP service.

NSStreamNetworkServiceTypeVideo
Indicates that the stream is providing a video service.

NSStreamNetworkServiceTypeBackground
Indicates that the thread is providing background service.

NSStreamNetworkServiceTypeVoice
Indicates that the stream is providing voice service.

But since the network code of the application does not use the Objective-C API, 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 so that it doesn't bother with its socket when the device is sleeping.

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

+3
source share
2 answers

If you use CFStream for networking, you can do this:

 NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream; NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream; 

After that, you can use the constants you already found with ObjC NSInputStream and NSOutputStream . ( NSInputStream and NSOutputStream extended by NSStream .)

0
source

clientSocket c and should already be connected, and then use the c object:

 @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] 
0
source

All Articles