changeset 2567:a9d6ce7b8c68

- Handle situations better where the initial buffer fill hits EOF on the stream
author Ralf Ertzinger <ralf@skytale.net>
date Wed, 14 May 2008 23:39:07 +0200
parents 12aa69e560b2
children 92f6d0503c04
files src/neon/neon.c
diffstat 1 files changed, 44 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/neon/neon.c	Tue May 13 20:10:57 2008 +0200
+++ b/src/neon/neon.c	Wed May 14 23:39:07 2008 +0200
@@ -323,6 +323,10 @@
 
     _ENTER;
 
+    if (NULL == h) {
+        _LEAVE;
+    }
+
     _DEBUG("Signaling reader thread to terminate");
     g_mutex_lock(h->reader_status.mutex);
     h->reader_status.reading = FALSE;
@@ -333,6 +337,8 @@
     g_thread_join(h->reader);
     _DEBUG("Reader thread has died");
     h->reader = NULL;
+
+    _LEAVE;
 }
 
 /*
@@ -774,7 +780,7 @@
             /*
              * EOF while filling the buffer. Return what we have.
              */
-            _LEAVE 0;
+            _LEAVE 1;
         }
 
         bfree = free_rb(&h->rb);
@@ -982,36 +988,45 @@
     }
 
     if (NULL == h->reader) {
-        /*
-         * There is no reader thread yet. Read the first bytes from
-         * the network ourselves, and then fire up the reader thread
-         * to keep the buffer filled up.
-         */
-        _DEBUG("<%p> Doing initial buffer fill", h);
-        ret = fill_buffer_limit(h, NBUFSIZ/2);
+        if (NEON_READER_EOF != h->reader_status.status) {
+            /*
+             * There is no reader thread yet. Read the first bytes from
+             * the network ourselves, and then fire up the reader thread
+             * to keep the buffer filled up.
+             */
+            _DEBUG("<%p> Doing initial buffer fill", h);
+            ret = fill_buffer_limit(h, NBUFSIZ/2);
+
+            if (-1 == ret) {
+                _ERROR("<%p> Error while reading from the network", h);
+                _LEAVE 0;
+            } else if (1 == ret) {
+                _DEBUG("<%p> EOF during initial read", h);
+            }
 
-        if (-1 == ret) {
-            _ERROR("<%p> Error while reading from the network", h);
-            _LEAVE 0;
-        } else if (1 == ret) {
-            _ERROR("<%p> EOF during initial read", h);
-            _LEAVE 0;
+            /*
+             * We have some data in the buffer now.
+             * Start the reader thread if we did not reach EOF during
+             * the initial fill
+             */
+            g_mutex_lock(h->reader_status.mutex);
+            if (0 == ret) {
+                h->reader_status.reading = TRUE;
+                _DEBUG("<%p> Starting reader thread", h);
+                if (NULL == (h->reader = g_thread_create(reader_thread, h, TRUE, NULL))) {
+                    h->reader_status.reading = FALSE;
+                    g_mutex_unlock(h->reader_status.mutex);
+                    _ERROR("<%p> Error creating reader thread!", h);
+                    _LEAVE 0;
+                }
+                h->reader_status.status = NEON_READER_RUN;
+            } else {
+                _DEBUG("<%p> No reader thread needed (stream has reached EOF during fill", h);
+                h->reader_status.reading = FALSE;
+                h->reader_status.status = NEON_READER_EOF;
+            }
+            g_mutex_unlock(h->reader_status.mutex);
         }
-
-        /*
-         * We have some data in the buffer now.
-         * Start the reader thread.
-         */
-        g_mutex_lock(h->reader_status.mutex);
-        h->reader_status.reading = TRUE;
-        if (NULL == (h->reader = g_thread_create(reader_thread, h, TRUE, NULL))) {
-            h->reader_status.reading = FALSE;
-            g_mutex_unlock(h->reader_status.mutex);
-            _ERROR("<%p> Error creating reader thread!", h);
-            _LEAVE 0;
-        }
-        h->reader_status.status = NEON_READER_RUN;
-        g_mutex_unlock(h->reader_status.mutex);
     } else {
         /*
          * There already is a reader thread. Look if it is in good