handling replies continuously from server
[oweals/gnunet.git] / src / util / server_mst.c
index adb0a0818eb4a3df3c16fc307ff58fa17e5803f9..1523de91a96c5d509fbd0addc75a25f697326f1b 100644 (file)
@@ -31,7 +31,6 @@
 #include "gnunet_server_lib.h"
 #include "gnunet_time_lib.h"
 
-#define DEBUG_SERVER_MST GNUNET_EXTRA_LOGGING
 
 #if HAVE_UNALIGNED_64_ACCESS
 #define ALIGN_FACTOR 4
@@ -39,6 +38,8 @@
 #define ALIGN_FACTOR 8
 #endif
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
 
 /**
  * Handle to a message stream tokenizer.
@@ -130,16 +131,17 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
   unsigned long offset;
   int ret;
 
-#if DEBUG_SERVER_MST
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Server-mst receives %u bytes with %u bytes already in private buffer\n",
-              (unsigned int) size, (unsigned int) (mst->pos - mst->off));
-#endif
+  GNUNET_assert (mst->off <= mst->pos);
+  GNUNET_assert (mst->pos <= mst->curr_buf);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Server-mst receives %u bytes with %u bytes already in private buffer\n",
+       (unsigned int) size, (unsigned int) (mst->pos - mst->off));
   ret = GNUNET_OK;
   ibuf = (char *) mst->hdr;
   while (mst->pos > 0)
   {
 do_align:
+    GNUNET_assert (mst->pos >= mst->off);
     if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
         (0 != (mst->off % ALIGN_FACTOR)))
     {
@@ -174,15 +176,18 @@ do_align:
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
-    if (mst->curr_buf - mst->off < want)
+    if ( (mst->curr_buf - mst->off < want) &&
+        (mst->off > 0) )
     {
-      /* need more space */
+      /* can get more space by moving */
       mst->pos -= mst->off;
       memmove (ibuf, &ibuf[mst->off], mst->pos);
       mst->off = 0;
     }
-    if (want > mst->curr_buf)
+    if (mst->curr_buf < want)
     {
+      /* need to get more space by growing buffer */
+      GNUNET_assert (0 == mst->off);
       mst->hdr = GNUNET_realloc (mst->hdr, want);
       ibuf = (char *) mst->hdr;
       mst->curr_buf = want;
@@ -191,6 +196,7 @@ do_align:
     if (mst->pos - mst->off < want)
     {
       delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
+      GNUNET_assert (mst->pos + delta <= mst->curr_buf);
       memcpy (&ibuf[mst->pos], buf, delta);
       mst->pos += delta;
       buf += delta;
@@ -214,8 +220,8 @@ do_align:
     }
     if (one_shot == GNUNET_YES)
       one_shot = GNUNET_SYSERR;
-    mst->cb (mst->cb_cls, client_identity, hdr);
     mst->off += want;
+    mst->cb (mst->cb_cls, client_identity, hdr);
     if (mst->off == mst->pos)
     {
       /* reset to beginning of buffer, it's free right now! */
@@ -223,17 +229,16 @@ do_align:
       mst->pos = 0;
     }
   }
+  GNUNET_assert (0 == mst->pos);
   while (size > 0)
   {
-#if DEBUG_SERVER_MST
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Server-mst has %u bytes left in inbound buffer\n",
-                (unsigned int) size);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Server-mst has %u bytes left in inbound buffer\n",
+         (unsigned int) size);
     if (size < sizeof (struct GNUNET_MessageHeader))
       break;
     offset = (unsigned long) buf;
-    need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
+    need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
     if (GNUNET_NO == need_align)
     {
       /* can try to do zero-copy and process directly from original buffer */
@@ -246,7 +251,7 @@ do_align:
         return GNUNET_SYSERR;
       }
       if (size < want)
-        break;                  /* or not, buffer incomplete, so copy to private buffer... */
+        break;                  /* or not: buffer incomplete, so copy to private buffer... */
       if (one_shot == GNUNET_SYSERR)
       {
         /* cannot call callback again, but return value saying that
@@ -276,17 +281,18 @@ copy:
       ibuf = (char *) mst->hdr;
       mst->curr_buf = size + mst->pos;
     }
-    GNUNET_assert (mst->pos + size <= mst->curr_buf);
+    GNUNET_assert (size + mst->pos <= mst->curr_buf);
     memcpy (&ibuf[mst->pos], buf, size);
     mst->pos += size;
   }
   if (purge)
+  {
     mst->off = 0;
-#if DEBUG_SERVER_MST
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Server-mst leaves %u bytes in private buffer\n",
-              (unsigned int) (mst->pos - mst->off));
-#endif
+    mst->pos = 0;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Server-mst leaves %u bytes in private buffer\n",
+       (unsigned int) (mst->pos - mst->off));
   return ret;
 }