InputStream.cxx 3.33 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "config.h"
21
#include "InputStream.hxx"
22
#include "tag/Tag.hxx"
23
#include "thread/Cond.hxx"
24
#include "util/StringCompare.hxx"
25

26 27
#include <stdexcept>

28
#include <assert.h>
29

30
InputStream::~InputStream() noexcept
31 32 33
{
}

34 35
void
InputStream::Check()
36 37 38
{
}

39
void
40
InputStream::Update() noexcept
41 42 43
{
}

44
void
45
InputStream::SetReady() noexcept
46 47 48 49 50 51 52
{
	assert(!ready);

	ready = true;
	cond.broadcast();
}

53
void
54
InputStream::WaitReady() noexcept
55 56
{
	while (true) {
57 58
		Update();
		if (ready)
59 60
			break;

61
		cond.wait(mutex);
62 63 64 65
	}
}

void
66
InputStream::LockWaitReady() noexcept
67
{
68
	const std::lock_guard<Mutex> protect(mutex);
69
	WaitReady();
70 71
}

72 73 74 75 76 77 78
/**
 * Is seeking on resources behind this URI "expensive"?  For example,
 * seeking in a HTTP file requires opening a new connection with a new
 * HTTP request.
 */
gcc_pure
static bool
79
ExpensiveSeeking(const char *uri) noexcept
80
{
81 82
	return StringStartsWith(uri, "http://") ||
		StringStartsWith(uri, "https://");
83 84
}

85
bool
86
InputStream::CheapSeeking() const noexcept
87
{
88
	return IsSeekable() && !ExpensiveSeeking(uri.c_str());
89 90
}

91 92
void
InputStream::Seek(gcc_unused offset_type new_offset)
Avuton Olrich's avatar
Avuton Olrich committed
93
{
94
	throw std::runtime_error("Seeking is not implemented");
95 96
}

97 98
void
InputStream::LockSeek(offset_type _offset)
99
{
100
	const std::lock_guard<Mutex> protect(mutex);
101
	Seek(_offset);
102 103
}

104 105
void
InputStream::LockSkip(offset_type _offset)
106
{
107
	const std::lock_guard<Mutex> protect(mutex);
108
	Skip(_offset);
109 110
}

111
std::unique_ptr<Tag>
112
InputStream::ReadTag()
113
{
114
	return nullptr;
115 116
}

117
std::unique_ptr<Tag>
118
InputStream::LockReadTag()
119
{
120
	const std::lock_guard<Mutex> protect(mutex);
121
	return ReadTag();
122 123 124
}

bool
125
InputStream::IsAvailable() noexcept
126
{
127
	return true;
128 129
}

130
size_t
131
InputStream::LockRead(void *ptr, size_t _size)
132
{
133 134
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
135
	assert(ptr != nullptr);
136
#endif
137
	assert(_size > 0);
138

139
	const std::lock_guard<Mutex> protect(mutex);
140
	return Read(ptr, _size);
141 142
}

143 144
void
InputStream::ReadFull(void *_ptr, size_t _size)
145 146 147 148 149
{
	uint8_t *ptr = (uint8_t *)_ptr;

	size_t nbytes_total = 0;
	while (_size > 0) {
150
		size_t nbytes = Read(ptr + nbytes_total, _size);
151
		if (nbytes == 0)
152
			throw std::runtime_error("Unexpected end of file");
153 154 155 156 157 158

		nbytes_total += nbytes;
		_size -= nbytes;
	}
}

159 160
void
InputStream::LockReadFull(void *ptr, size_t _size)
161 162 163 164 165 166 167
{
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
	assert(ptr != nullptr);
#endif
	assert(_size > 0);

168
	const std::lock_guard<Mutex> protect(mutex);
169
	ReadFull(ptr, _size);
170 171
}

172
bool
173
InputStream::LockIsEOF() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
174
{
175
	const std::lock_guard<Mutex> protect(mutex);
176
	return IsEOF();
177
}