diff options
Diffstat (limited to 'src/plugins/youtube')
-rw-r--r-- | src/plugins/youtube/YoutubeMediaProxy.cpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/plugins/youtube/YoutubeMediaProxy.cpp b/src/plugins/youtube/YoutubeMediaProxy.cpp index 6e2c5c4..b33c122 100644 --- a/src/plugins/youtube/YoutubeMediaProxy.cpp +++ b/src/plugins/youtube/YoutubeMediaProxy.cpp @@ -18,6 +18,32 @@ // TODO: What if the client sends a new header without reconnecting? is that even allowed by http standard? // TODO: Detect when download has finished (and close connection). +static ssize_t read_eintr(int fd, void *buffer, size_t size) { + while(true) { + ssize_t bytes_read = read(fd, buffer, size); + if(bytes_read == -1) { + if(errno != EINTR) + return -1; + } else { + return bytes_read; + } + } +} + +static ssize_t write_all(int fd, const void *buffer, size_t size) { + ssize_t bytes_written = 0; + while((size_t)bytes_written < size) { + ssize_t written = write(fd, (char*)buffer + bytes_written, size - bytes_written); + if(written == -1) { + if(errno != EINTR) + return -1; + } else { + bytes_written += written; + } + } + return bytes_written; +} + namespace QuickMedia { static const int MAX_BUFFER_SIZE = 65536; static const int64_t RANGE = 5242870; @@ -232,7 +258,7 @@ namespace QuickMedia { return Error::OK; char read_buffer[4096]; - const ssize_t num_bytes_read = read(client_fd, read_buffer, sizeof(read_buffer)); + const ssize_t num_bytes_read = read_eintr(client_fd, read_buffer, sizeof(read_buffer)); if(num_bytes_read == -1) { const int err = errno; if(err == EAGAIN || err == EWOULDBLOCK) { @@ -308,7 +334,7 @@ namespace QuickMedia { if(program_status != 0) { //fprintf(stderr, "YoutubeStaticMediaProxy::update_download_program_status: download failed, exit status: %d\n", program_status); if(client_fd != -1) { - write(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1); + write_all(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1); close(client_fd); client_fd = -1; client_request_buffer.clear(); @@ -354,7 +380,7 @@ namespace QuickMedia { if(!start_download_success) { fprintf(stderr, "YoutubeStaticMediaProxy::update_download_program_status: failed to start download\n"); if(client_fd != -1) { - write(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1); + write_all(client_fd, download_error_response_msg, sizeof(download_error_response_msg) - 1); close(client_fd); client_fd = -1; client_request_buffer.clear(); @@ -392,7 +418,7 @@ namespace QuickMedia { return Error::OK; } - const ssize_t num_bytes_written = write(client_fd, buffer_start + buffer_offset, num_bytes_to_write); + const ssize_t num_bytes_written = write_all(client_fd, buffer_start + buffer_offset, num_bytes_to_write); if(num_bytes_written == -1) { const int err = errno; if(err == EAGAIN || err == EWOULDBLOCK) { @@ -446,7 +472,7 @@ namespace QuickMedia { YoutubeStaticMediaProxy::Error YoutubeStaticMediaProxy::handle_download() { // TODO: Maybe read even if write is being slow and failing? if(download_read_buffer_offset == 0) { - downloader_num_read_bytes = read(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer)); + downloader_num_read_bytes = read_eintr(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer)); if(downloader_num_read_bytes == -1) { const int err = errno; if(err == EAGAIN || err == EWOULDBLOCK) { @@ -673,7 +699,7 @@ namespace QuickMedia { return Error::OK; } - const ssize_t num_bytes_written = write(fd[1], buffer_start + buffer_offset, num_bytes_to_write); + const ssize_t num_bytes_written = write_all(fd[1], buffer_start + buffer_offset, num_bytes_to_write); if(num_bytes_written == -1) { const int err = errno; if(err == EAGAIN || err == EWOULDBLOCK) { @@ -703,7 +729,7 @@ namespace QuickMedia { return Error::OK; if(download_read_buffer_offset == 0) { - downloader_num_read_bytes = read(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer)); + downloader_num_read_bytes = read_eintr(downloader_read_program.read_fd, download_read_buffer, sizeof(download_read_buffer)); if(downloader_num_read_bytes == -1) { const int err = errno; if(err == EAGAIN || err == EWOULDBLOCK) { |