From 68b7b534be5912dbd28283d0d566a2ee88e51f9b Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Sat, 6 Jul 2013 01:33:19 +0200 Subject: [PATCH] tcp: Use a default timeout of 5 sec for opening a connection but not for receiving packets This should be closer to how tcp behaved longer ago and should fix the issue with idle connections timing out. Signed-off-by: Michael Niedermayer --- libavformat/tcp.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 61b7d688a5..bf51701437 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -34,6 +34,7 @@ typedef struct TCPContext { const AVClass *class; int fd; int listen; + int open_timeout; int rw_timeout; int listen_timeout; } TCPContext; @@ -43,7 +44,7 @@ typedef struct TCPContext { #define E AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { {"listen", "listen on port instead of connecting", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E }, -{"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = 5000000}, 0, INT_MAX, D|E }, +{"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E }, {"listen_timeout", "connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E }, {NULL} }; @@ -66,6 +67,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags) int ret; char hostname[1024],proto[1024],path[1024]; char portstr[10]; + s->open_timeout = 5000000; av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port, path, sizeof(path), uri); @@ -86,7 +88,10 @@ static int tcp_open(URLContext *h, const char *uri, int flags) s->listen_timeout = strtol(buf, NULL, 10); } } - h->rw_timeout = s->rw_timeout; + if (s->rw_timeout >= 0) { + s->open_timeout = + h->rw_timeout = s->rw_timeout; + } hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf(portstr, sizeof(portstr), "%d", port); @@ -120,7 +125,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags) } } else { if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, - h->rw_timeout / 1000, h)) < 0) { + s->open_timeout / 1000, h)) < 0) { if (ret == AVERROR_EXIT) goto fail1;