Open finscn opened 8 years ago
I add this feature in my fork with:
- (void)disableNaglesAlgorithmForStream:(NSStream *)stream {
CFDataRef socketData = NULL;
// 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 = (CFSocketNativeHandle *)CFDataGetBytePtr(socketData);
// Disable Nagle's algorythm
static const int kOne = 1;
int err = setsockopt(rawsock, IPPROTO_TCP, TCP_NODELAY, &kOne, sizeof(kOne));
if (socketData) {
CFRelease(socketData);
}
// Debug info
BOOL isInput = [stream isKindOfClass:[NSInputStream class]];
NSString * streamType = isInput ? @"INPUT" : @"OUTPUT";
if (err < 0) {
NSLog(@"Could Not Disable Nagle for %@ stream", streamType);
} else {
NSLog(@"Nagle Is Disabled for %@ stream", streamType);
}
}
I would like to see tcp_nodelay as a socket option in the master branch.
Disable Nagle's algorithm could improved performance. And many browsers use nagle disabled sockets by default,
https://groups.google.com/forum/#!topic/native-client-discuss/T8zdrMjiTAE
https://bugs.webkit.org/show_bug.cgi?id=102079
There is thread talk about it : http://stackoverflow.com/questions/23810373/disable-nagles-algorithm-for-nsoutputstream