disable nagle algorithm
authorAndy Green <andy@warmcat.com>
Tue, 8 Mar 2011 08:56:57 +0000 (08:56 +0000)
committerAndy Green <andy@warmcat.com>
Tue, 8 Mar 2011 08:56:57 +0000 (08:56 +0000)
Ago noticed that some Windows clients experience small packets
from the server being aggregated and set after a long delay
(200-300ms).

He found that TCP_NODELAY on the socket solved this, I tested it
and it didn't have any noticable bad effect, so I implemented it
for all sockets, client and server.

Thans Ago for debugging this and notifying the cause.

Reported-by: Ago Allikmaa <maxorator@gmail.com>
Signed-off-by: Andy Green <andy@warmcat.com>
lib/client-handshake.c
lib/libwebsockets.c
lib/private-libwebsockets.h

index 74f894d..f583501 100644 (file)
@@ -38,6 +38,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
        struct pollfd pfd;
        struct libwebsocket *wsi;
        int n;
+       int opt = 1;
        int plen = 0;
 #ifndef LWS_OPENSSL_SUPPORT
        if (ssl_connection) {
@@ -164,6 +165,9 @@ libwebsocket_client_connect(struct libwebsocket_context *context,
        server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
        bzero(&server_addr.sin_zero, 8);
 
+       /* Disable Nagle */
+       setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
+
        if (connect(wsi->sock, (struct sockaddr *)&server_addr,
                                              sizeof(struct sockaddr)) == -1)  {
                fprintf(stderr, "Connect failed\n");
index eb9cc85..80d17de 100644 (file)
@@ -641,6 +641,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
        struct lws_tokens eff_buf;
        int ext_count = 0;
        struct libwebsocket_extension *ext;
+       int opt = 1;
 
 #ifdef LWS_OPENSSL_SUPPORT
        char ssl_err_buf[512];
@@ -706,6 +707,10 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
                        break;
                }
 
+               /* Disable Nagle */
+               opt = 1;
+               setsockopt(accept_fd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
+
                if (context->fds_count >= MAX_CLIENTS) {
                        fprintf(stderr, "too busy to accept new client\n");
 #ifdef WIN32
@@ -2416,6 +2421,11 @@ libwebsocket_create_context(int port, const char *interf,
                /* allow us to restart even if old sockets in TIME_WAIT */
                setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
 
+
+               /* Disable Nagle */
+               opt = 1;
+               setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
+
                bzero((char *) &serv_addr, sizeof(serv_addr));
                serv_addr.sin_family = AF_INET;
                if (interf == NULL)
index 6d3f1c8..2a11c11 100644 (file)
@@ -48,6 +48,7 @@
 #include <sys/prctl.h>
 #endif
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <arpa/inet.h>
 
 #include <poll.h>