[FFmpeg-devel] [PATCH 3/3] Non-blocking protocols: TCP

Nicolas George nicolas.george
Sat Jan 29 21:03:59 CET 2011


---
 libavformat/tcp.c |   68 +++++++++++++++++++---------------------------------
 1 files changed, 25 insertions(+), 43 deletions(-)

diff --git a/libavformat/tcp.c b/libavformat/tcp.c
index ac4e4b0..f204bfd 100644
--- a/libavformat/tcp.c
+++ b/libavformat/tcp.c
@@ -129,59 +129,41 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
     return ret;
 }
 
+static int tcp_wait_fd(int fd, int write)
+{
+    struct pollfd p = { .fd = fd, .events = POLLOUT, .revents = 0 };
+    int ret;
+
+    ret = poll(&p, 1, 100);
+    return ret < 0 ? ff_neterrno() : 0;
+}
+
 static int tcp_read(URLContext *h, uint8_t *buf, int size)
 {
     TCPContext *s = h->priv_data;
-    struct pollfd p = {s->fd, POLLIN, 0};
-    int len, ret;
-
-    for (;;) {
-        if (url_interrupt_cb())
-            return AVERROR(EINTR);
-        ret = poll(&p, 1, 100);
-        if (ret == 1 && p.revents & POLLIN) {
-            len = recv(s->fd, buf, size, 0);
-            if (len < 0) {
-                if (ff_neterrno() != FF_NETERROR(EINTR) &&
-                    ff_neterrno() != FF_NETERROR(EAGAIN))
-                    return ff_neterrno();
-            } else return len;
-        } else if (ret < 0) {
-            if (ff_neterrno() == FF_NETERROR(EINTR))
-                continue;
-            return -1;
-        }
+    int ret;
+
+    if (!(h->flags & URL_FLAG_NONBLOCK)) {
+        ret = tcp_wait_fd(s->fd, 0);
+        if (ret < 0)
+            return ret;
     }
+    ret = recv(s->fd, buf, size, 0);
+    return ret < 0 ? ff_neterrno() : ret;
 }
 
 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
 {
     TCPContext *s = h->priv_data;
-    int ret, size1, len;
-    struct pollfd p = {s->fd, POLLOUT, 0};
-
-    size1 = size;
-    while (size > 0) {
-        if (url_interrupt_cb())
-            return AVERROR(EINTR);
-        ret = poll(&p, 1, 100);
-        if (ret == 1 && p.revents & POLLOUT) {
-            len = send(s->fd, buf, size, 0);
-            if (len < 0) {
-                if (ff_neterrno() != FF_NETERROR(EINTR) &&
-                    ff_neterrno() != FF_NETERROR(EAGAIN))
-                    return ff_neterrno();
-                continue;
-            }
-            size -= len;
-            buf += len;
-        } else if (ret < 0) {
-            if (ff_neterrno() == FF_NETERROR(EINTR))
-                continue;
-            return -1;
-        }
+    int ret;
+
+    if (!(h->flags & URL_FLAG_NONBLOCK)) {
+        ret = tcp_wait_fd(s->fd, 1);
+        if (ret < 0)
+            return ret;
     }
-    return size1 - size;
+    ret = send(s->fd, buf, size, 0);
+    return ret < 0 ? ff_neterrno() : ret;
 }
 
 static int tcp_close(URLContext *h)
-- 
1.7.2.3




More information about the ffmpeg-devel mailing list