#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
+#include <netinet/tcp.h>
#include <sys/ioctl.h>
uint64_t time_to_wait_to = get_absolute_time_in_ns();
;
time_to_wait_to = time_to_wait_to + wait_time;
+
+ int flags = 1;
+ if (setsockopt(conn->fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags))) {
+ debug(1,"can't enable keepalive checking on the RTSP socket");
+ }
// remaining_time will be zero if wait_time is zero
if (wait_time != 0) {
// Note: the socket will be closed when the thread exits
goto shutdown;
}
+
+ // An ETIMEDOUT error usually means keepalive has failed.
if (nread < 0) {
if (errno == EINTR)
} else {
size_of_reply = sizeof(SOCKADDR);
if (getsockname(conn->fd, (struct sockaddr *)&conn->local, &size_of_reply) == 0) {
+
+ // Thanks to https://holmeshe.me/network-essentials-setsockopt-SO_KEEPALIVE/ for this.
+
+ // turn on keepalive stuff -- wait for keepidle + (keepcnt * keepinttvl time) seconds before giving up
+ // an ETIMEOUT error is returned if the keepalive check fails
+
+ int flags = 35; // wait this many seconds before checking for a dropped client
+ if (setsockopt(conn->fd, SOL_TCP, TCP_KEEPIDLE, (void *)&flags, sizeof(flags))) {
+ debug(1,"can't set the keepidle wait time");
+ }
+
+ flags = 5; // check this many times
+ if (setsockopt(conn->fd, SOL_TCP, TCP_KEEPCNT, (void *)&flags, sizeof(flags))) {
+ debug(1,"can't set the keepidle missing count");
+ }
+
+ flags = 5; // wait this many seconds between checks
+ if (setsockopt(conn->fd, SOL_TCP, TCP_KEEPINTVL, (void *)&flags, sizeof(flags))) {
+ debug(1,"can't set the keepidle missing count interval");
+ exit(0);
+ };
+
// initialise the connection info
void *client_addr = NULL, *self_addr = NULL;
conn->connection_ip_family = conn->local.SAFAMILY;