diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2013-06-05 11:56:56 +0200 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-06-05 11:56:56 +0200 |
commit | 82070b01b85f200ec1a76064159f82626c167c12 (patch) | |
tree | 0c7b43a067e268ebfd3207281b0ecc47816888d8 | |
parent | 606e8baf0fec23140f2a1baffb8752cb761039f5 (diff) | |
parent | 9835abb6d63fb07613994ae90e72fef758149408 (diff) | |
download | ffmpeg-82070b01b85f200ec1a76064159f82626c167c12.tar.gz |
Merge commit '9835abb6d63fb07613994ae90e72fef758149408'
* commit '9835abb6d63fb07613994ae90e72fef758149408':
network: uniform ff_listen_bind and ff_listen_connect
Conflicts:
libavformat/network.c
libavformat/tcp.c
Merged-by: Michael Niedermayer <michaelni@gmx.at>
-rw-r--r-- | libavformat/network.c | 44 | ||||
-rw-r--r-- | libavformat/network.h | 31 | ||||
-rw-r--r-- | libavformat/tcp.c | 4 |
3 files changed, 61 insertions, 18 deletions
diff --git a/libavformat/network.c b/libavformat/network.c index 7d1a29f3e5..828c1ec47d 100644 --- a/libavformat/network.c +++ b/libavformat/network.c @@ -214,8 +214,29 @@ int ff_is_multicast_address(struct sockaddr *addr) return 0; } +static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout, + AVIOInterruptCB *cb) +{ + int runs = timeout / POLLING_TIME; + int ret = 0; + + do { + if (ff_check_interrupt(cb)) + return AVERROR_EXIT; + ret = poll(p, nfds, POLLING_TIME); + if (ret != 0) + break; + } while (timeout <= 0 || runs-- > 0); + + if (!ret) + return AVERROR(ETIMEDOUT); + if (ret < 0) + return AVERROR(errno); + return ret; +} + int ff_listen_bind(int fd, const struct sockaddr *addr, - socklen_t addrlen, int timeout) + socklen_t addrlen, int timeout, URLContext *h) { int ret; int reuse = 1; @@ -231,9 +252,9 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, if (ret) return ff_neterrno(); - ret = poll(&lp, 1, timeout >= 0 ? timeout : -1); - if (ret <= 0) - return AVERROR(ETIMEDOUT); + ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback); + if (ret < 0) + return ret; ret = accept(fd, NULL, NULL); if (ret < 0) @@ -246,7 +267,7 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, } int ff_listen_connect(int fd, const struct sockaddr *addr, - socklen_t addrlen, int rw_timeout, URLContext *h) + socklen_t addrlen, int timeout, URLContext *h) { struct pollfd p = {fd, POLLOUT, 0}; int64_t wait_started; @@ -264,16 +285,9 @@ int ff_listen_connect(int fd, const struct sockaddr *addr, continue; case AVERROR(EINPROGRESS): case AVERROR(EAGAIN): - wait_started = av_gettime(); - do { - if (ff_check_interrupt(&h->interrupt_callback)) - return AVERROR_EXIT; - ret = poll(&p, 1, 100); - if (ret > 0) - break; - } while (!rw_timeout || (av_gettime() - wait_started < rw_timeout)); - if (ret <= 0) - return AVERROR(ETIMEDOUT); + ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback); + if (ret < 0) + return ret; optlen = sizeof(ret); if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen)) ret = AVUNERROR(ff_neterrno()); diff --git a/libavformat/network.h b/libavformat/network.h index c6264fdc5f..3798f97829 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -223,9 +223,38 @@ const char *ff_gai_strerror(int ecode); int ff_is_multicast_address(struct sockaddr *addr); +#define POLLING_TIME 100 /// Time in milliseconds between interrupt check + +/** + * Bind to a file descriptor and poll for a connection. + * + * @param fd First argument of bind(). + * @param addr Second argument of bind(). + * @param addrlen Third argument of bind(). + * @param timeout Polling timeout in milliseconds. + * @param h URLContext providing interrupt check + * callback and logging context. + * @return A non-blocking file descriptor on success + * or an AVERROR on failure. + */ int ff_listen_bind(int fd, const struct sockaddr *addr, - socklen_t addrlen, int timeout); + socklen_t addrlen, int timeout, + URLContext *h); + +/** + * Connect to a file descriptor and poll for result. + * + * @param fd First argument of connect(), + * will be set as non-blocking. + * @param addr Second argument of connect(). + * @param addrlen Third argument of connect(). + * @param timeout Polling timeout in milliseconds. + * @param h URLContext providing interrupt check + * callback and logging context. + * @return 0 on success, AVERROR on failure. + */ int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h); + #endif /* AVFORMAT_NETWORK_H */ diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 1442f90477..22fc8ce1ea 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -115,13 +115,13 @@ static int tcp_open(URLContext *h, const char *uri, int flags) if (s->listen) { if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, - s->listen_timeout)) < 0) { + s->listen_timeout, h)) < 0) { ret = fd; goto fail1; } } else { if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, - h->rw_timeout, h)) < 0) { + h->rw_timeout / 1000, h)) < 0) { if (ret == AVERROR_EXIT) goto fail1; |