Browse code

network: factor out connect-listening code

Introduce ff_listen_connect, to be shared with the other
non-tcp network protocols.

Luca Barbato authored on 2013/05/31 10:05:13
Showing 3 changed files
... ...
@@ -19,6 +19,7 @@
19 19
  */
20 20
 
21 21
 #include "network.h"
22
+#include "url.h"
22 23
 #include "libavcodec/internal.h"
23 24
 #include "libavutil/mem.h"
24 25
 
... ...
@@ -216,3 +217,48 @@ int ff_listen_bind(int fd, const struct sockaddr *addr,
216 216
     ff_socket_nonblock(ret, 1);
217 217
     return ret;
218 218
 }
219
+
220
+int ff_listen_connect(int fd, const struct sockaddr *addr,
221
+                      socklen_t addrlen, int timeout, URLContext *h)
222
+{
223
+    struct pollfd p = {fd, POLLOUT, 0};
224
+    int ret;
225
+    socklen_t optlen;
226
+
227
+    ff_socket_nonblock(fd, 1);
228
+
229
+    while ((ret = connect(fd, addr, addrlen))) {
230
+        ret = ff_neterrno();
231
+        switch (ret) {
232
+        case AVERROR(EINTR):
233
+            if (ff_check_interrupt(&h->interrupt_callback))
234
+                return AVERROR_EXIT;
235
+            continue;
236
+        case AVERROR(EINPROGRESS):
237
+        case AVERROR(EAGAIN):
238
+            while (timeout--) {
239
+                if (ff_check_interrupt(&h->interrupt_callback))
240
+                    return AVERROR_EXIT;
241
+                ret = poll(&p, 1, 100);
242
+                if (ret > 0)
243
+                    break;
244
+            }
245
+            if (ret <= 0)
246
+                return AVERROR(ETIMEDOUT);
247
+            optlen = sizeof(ret);
248
+            if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
249
+                ret = AVUNERROR(ff_neterrno());
250
+            if (ret != 0) {
251
+                char errbuf[100];
252
+                ret = AVERROR(ret);
253
+                av_strerror(ret, errbuf, sizeof(errbuf));
254
+                av_log(h, AV_LOG_ERROR,
255
+                       "Connection to %s failed: %s\n",
256
+                       h->filename, errbuf);
257
+            }
258
+        default:
259
+            return ret;
260
+        }
261
+    }
262
+    return ret;
263
+}
... ...
@@ -27,6 +27,7 @@
27 27
 #include "config.h"
28 28
 #include "libavutil/error.h"
29 29
 #include "os_support.h"
30
+#include "url.h"
30 31
 
31 32
 #if HAVE_UNISTD_H
32 33
 #include <unistd.h>
... ...
@@ -211,5 +212,7 @@ int ff_is_multicast_address(struct sockaddr *addr);
211 211
 
212 212
 int ff_listen_bind(int fd, const struct sockaddr *addr,
213 213
                    socklen_t addrlen, int timeout);
214
-
214
+int ff_listen_connect(int fd, const struct sockaddr *addr,
215
+                      socklen_t addrlen, int timeout,
216
+                      URLContext *h);
215 217
 #endif /* AVFORMAT_NETWORK_H */
... ...
@@ -42,7 +42,6 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
42 42
     const char *p;
43 43
     char buf[256];
44 44
     int ret;
45
-    socklen_t optlen;
46 45
     int timeout = 100, listen_timeout = -1;
47 46
     char hostname[1024],proto[1024],path[1024];
48 47
     char portstr[10];
... ...
@@ -98,53 +97,16 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
98 98
             goto fail1;
99 99
         }
100 100
     } else {
101
- redo:
102
-        ff_socket_nonblock(fd, 1);
103
-        ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
104
-    }
101
+        if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
102
+                                     timeout, h)) < 0) {
105 103
 
106
-    if (ret < 0) {
107
-        struct pollfd p = {fd, POLLOUT, 0};
108
-        ret = ff_neterrno();
109
-        if (ret == AVERROR(EINTR)) {
110
-            if (ff_check_interrupt(&h->interrupt_callback)) {
111
-                ret = AVERROR_EXIT;
112
-                goto fail1;
113
-            }
114
-            goto redo;
115
-        }
116
-        if (ret != AVERROR(EINPROGRESS) &&
117
-            ret != AVERROR(EAGAIN))
118
-            goto fail;
119
-
120
-        /* wait until we are connected or until abort */
121
-        while(timeout--) {
122
-            if (ff_check_interrupt(&h->interrupt_callback)) {
123
-                ret = AVERROR_EXIT;
104
+            if (ret == AVERROR_EXIT)
124 105
                 goto fail1;
125
-            }
126
-            ret = poll(&p, 1, 100);
127
-            if (ret > 0)
128
-                break;
129
-        }
130
-        if (ret <= 0) {
131
-            ret = AVERROR(ETIMEDOUT);
132
-            goto fail;
133
-        }
134
-        /* test error */
135
-        optlen = sizeof(ret);
136
-        if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
137
-            ret = AVUNERROR(ff_neterrno());
138
-        if (ret != 0) {
139
-            char errbuf[100];
140
-            ret = AVERROR(ret);
141
-            av_strerror(ret, errbuf, sizeof(errbuf));
142
-            av_log(h, AV_LOG_ERROR,
143
-                   "TCP connection to %s:%d failed: %s\n",
144
-                   hostname, port, errbuf);
145
-            goto fail;
106
+            else
107
+                goto fail;
146 108
         }
147 109
     }
110
+
148 111
     h->is_streamed = 1;
149 112
     s->fd = fd;
150 113
     freeaddrinfo(ai);