aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/network.c
diff options
context:
space:
mode:
authorLuca Barbato <lu_zero@gentoo.org>2013-05-31 03:05:13 +0200
committerLuca Barbato <lu_zero@gentoo.org>2013-06-01 15:29:53 +0200
commitf849a77e67959eb6a83eb59b784aeefdb98cb80a (patch)
tree8c3ac49ace6c1e03463206860b1484332818108d /libavformat/network.c
parent28306e6d620c109ddd672f7243adfbc2bbb3b18f (diff)
downloadffmpeg-f849a77e67959eb6a83eb59b784aeefdb98cb80a.tar.gz
network: factor out connect-listening code
Introduce ff_listen_connect, to be shared with the other non-tcp network protocols.
Diffstat (limited to 'libavformat/network.c')
-rw-r--r--libavformat/network.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/libavformat/network.c b/libavformat/network.c
index 55d55af35d..6f43c41626 100644
--- a/libavformat/network.c
+++ b/libavformat/network.c
@@ -19,6 +19,7 @@
*/
#include "network.h"
+#include "url.h"
#include "libavcodec/internal.h"
#include "libavutil/mem.h"
@@ -216,3 +217,48 @@ int ff_listen_bind(int fd, const struct sockaddr *addr,
ff_socket_nonblock(ret, 1);
return ret;
}
+
+int ff_listen_connect(int fd, const struct sockaddr *addr,
+ socklen_t addrlen, int timeout, URLContext *h)
+{
+ struct pollfd p = {fd, POLLOUT, 0};
+ int ret;
+ socklen_t optlen;
+
+ ff_socket_nonblock(fd, 1);
+
+ while ((ret = connect(fd, addr, addrlen))) {
+ ret = ff_neterrno();
+ switch (ret) {
+ case AVERROR(EINTR):
+ if (ff_check_interrupt(&h->interrupt_callback))
+ return AVERROR_EXIT;
+ continue;
+ case AVERROR(EINPROGRESS):
+ case AVERROR(EAGAIN):
+ while (timeout--) {
+ if (ff_check_interrupt(&h->interrupt_callback))
+ return AVERROR_EXIT;
+ ret = poll(&p, 1, 100);
+ if (ret > 0)
+ break;
+ }
+ if (ret <= 0)
+ return AVERROR(ETIMEDOUT);
+ optlen = sizeof(ret);
+ if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
+ ret = AVUNERROR(ff_neterrno());
+ if (ret != 0) {
+ char errbuf[100];
+ ret = AVERROR(ret);
+ av_strerror(ret, errbuf, sizeof(errbuf));
+ av_log(h, AV_LOG_ERROR,
+ "Connection to %s failed: %s\n",
+ h->filename, errbuf);
+ }
+ default:
+ return ret;
+ }
+ }
+ return ret;
+}