have flexible timouts while reading lines in our client mode connections
[citadel.git] / citadel / clientsocket.c
index 159ec72125728f1ed8004d7c8712dcbbe3924d88..d8a3e6b017de5a3d919d6303b4c0957542afd3d0 100644 (file)
@@ -167,7 +167,7 @@ int sock_read_to(int *sock, char *buf, int bytes, int timeout,
 }
 
 
-int CtdlSockGetLine(int *sock, StrBuf * Target)
+int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
 {
        CitContext *CCC = MyContext();
        const char *Error;
@@ -177,7 +177,7 @@ int CtdlSockGetLine(int *sock, StrBuf * Target)
        rc = StrBufTCP_read_buffered_line_fast(Target,
                                               CCC->sReadBuf,
                                               &CCC->sPos,
-                                              sock, 5, 1, &Error);
+                                              sock, nSec, 1, &Error);
        if ((rc < 0) && (Error != NULL))
                CtdlLogPrintf(CTDL_CRIT,
                              "%s failed: %s\n", __FUNCTION__, Error);
@@ -197,7 +197,7 @@ int sock_getln(int *sock, char *buf, int bufsize)
        const char *pCh;
 
        FlushStrBuf(CCC->sMigrateBuf);
-       retval = CtdlSockGetLine(sock, CCC->sMigrateBuf);
+       retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
 
        i = StrLength(CCC->sMigrateBuf);
        pCh = ChrPtr(CCC->sMigrateBuf);
@@ -273,6 +273,19 @@ int sock_write(int *sock, const char *buf, int nbytes)
                        return (-1);
                }
                bytes_written = bytes_written + retval;
+               if (IsNonBlock && (bytes_written == nbytes)){
+                       tv.tv_sec = selectresolution;
+                       tv.tv_usec = 0;
+                       
+                       FD_ZERO(&rfds);
+                       FD_SET(*sock, &rfds);
+                       if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
+///                            *Error = strerror(errno);
+                               close (*sock);
+                               *sock = -1;
+                               return -1;
+                       }
+               }
        }
        return (bytes_written);
 }
@@ -284,14 +297,14 @@ int sock_write(int *sock, const char *buf, int nbytes)
  * (This is implemented in terms of client_read() and could be
  * justifiably moved out of sysdep.c)
  */
-int sock_getln_err(int *sock, char *buf, int bufsize, int *rc)
+int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
 {
        int i, retval;
        CitContext *CCC = MyContext();
        const char *pCh;
 
        FlushStrBuf(CCC->sMigrateBuf);
-       *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf);
+       *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
 
        i = StrLength(CCC->sMigrateBuf);
        pCh = ChrPtr(CCC->sMigrateBuf);
@@ -311,13 +324,13 @@ int sock_getln_err(int *sock, char *buf, int bufsize, int *rc)
  * client side protocol implementations.  It only returns the first line of
  * a multiline response, discarding the rest.
  */
-int ml_sock_gets(int *sock, char *buf)
+int ml_sock_gets(int *sock, char *buf, int nSec)
 {
        int rc = 0;
        char bigbuf[1024];
        int g;
 
-       g = sock_getln_err(sock, buf, SIZ, &rc);
+       g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
        if (rc < 0)
                return rc;
        if (g < 4)
@@ -326,7 +339,7 @@ int ml_sock_gets(int *sock, char *buf)
                return (g);
 
        do {
-               g = sock_getln_err(sock, bigbuf, SIZ, &rc);
+               g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
                if (rc < 0)
                        return rc;
                if (g < 0)