]> git.ipfire.org Git - thirdparty/shairport-sync.git/commitdiff
Fix an error in non_blocking_write, expose a more general version of it, use it when...
authorMike Brady <mikebrady@eircom.net>
Sun, 24 Feb 2019 13:16:55 +0000 (13:16 +0000)
committerMike Brady <mikebrady@eircom.net>
Sun, 24 Feb 2019 13:16:55 +0000 (13:16 +0000)
common.c
common.h
rtsp.c

index cf5c0730938de3edb4749137805fdb9a6c2363ec..08b5bed1e0afde1b1d96b47bc96d0c0c5c1bc55b 100644 (file)
--- a/common.c
+++ b/common.c
@@ -955,7 +955,8 @@ uint64_t get_absolute_time_in_fp() {
   return time_now_fp;
 }
 
-ssize_t non_blocking_write(int fd, const void *buf, size_t count) {
+ssize_t non_blocking_write_with_timeout(int fd, const void *buf, size_t count, int timeout) {
+  // timeout is in milliseconds
   void *ibuf = (void *)buf;
   size_t bytes_remaining = count;
   int rc = 1;
@@ -964,7 +965,7 @@ ssize_t non_blocking_write(int fd, const void *buf, size_t count) {
     // check that we can do some writing
     ufds[0].fd = fd;
     ufds[0].events = POLLOUT;
-    rc = poll(ufds, 1, 5000);
+    rc = poll(ufds, 1, timeout);
     if (rc < 0) {
       // debug(1, "non-blocking write error waiting for pipe to become ready for writing...");
     } else if (rc == 0) {
@@ -975,20 +976,24 @@ ssize_t non_blocking_write(int fd, const void *buf, size_t count) {
       ssize_t bytes_written = write(fd, ibuf, bytes_remaining);
       if (bytes_written == -1) {
         // debug(1,"Error %d in non_blocking_write: \"%s\".",errno,strerror(errno));
-        rc = -1;
+        rc = bytes_written; // to imitate the return from write()
       } else {
         ibuf += bytes_written;
         bytes_remaining -= bytes_written;
       }
     }
   }
-  if (rc == 0)
+  if (rc > 0)
     return count - bytes_remaining; // this is just to mimic a normal write/3.
   else
     return rc;
   //  return write(fd,buf,count);
 }
 
+ssize_t non_blocking_write(int fd, const void *buf, size_t count) {
+  return non_blocking_write_with_timeout(fd,buf,count,5000); // default is 5 seconds.
+}
+
 /* from
  * http://coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring#comment-722
  */
index e3e02635b4e77215a50f38c446d9ce2d50b95886..9bc849e1e85be53b8fee2244386ae1afd9d4b314 100644 (file)
--- a/common.h
+++ b/common.h
@@ -251,6 +251,8 @@ int get_requested_connection_state_to_output();
 
 void set_requested_connection_state_to_output(int v);
 
+ssize_t non_blocking_write_with_timeout(int fd, const void *buf, size_t count, int timeout); // timeout in milliseconds
+
 ssize_t non_blocking_write(int fd, const void *buf, size_t count); // used in a few places
 
 /* from
diff --git a/rtsp.c b/rtsp.c
index fb4ed4f3d937c2e96bfdc4cff46b296a014c2df7..6279d7a151ae76aac0a69894ffff70b0a960ef99 100644 (file)
--- a/rtsp.c
+++ b/rtsp.c
@@ -779,7 +779,7 @@ int msg_write_response(int fd, rtsp_message *resp) {
     debug(1, "Attempted to write overlong RTSP packet 3");
     return -3;
   }
-  ssize_t reply = write(fd, pkt, p - pkt);
+  ssize_t reply = non_blocking_write_with_timeout(fd, pkt, p - pkt, 3000); // wait three seconds (3,000 milliseconds) for it to become available
   if (reply == -1) {
     char errorstring[1024];
     strerror_r(errno, (char *)errorstring, sizeof(errorstring));
@@ -787,7 +787,7 @@ int msg_write_response(int fd, rtsp_message *resp) {
     return -4;
   }
   if (reply != p - pkt) {
-    debug(1, "msg_write_response error -- requested bytes not fully written.");
+    debug(1, "msg_write_response error -- requested bytes: %d not fully written: %d.",p - pkt, reply);
     return -5;
   }
   return 0;