4

I am using cfwritestream to read and write into socket for iOS. i need to disable Nagle's algorithm so as to increase the writing efficiency of the socket and have made changes to include TCP_NODELAY, however, from the pcap logs I dont see that Nagle's algorithm has been disabled.

Following is my bit of code to enable the tcp_nodelay option for the socket. This is done at the time wrapsocket is called to wrap the native socket to the cfsocketstream.

// Wrap socket and listen to events
if (!sock->cf_socket && !sock->cf_read_stream && !sock->cf_write_stream) {
    sock->cf_socket = CFSocketCreateWithNative(kCFAllocatorDefault,
                                               sock->fd,
                                               kCFSocketReadCallBack | kCFSocketConnectCallBack | kCFSocketWriteCallBack | kCFSocketAcceptCallBack | kCFSocketDataCallBack,
                                               &__CFSocketCallBack,
                                               &socket_context);

        // Don't close underlying socket
        CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
        CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);


        // Setup a context for the streams
        CFStreamClientContext streamContext = { 0, transport, NULL, NULL, NULL };

        // Set the client callback for the stream
        CFReadStreamSetClient(sock->cf_read_stream,
                              kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
                              &__CFReadStreamClientCallBack,
                              &streamContext);
        CFWriteStreamSetClient(sock->cf_write_stream,
                               kCFStreamEventOpenCompleted | kCFStreamEventErrorOccurred | kCFStreamEventCanAcceptBytes |kCFStreamEventEndEncountered,
                               &__CFWriteStreamClientCallBack,
                               &streamContext);


        int yes=1;
        CFDataRef socketData = CFWriteStreamCopyProperty(sock->cf_write_stream, kCFStreamPropertySocketNativeHandle);
        CFSocketNativeHandle handle;
        CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), &handle);
        int result=setsockopt(handle, IPPROTO_TCP , TCP_NODELAY , (void *)&yes, sizeof(yes));

Please let me know if I am missing something.

scooby
  • 493
  • 11
  • 31
  • Can you show us a small, relevant sample of pcap's output? Also, are you checking the value of `int result=setsockopt(handle, IPPROTO_TCP, TCP_NODELAY,...` – John Hascall Jan 08 '16 at 13:01
  • Yes I was checking for the return value. It is returning 0. in pcap logs I see that ACK for a certain msrp packet is reaching the device after a longg time, almost 2 secs. Till that time device is not sending any msrp packets, since it is waiting for the ack of the previous packet to come. – scooby Jan 08 '16 at 13:19
  • A 2 second delay seems long for a Nagle problem. And you are sure that the delay in sending is at the TCP level and not caused by a higher-level protocol (msrp) waiting for data? One clue would be are the packets that unclog the connection ACK-only or do they contain data? – John Hascall Jan 08 '16 at 13:29
  • Is there a way by which i can share the pcap log snippet? I understand your concern about the delay from msrp layer, but it doesnt seem so. Although the packets that are getting delayed in sending are data packets. – scooby Jan 08 '16 at 13:46
  • Is there data in the ACK packet? – John Hascall Jan 08 '16 at 13:49
  • no, no data in the ACK packet. – scooby Jan 08 '16 at 14:12
  • 2
    Via google I found some other accounts of people complaining that iOS would accept the TCP_NODELAY option, but not actually do it. Looks like what you are seeing corroborates that. You might be able to improve the situation if you can disable "delayed ack" on the other end. – John Hascall Jan 08 '16 at 14:24
  • thanks john for your effort. – scooby Jan 11 '16 at 07:42

0 Answers0