aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/network.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2013-06-02 10:52:48 +0200
committerMichael Niedermayer <michaelni@gmx.at>2013-06-02 11:02:02 +0200
commit54ddbb477b786be1b90f54bc9aab36cd5babdf7d (patch)
tree6396225eedd6179882e50cd4aa82d877eeb53da0 /libavformat/network.c
parent4d4f5911d3121133929da7b859755860f93684fd (diff)
parentf849a77e67959eb6a83eb59b784aeefdb98cb80a (diff)
downloadffmpeg-54ddbb477b786be1b90f54bc9aab36cd5babdf7d.tar.gz
Merge remote-tracking branch 'qatar/master'
* qatar/master: network: factor out connect-listening code Conflicts: libavformat/network.h libavformat/tcp.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavformat/network.c')
-rw-r--r--libavformat/network.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/libavformat/network.c b/libavformat/network.c
index 454ee13ff1..f80f43ec2a 100644
--- a/libavformat/network.c
+++ b/libavformat/network.c
@@ -20,6 +20,7 @@
#include "libavutil/avutil.h"
#include "network.h"
+#include "url.h"
#include "libavcodec/internal.h"
#include "libavutil/mem.h"
#include "url.h"
@@ -241,3 +242,50 @@ 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 rw_timeout, URLContext *h)
+{
+ struct pollfd p = {fd, POLLOUT, 0};
+ int64_t wait_started;
+ 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):
+ wait_started = av_gettime();
+ do {
+ if (ff_check_interrupt(&h->interrupt_callback))
+ return AVERROR_EXIT;
+ ret = poll(&p, 1, 100);
+ if (ret > 0)
+ break;
+ } while (!rw_timeout || (av_gettime() - wait_started < rw_timeout));
+ 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;
+}