facebookincubator / SocketRocket

A conforming Objective-C WebSocket client library.
Other
9.56k stars 2.01k forks source link

[request] Disable Nagle's algorithm #448

Open finscn opened 8 years ago

finscn commented 8 years ago

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

finscn commented 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);
    }
}
xcrdev commented 5 years ago

I would like to see tcp_nodelay as a socket option in the master branch.