redo:
ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
if (ret < 0) {
+ int timeout=50;
struct pollfd p = {fd, POLLOUT, 0};
- if (ff_neterrno() == FF_NETERROR(EINTR)) {
- if (url_interrupt_cb())
+ if (ff_neterrno() == AVERROR(EINTR)) {
+ if (url_interrupt_cb()) {
+ ret = AVERROR_EXIT;
goto fail1;
+ }
goto redo;
}
- if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
- ff_neterrno() != FF_NETERROR(EAGAIN))
+ if (ff_neterrno() != AVERROR(EINPROGRESS) &&
+ ff_neterrno() != AVERROR(EAGAIN))
goto fail;
/* wait until we are connected or until abort */
for(;;) {
if (url_interrupt_cb()) {
- ret = AVERROR(EINTR);
+ ret = AVERROR_EXIT;
goto fail1;
}
ret = poll(&p, 1, 100);
if (ret > 0)
break;
+ if(!--timeout){
+ av_log(NULL, AV_LOG_ERROR,
+ "TCP open %s:%d timeout\n",
+ hostname, port);
+ goto fail;
+ }
}
/* test error */
return ret;
}
+static int tcp_wait_fd(int fd, int write)
+{
+ int ev = write ? POLLOUT : POLLIN;
+ struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
+ int ret;
+
+ ret = poll(&p, 1, 100);
+ return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
+}
+
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)