tSet SO_LINGER, flush using TCP_NODELAY and fix wait overflow. - geomyidae - A small C-based gopherd. (gopher://bitreich.org/1/scm/geomyidae)
git clone git://r-36.net/geomyidae
Log
Files
Refs
README
LICENSE
---
commit c99b01b00b1c6d0740e92d9e77ab8e0daa96c4bf
parent 1375ce4688358eca4db5bd1f0309863a574d250e
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed, 22 Jul 2020 14:41:10 +0200

Set SO_LINGER, flush using TCP_NODELAY and fix wait overflow.

Diffstat:
  ind.c                               |       3 ++-
  main.c                              |      17 ++++++++++++++++-

2 files changed, 18 insertions(+), 2 deletions(-)
---
diff --git a/ind.c b/ind.c
t@@ -79,7 +79,8 @@ pendingbytes(int sock)
 void
 waitforpendingbytes(int sock)
 {
-        int npending = 0, opending = 0, trytime = 10;
+        int npending = 0, opending = 0;
+        useconds_t trytime = 10;
 
         /*
          * Wait until there is nothing pending or the connection stalled
diff --git a/main.c b/main.c
t@@ -15,6 +15,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
t@@ -416,6 +417,7 @@ main(int argc, char *argv[])
 {
         struct addrinfo hints;
         struct sockaddr_storage clt;
+        struct linger lingerie;
         socklen_t cltlen;
         int sock, dofork = 1, inetf = AF_UNSPEC, usechroot = 0,
             nocgi = 0, errno_save, nbindips = 0, i, j,
t@@ -867,9 +869,22 @@ main(int argc, char *argv[])
                                         clientp, nocgi, istls);
 
                         if (!istls) {
+                                lingerie.l_onoff = 1;
+                                lingerie.l_linger = 60;
+                                setsockopt(sock, SOL_SOCKET, SO_LINGER,
+                                                &lingerie, sizeof(lingerie));
+                                /*
+                                 * Force explict flush of buffers using
+                                 * TCP_NODELAY.
+                                 */
+                                j = 1;
+                                setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
+                                                &j, sizeof(int));
                                 waitforpendingbytes(sock);
+                                j = 0;
+                                setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
+                                                &j, sizeof(int));
                                 shutdown(sock, SHUT_RDWR);
-                                close(sock);
                         }
                         close(sock);