Re: A NSOutputStream vs. native socket question
Re: A NSOutputStream vs. native socket question
- Subject: Re: A NSOutputStream vs. native socket question
- From: Josh Graessley <email@hidden>
- Date: Thu, 14 Nov 2013 11:10:40 -0800
On Nov 14, 2013, at 10:53 AM, Motti Shneor <email@hidden> wrote:
> Hi Everyone. First post to this group.
>
> I'm controlling the output stream created by
>
> ::CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)hostAddress, portNum, &readStream, &writeStream);
> ::CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
> ::CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
>
> and later bridged to NSInputStream and NSOutputStream instances,
> via its internal native socket, using the posix getsockopt and setsockopt calls.
>
> The properties I need to work with are the read (input) and write (output) buffer sizes, and the write (output) socket's buffer pending content size.
>
> Things like this...
> oss = getsockopt(socket,SOL_SOCKET,SO_RCVBUF,&value,&valueSize);
> oss = setsockopt(socket,SOL_SOCKET,SO_SNDBUF, &value, sizeof(value);
> oss = getsockopt(socket,SOL_SOCKET,SO_NWRITE, &value, &valueSize);
>
> etc.
>
> To retrieve the native socket I'm using code like this:
>
> // Getting the amount of bytes in the output socket buffer which are waiting to be sent
> - (int) getSocketOutputBufferPendingSize:(uint32_t*)bufSize {
> int oss = -1;
> CFDataRef data = NULL;
> do {
> if (_outputStream==nil)
> break;
>
> // retrieve native socket handle from output stream.
> if((data = (CFDataRef)::CFWriteStreamCopyProperty((CFWriteStreamRef)_outputStream, kCFStreamPropertySocketNativeHandle))== NULL)
> break;
>
> CFSocketNativeHandle socket;
> CFDataGetBytes(data, CFRangeMake(0, sizeof(CFSocketNativeHandle)), (UInt8*)&socket);
> .
> .
> . // --- here I work with the native socket properties.
> .
> } while (false);
>
> if(data!= NULL)
> ::CFRelease(data);
> return oss;
> }
>
> My questions are:
>
> 1. What is the lifespan of the native socket handle?
> 2. Can I retrieve it once, when the socket is created or opened the first time, then use it until socket is closed, or should I retrieve it from the stream like this, every time I want to use it?
> 3. What is the cost of the above lines that retrieve the native socket handle?
I hate to answer a question with another question, but…
Why are you modifying SO_SNDBUF? Modifying SO_SNDBUF and SO_RCVBUF means the system can’t dynamically adjust these values based on network conditions. If you’re hand tuning these values, that might have a small improvement in performance in one network environment but it is likely to have a negative performance impact in other environments.
Fetching the native file descriptor out of the socket stream does work but it doesn’t come without cost.
-josh
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden