81 | 81 |
size_t space;
|
82 | 82 |
|
83 | 83 |
LOCK;
|
|
84 |
|
84 | 85 |
space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
|
85 | |
UNLOCK;
|
86 | |
|
87 | |
if (fd >= 0 && stream.state > STREAMING_WAIT && space) {
|
|
86 |
|
|
87 |
if (fd < 0 || !space || stream.state <= STREAMING_WAIT) {
|
|
88 |
UNLOCK;
|
|
89 |
usleep(100000);
|
|
90 |
continue;
|
|
91 |
}
|
|
92 |
|
|
93 |
if (stream.state == STREAMING_FILE) {
|
|
94 |
|
|
95 |
int n = read(fd, streambuf->writep, space);
|
|
96 |
if (n == 0) {
|
|
97 |
LOG_INFO("end of stream");
|
|
98 |
_disconnect(DISCONNECT, DISCONNECT_OK);
|
|
99 |
}
|
|
100 |
if (n > 0) {
|
|
101 |
_buf_inc_writep(streambuf, n);
|
|
102 |
stream.bytes += n;
|
|
103 |
LOG_SDEBUG("streambuf read %d bytes", n);
|
|
104 |
}
|
|
105 |
if (n < 0) {
|
|
106 |
LOG_WARN("error reading: %s", strerror(last_error()));
|
|
107 |
_disconnect(DISCONNECT, REMOTE_DISCONNECT);
|
|
108 |
}
|
|
109 |
|
|
110 |
UNLOCK;
|
|
111 |
continue;
|
|
112 |
|
|
113 |
} else {
|
|
114 |
|
88 | 115 |
pollinfo.fd = fd;
|
89 | 116 |
pollinfo.events = POLLIN;
|
90 | 117 |
if (stream.state == SEND_HEADERS) {
|
91 | 118 |
pollinfo.events |= POLLOUT;
|
92 | 119 |
}
|
93 | |
} else {
|
94 | |
usleep(100000);
|
95 | |
continue;
|
96 | 120 |
}
|
|
121 |
|
|
122 |
UNLOCK;
|
97 | 123 |
|
98 | 124 |
if (poll(&pollinfo, 1, 100)) {
|
99 | 125 |
|
|
217 | 243 |
space = min(space, stream.meta_next);
|
218 | 244 |
}
|
219 | 245 |
|
220 | |
n = stream.state == STREAMING_FILE ? read(fd, streambuf->writep, space) : recv(fd, streambuf->writep, space, 0);
|
|
246 |
n = stream.state == recv(fd, streambuf->writep, space, 0);
|
221 | 247 |
if (n == 0) {
|
222 | 248 |
LOG_INFO("end of stream");
|
223 | 249 |
_disconnect(DISCONNECT, DISCONNECT_OK);
|
|
314 | 340 |
|
315 | 341 |
LOG_INFO("opening local file: %s", stream.header);
|
316 | 342 |
|
|
343 |
#if WIN
|
|
344 |
fd = open(stream.header, O_RDONLY | O_BINARY);
|
|
345 |
#else
|
317 | 346 |
fd = open(stream.header, O_RDONLY);
|
|
347 |
#endif
|
|
348 |
|
318 | 349 |
stream.state = STREAMING_FILE;
|
319 | 350 |
if (fd < 0) {
|
320 | 351 |
LOG_INFO("can't open file: %s", stream.header);
|