Browse code

Introduce ff_network_wait_fd_timeout()

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Andrey Utkin authored on 2012/10/09 21:26:02
Showing 2 changed files
... ...
@@ -22,6 +22,8 @@
22 22
 #include "network.h"
23 23
 #include "libavcodec/internal.h"
24 24
 #include "libavutil/mem.h"
25
+#include "url.h"
26
+#include "libavutil/time.h"
25 27
 
26 28
 #define THREADS (HAVE_PTHREADS || (defined(WIN32) && !defined(__MINGW32CE__)))
27 29
 
... ...
@@ -150,6 +152,26 @@ int ff_network_wait_fd(int fd, int write)
150 150
     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
151 151
 }
152 152
 
153
+int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
154
+{
155
+    int ret;
156
+    int64_t wait_start = 0;
157
+
158
+    while (1) {
159
+        ret = ff_network_wait_fd(fd, write);
160
+        if (ret != AVERROR(EAGAIN))
161
+            return ret;
162
+        if (ff_check_interrupt(int_cb))
163
+            return AVERROR_EXIT;
164
+        if (timeout) {
165
+            if (!wait_start)
166
+                wait_start = av_gettime();
167
+            else if (av_gettime() - wait_start > timeout)
168
+                return AVERROR(ETIMEDOUT);
169
+        }
170
+    }
171
+}
172
+
153 173
 void ff_network_close(void)
154 174
 {
155 175
 #if HAVE_WINSOCK2_H
... ...
@@ -26,6 +26,7 @@
26 26
 #include "config.h"
27 27
 #include "libavutil/error.h"
28 28
 #include "os_support.h"
29
+#include "avio.h"
29 30
 
30 31
 #if HAVE_UNISTD_H
31 32
 #include <unistd.h>
... ...
@@ -80,6 +81,18 @@ void ff_tls_deinit(void);
80 80
 
81 81
 int ff_network_wait_fd(int fd, int write);
82 82
 
83
+/**
84
+ * This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds
85
+ * Uses ff_network_wait_fd in a loop
86
+ *
87
+ * @fd Socket descriptor
88
+ * @write Set 1 to wait for socket able to be read, 0 to be written
89
+ * @timeout Timeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
90
+ * @param int_cb Interrupt callback, is checked after each ff_network_wait_fd call
91
+ * @return 0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code
92
+ */
93
+int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb);
94
+
83 95
 int ff_inet_aton (const char * str, struct in_addr * add);
84 96
 
85 97
 #if !HAVE_STRUCT_SOCKADDR_STORAGE