Revision 51b317d2

View differences:

libavformat/tcp.c
129 129
    return ret;
130 130
}
131 131

  
132
static int tcp_wait_fd(int fd, int write)
133
{
134
    int ev = write ? POLLOUT : POLLIN;
135
    struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
136
    int ret;
137

  
138
    ret = poll(&p, 1, 100);
139
    return ret < 0 ? ff_neterrno() : !!(p.revents & ev);
140
}
141

  
132 142
static int tcp_read(URLContext *h, uint8_t *buf, int size)
133 143
{
134 144
    TCPContext *s = h->priv_data;
135
    struct pollfd p = {s->fd, POLLIN, 0};
136 145
    int len, ret;
137 146

  
138 147
    for (;;) {
139 148
        if (url_interrupt_cb())
140 149
            return AVERROR(EINTR);
141
        ret = poll(&p, 1, 100);
142
        if (ret == 1 && p.revents & POLLIN) {
150
        ret = tcp_wait_fd(s->fd, 0);
151
        if (ret > 0) {
143 152
            len = recv(s->fd, buf, size, 0);
144 153
            if (len < 0) {
145 154
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
......
147 156
                    return ff_neterrno();
148 157
            } else return len;
149 158
        } else if (ret < 0) {
150
            if (ff_neterrno() == FF_NETERROR(EINTR))
159
            if (ret == FF_NETERROR(EINTR))
151 160
                continue;
152
            return -1;
161
            return ret;
153 162
        }
154 163
    }
155 164
}
......
158 167
{
159 168
    TCPContext *s = h->priv_data;
160 169
    int ret, size1, len;
161
    struct pollfd p = {s->fd, POLLOUT, 0};
162 170

  
163 171
    size1 = size;
164 172
    while (size > 0) {
165 173
        if (url_interrupt_cb())
166 174
            return AVERROR(EINTR);
167
        ret = poll(&p, 1, 100);
168
        if (ret == 1 && p.revents & POLLOUT) {
175
        ret = tcp_wait_fd(s->fd, 1);
176
        if (ret > 0) {
169 177
            len = send(s->fd, buf, size, 0);
170 178
            if (len < 0) {
171 179
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
......
176 184
            size -= len;
177 185
            buf += len;
178 186
        } else if (ret < 0) {
179
            if (ff_neterrno() == FF_NETERROR(EINTR))
187
            if (ret == FF_NETERROR(EINTR))
180 188
                continue;
181
            return -1;
189
            return ret;
182 190
        }
183 191
    }
184 192
    return size1 - size;

Also available in: Unified diff