Skip to content

Commit

Permalink
Fix an error in non_blocking_write, expose a more general version of …
Browse files Browse the repository at this point in the history
…it, use it when writing an RTSP response with a three-second timeout.
  • Loading branch information
mikebrady committed Feb 24, 2019
1 parent 623b9b4 commit ca325d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
13 changes: 9 additions & 4 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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:https://coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring#comment-722
*/
Expand Down
2 changes: 2 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,15 @@ 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));
debug(1, "msg_write_response error %d: \"%s\".", errno, (char *)errorstring);
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;
Expand Down

0 comments on commit ca325d0

Please sign in to comment.