From 6c9395529ee6f5c4cc4d4a5e47c211ec7ad61644 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Tue, 8 Mar 2011 08:56:57 +0000 Subject: [PATCH] disable nagle algorithm 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 Signed-off-by: Andy Green --- lib/client-handshake.c | 4 ++++ lib/libwebsockets.c | 10 ++++++++++ lib/private-libwebsockets.h | 1 + 3 files changed, 15 insertions(+) diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 74f894d..f583501 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -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"); diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index eb9cc85..80d17de 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -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) diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 6d3f1c8..2a11c11 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -48,6 +48,7 @@ #include #endif #include +#include #include #include -- 2.7.4