changeset 360:b465a17ffa47

Finally fix handling of long packets.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jun 2011 10:37:11 +0300
parents 8f3c102db611
children b3556ff686fc
files libnnchat.c libnnchat.h nnchat.c
diffstat 3 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Thu Jun 23 10:11:45 2011 +0300
+++ b/libnnchat.c	Thu Jun 23 10:37:11 2011 +0300
@@ -183,7 +183,7 @@
 
     FD_ZERO(&(conn->sockfds));
     FD_SET(conn->socket, &(conn->sockfds));
-
+    
     /* Proxy-specific setup */
     if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) {
         struct nn_socks_t *socksh;
@@ -221,6 +221,7 @@
         }
         
         /* Send request */
+        nn_conn_reset(conn);
         if (!nn_conn_send_buf(conn, buf, bufsiz)) {
             th_free(buf);
             nn_conn_err(conn, "Error sending SOCKS proxy request.\n");
@@ -231,6 +232,7 @@
         /* Wait for SOCKS server to reply */
         for (status = tries = 1; tries <= 20 && status > 0; tries++) {
             usleep(500);
+            nn_conn_reset(conn);
             status = nn_conn_pull(conn);
         }
         
@@ -264,6 +266,7 @@
         }
     }
 
+    nn_conn_reset(conn);
     conn->status = NN_CONN_OPEN;
     return 0;
 
@@ -316,6 +319,13 @@
     return TRUE;
 }
 
+void nn_conn_reset(nn_conn_t *conn)
+{
+    if (conn != NULL) {
+        conn->ptr = conn->buf;
+        conn->got = conn->total = 0;
+    }
+}
 
 int nn_conn_pull(nn_conn_t *conn)
 {
@@ -326,8 +336,6 @@
     if (conn == NULL)
         return -10;
 
-    conn->ptr = conn->buf;
-
     /* Check for incoming data */
     socktv.tv_sec = 0;
     socktv.tv_usec = NN_DELAY_USEC;
@@ -342,7 +350,7 @@
         }
     } else
     if (FD_ISSET(conn->socket, &tmpfds)) {
-        conn->got = recv(conn->socket, conn->buf, NN_CONNBUF_SIZE, 0);
+        conn->got = recv(conn->socket, conn->ptr, NN_CONNBUF_SIZE - conn->total, 0);
         if (conn->got < 0) {
             conn->err = nn_get_socket_errno();
             nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err));
@@ -353,7 +361,8 @@
             return -3;
         } else {
             /* Handle protocol data */
-            conn->buf[conn->got] = 0;
+            conn->total += conn->got;
+            conn->ptr += conn->got;
             return 0;
         }
     }
--- a/libnnchat.h	Thu Jun 23 10:11:45 2011 +0300
+++ b/libnnchat.h	Thu Jun 23 10:37:11 2011 +0300
@@ -91,7 +91,7 @@
     
     char buf[NN_CONNBUF_SIZE + 16];
     char *ptr;
-    ssize_t got;
+    ssize_t got, total;
 } nn_conn_t;
 
 
@@ -107,6 +107,7 @@
 int         nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host);
 int         nn_conn_open(nn_conn_t *conn, const int port, const char *host);
 void        nn_conn_close(nn_conn_t *);
+void        nn_conn_reset(nn_conn_t *);
 int         nn_conn_pull(nn_conn_t *);
 BOOL        nn_conn_send_buf(nn_conn_t *, const char *buf, const size_t len);
 BOOL        nn_conn_send_msg(nn_conn_t *, const char *user, const char *fmt, ...);
--- a/nnchat.c	Thu Jun 23 10:11:45 2011 +0300
+++ b/nnchat.c	Thu Jun 23 10:37:11 2011 +0300
@@ -1536,25 +1536,28 @@
     }
 
     /* Enter mainloop */
+    nn_conn_reset(conn);
     while (!isError && !exitProg) {
         int cres = nn_conn_pull(conn);
-        if (cres == 0) {
+        if (cres == 0 && *(conn->ptr - 1) == 0) {
+            char *ptr = conn->buf;
             do {
-                size_t bufLen = strlen(conn->ptr) + 1;
-                int result = handleProtocol(conn, conn->ptr, bufLen);
+                size_t bufLen = strlen(ptr) + 1;
+                int result = handleProtocol(conn, ptr, bufLen);
 
                 if (result > 0) {
                     /* Couldn't handle the message for some reason */
-                    printMsg(currWin, "Could not handle: %s\n", conn->ptr);
+                    printMsg(currWin, "Could not handle: %s\n", ptr);
                 } else if (result < 0) {
                     /* Fatal error, quit */
-                    errorMsg("Fatal error with message: %s\n", conn->ptr);
+                    errorMsg("Fatal error with message: %s\n", ptr);
                     isError = TRUE;
                 }
 
-                conn->got -= bufLen;
-                conn->ptr += bufLen;
-            } while (conn->got > 0 && !isError);
+                conn->total -= bufLen;
+                ptr += bufLen;
+            } while (conn->total > 0 && !isError);
+            nn_conn_reset(conn);
         }
         if (!nn_conn_check(conn))
             isError = TRUE;