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;
// 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) {
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
*/
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
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));
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;