Fix an error in non_blocking_write, expose a more general version of it, use it when writing an RTSP response with a three-second timeout.

This commit is contained in:
Mike Brady
2019-02-24 13:16:55 +00:00
parent 623b9b4b9f
commit ca325d0459
3 changed files with 13 additions and 6 deletions
+9 -4
View File
@@ -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
*/
+2
View File
@@ -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
+2 -2
View File
@@ -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;