]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
HTSP: Handle partial write()s
authorAndreas Öman <andreas@lonelycoder.com>
Mon, 5 Nov 2012 12:05:43 +0000 (13:05 +0100)
committerAndreas Öman <andreas@lonelycoder.com>
Mon, 5 Nov 2012 12:05:52 +0000 (13:05 +0100)
src/htsp_server.c

index d1f65f602a8ba6a2adb9d76a529193f1298ed694..8fcbdd415266400e55dcbe4dbb8c278145dcc145 100644 (file)
@@ -1469,24 +1469,30 @@ htsp_write_scheduler(void *aux)
 
     r = htsmsg_binary_serialize(hm->hm_msg, &dptr, &dlen, INT32_MAX);
 
-#if 0
-    if(hm->hm_pktref) {
-      usleep(hm->hm_payloadsize * 3);
-    }
-#endif
     htsp_msg_destroy(hm);
-   
-    /* ignore return value */ 
-    r = write(htsp->htsp_fd, dptr, dlen);
-    if(r != dlen)
-      tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s", 
-            htsp->htsp_logname, strerror(errno));
-    free(dptr);
+
+    void *freeme = dptr;
+
+    while(dlen > 0) {
+      r = write(htsp->htsp_fd, dptr, dlen);
+      if(r < 1) {
+        tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s",
+               htsp->htsp_logname, strerror(errno));
+        break;
+      }
+
+      dptr += r;
+      dlen -= r;
+    }
+
+    free(freeme);
     pthread_mutex_lock(&htsp->htsp_out_mutex);
-    if(r != dlen) 
+    if(dlen)
       break;
   }
+  // Shutdown socket to make receive thread terminate entire HTSP connection
 
+  shutdown(htsp->htsp_fd, SHUT_RDWR);
   pthread_mutex_unlock(&htsp->htsp_out_mutex);
   return NULL;
 }