[FFmpeg-devel] [PATCH 1/6] tcp: use timeout option consistently
Guillaume LECERF
glecerf at gmail.com
Thu Sep 13 21:13:57 CEST 2012
2012/9/13 Andrey Utkin <andrey.krieger.utkin at gmail.com>:
> In this edition, av_gettime() is called only if socket buffer is empty (in case of read) or full (in case of write)
> @@ -192,9 +195,22 @@ static int tcp_read(URLContext *h, uint8_t *buf, int size)
> int ret;
>
> if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
> - ret = ff_network_wait_fd(s->fd, 0);
> - if (ret < 0)
> - return ret;
> + int64_t wait_start = 0;
> + while (1) {
> + ret = ff_network_wait_fd(s->fd, 0);
> + if (!ret)
> + break;
> + if (ret != AVERROR(EAGAIN))
> + return ret;
> + if (ff_check_interrupt(&h->interrupt_callback))
> + return AVERROR_EXIT;
> + if (h->rw_timeout) {
> + if (!wait_start)
> + wait_start = av_gettime();
> + else if (av_gettime() - wait_start > h->rw_timeout)
> + return AVERROR(ETIMEDOUT);
> + }
> + }
> }
> ret = recv(s->fd, buf, size, 0);
> return ret < 0 ? ff_neterrno() : ret;
> @@ -206,9 +222,22 @@ static int tcp_write(URLContext *h, const uint8_t *buf, int size)
> int ret;
>
> if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
> - ret = ff_network_wait_fd(s->fd, 1);
> - if (ret < 0)
> - return ret;
> + int64_t wait_start = 0;
> + while (1) {
> + ret = ff_network_wait_fd(s->fd, 1);
> + if (!ret)
> + break;
> + if (ret != AVERROR(EAGAIN))
> + return ret;
> + if (ff_check_interrupt(&h->interrupt_callback))
> + return AVERROR_EXIT;
> + if (h->rw_timeout) {
> + if (!wait_start)
> + wait_start = av_gettime();
> + else if (av_gettime() - wait_start > h->rw_timeout)
> + return AVERROR(ETIMEDOUT);
> + }
> + }
> }
> ret = send(s->fd, buf, size, 0);
> return ret < 0 ? ff_neterrno() : ret;
I think it deserves a function to avoid such code duplication.
Just my 0.02$ ;)
--
Guillaume LECERF
OpenBricks developer - www.openbricks.org
More information about the ffmpeg-devel
mailing list