It would be really nice if we can control somehow the connection timeout on receive and send operation because those calls are blocking. (Eg using libmms with GStreamer).
The following patch reduces the timeout to a fixed value.
diff --git a/src/mms.c b/src/mms.c
index 4af5f0f..f7a7df3 100644
--- a/src/mms.c
+++ b/src/mms.c
@@ -40,6 +40,7 @@
#include <stdio.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
+#include <sys/time.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
@@ -212,6 +213,7 @@ static off_t fallback_io_write(void *data, int socket, char *buf, off_t num)
static int fallback_io_tcp_connect(void *data, const char *host, int port)
{
struct addrinfo *r, *res, hints;
+ struct timeval timeout;
char port_str[16];
int i, s;
@@ -221,6 +223,9 @@ static int fallback_io_tcp_connect(void *data, const char *host, int port)
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
+ timeout.tv_sec = 15;
+ timeout.tv_usec = 0;
+
sprintf(port_str, "%d", port);
i = getaddrinfo(host, port_str, &hints, &res);
if (i != 0) {
@@ -231,6 +236,12 @@ static int fallback_io_tcp_connect(void *data, const char *host, int port)
for (r = res; r != NULL; r = r->ai_next) {
s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
if (s != -1) {
+ if (setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
+ sizeof(timeout)) < 0)
+ lprintf("Setting socket receive timeout to %ds failed!", timeout.tv_sec);
+ if (setsockopt (s, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
+ sizeof(timeout)) < 0)
+ lprintf("Setting socket send timeout to %ds failed!", timeout.tv_sec);
if (connect(s, r->ai_addr, r->ai_addrlen) != -1) {
freeaddrinfo(res);
return s;
Hi,
I agree that having a non blocking API or a way to limit the timeout would be good, but I do not think that having a fixed timeout is a good idea. How about adding with_timeout postfixed variants of the existing connect functions which take a timeout argument. Note that it is important to maintain API and ABI compatibility with existing users.
Regards,
Hans