summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2021-01-21 16:27:52 +0100
committerMax Kellermann <max@musicpd.org>2021-01-21 17:17:10 +0100
commit995aafe9cc511430bff7a7a690df70998f4bb025 (patch)
treeb2f570aba54bb10f1b238922e1212766d1cc1a25 /src
parent6e33566ceeb7e6c4454e44efd203b2e2fcd10feb (diff)
protocol: add command "binarylimit"
Increasing the protocol version to 0.22.4 to allow clients to detect this feature. Closes https://github.com/MusicPlayerDaemon/MPD/issues/1038
Diffstat (limited to 'src')
-rw-r--r--src/client/Client.hxx7
-rw-r--r--src/client/Response.cxx2
-rw-r--r--src/client/Response.hxx2
-rw-r--r--src/command/AllCommands.cxx1
-rw-r--r--src/command/ClientCommands.cxx15
-rw-r--r--src/command/ClientCommands.hxx3
-rw-r--r--src/command/FileCommands.cxx33
7 files changed, 44 insertions, 19 deletions
diff --git a/src/client/Client.hxx b/src/client/Client.hxx
index d19d08760..deb4b8caa 100644
--- a/src/client/Client.hxx
+++ b/src/client/Client.hxx
@@ -84,6 +84,12 @@ public:
*/
TagMask tag_mask = TagMask::All();
+ /**
+ * The maximum number of bytes transmitted in a binary
+ * response. Can be changed with the "binarylimit" command.
+ */
+ size_t binary_limit = 8192;
+
private:
static constexpr size_t MAX_SUBSCRIPTIONS = 16;
@@ -122,6 +128,7 @@ public:
~Client() noexcept;
using FullyBufferedSocket::GetEventLoop;
+ using FullyBufferedSocket::GetOutputMaxSize;
gcc_pure
bool IsExpired() const noexcept {
diff --git a/src/client/Response.cxx b/src/client/Response.cxx
index 3900df75e..0c9145a50 100644
--- a/src/client/Response.cxx
+++ b/src/client/Response.cxx
@@ -59,7 +59,7 @@ Response::Format(const char *fmt, ...) noexcept
bool
Response::WriteBinary(ConstBuffer<void> payload) noexcept
{
- assert(payload.size <= MAX_BINARY_SIZE);
+ assert(payload.size <= client.binary_limit);
return Format("binary: %zu\n", payload.size) &&
Write(payload.data, payload.size) &&
diff --git a/src/client/Response.hxx b/src/client/Response.hxx
index 00c1566fc..4c7e40e8a 100644
--- a/src/client/Response.hxx
+++ b/src/client/Response.hxx
@@ -79,8 +79,6 @@ public:
gcc_printf(2,3)
bool Format(const char *fmt, ...) noexcept;
- static constexpr size_t MAX_BINARY_SIZE = 8192;
-
/**
* Write a binary chunk; this writes the "binary" line, the
* given chunk and the trailing newline.
diff --git a/src/command/AllCommands.cxx b/src/command/AllCommands.cxx
index 96ae21e42..4508fa92f 100644
--- a/src/command/AllCommands.cxx
+++ b/src/command/AllCommands.cxx
@@ -87,6 +87,7 @@ static constexpr struct command commands[] = {
{ "addid", PERMISSION_ADD, 1, 2, handle_addid },
{ "addtagid", PERMISSION_ADD, 3, 3, handle_addtagid },
{ "albumart", PERMISSION_READ, 2, 2, handle_album_art },
+ { "binarylimit", PERMISSION_NONE, 1, 1, handle_binary_limit },
{ "channels", PERMISSION_READ, 0, 0, handle_channels },
{ "clear", PERMISSION_CONTROL, 0, 0, handle_clear },
{ "clearerror", PERMISSION_CONTROL, 0, 0, handle_clearerror },
diff --git a/src/command/ClientCommands.cxx b/src/command/ClientCommands.cxx
index f0e3470be..01bd462f2 100644
--- a/src/command/ClientCommands.cxx
+++ b/src/command/ClientCommands.cxx
@@ -41,6 +41,21 @@ handle_ping([[maybe_unused]] Client &client, [[maybe_unused]] Request args,
}
CommandResult
+handle_binary_limit(Client &client, Request args,
+ [[maybe_unused]] Response &r)
+{
+ size_t value = args.ParseUnsigned(0, client.GetOutputMaxSize() - 4096);
+ if (value < 64) {
+ r.Error(ACK_ERROR_ARG, "Value too small");
+ return CommandResult::ERROR;
+ }
+
+ client.binary_limit = value;
+
+ return CommandResult::OK;
+}
+
+CommandResult
handle_password(Client &client, Request args, Response &r)
{
unsigned permission = 0;
diff --git a/src/command/ClientCommands.hxx b/src/command/ClientCommands.hxx
index fc0dc42e1..de766e802 100644
--- a/src/command/ClientCommands.hxx
+++ b/src/command/ClientCommands.hxx
@@ -33,6 +33,9 @@ CommandResult
handle_ping(Client &client, Request request, Response &response);
CommandResult
+handle_binary_limit(Client &client, Request request, Response &response);
+
+CommandResult
handle_password(Client &client, Request request, Response &response);
CommandResult
diff --git a/src/command/FileCommands.cxx b/src/command/FileCommands.cxx
index 4ac239646..7efbb211c 100644
--- a/src/command/FileCommands.cxx
+++ b/src/command/FileCommands.cxx
@@ -43,6 +43,7 @@
#include "thread/Mutex.hxx"
#include "Log.hxx"
+#include <algorithm>
#include <cassert>
#include <cinttypes> /* for PRIu64 */
@@ -205,28 +206,26 @@ read_stream_art(Response &r, const char *uri, size_t offset)
const offset_type art_file_size = is->GetSize();
- if (offset >= art_file_size) {
- if (offset > art_file_size) {
- r.Error(ACK_ERROR_ARG, "Offset too large");
- return CommandResult::ERROR;
- } else {
- r.Format("size: %" PRIoffset "\n", art_file_size);
- r.WriteBinary(nullptr);
- return CommandResult::OK;
- }
+ if (offset > art_file_size) {
+ r.Error(ACK_ERROR_ARG, "Offset too large");
+ return CommandResult::ERROR;
}
- uint8_t buffer[Response::MAX_BINARY_SIZE];
- size_t read_size;
+ std::size_t buffer_size =
+ std::min<offset_type>(art_file_size - offset,
+ r.GetClient().binary_limit);
- {
+ std::unique_ptr<std::byte[]> buffer(new std::byte[buffer_size]);
+
+ std::size_t read_size = 0;
+ if (buffer_size > 0) {
std::unique_lock<Mutex> lock(mutex);
is->Seek(lock, offset);
- read_size = is->Read(lock, &buffer, sizeof(buffer));
+ read_size = is->Read(lock, buffer.get(), buffer_size);
}
r.Format("size: %" PRIoffset "\n", art_file_size);
- r.WriteBinary({buffer, read_size});
+ r.WriteBinary({buffer.get(), read_size});
return CommandResult::OK;
}
@@ -313,8 +312,10 @@ public:
response.Format("type: %s\n", mime_type);
buffer.size -= offset;
- if (buffer.size > Response::MAX_BINARY_SIZE)
- buffer.size = Response::MAX_BINARY_SIZE;
+
+ const std::size_t binary_limit = response.GetClient().binary_limit;
+ if (buffer.size > binary_limit)
+ buffer.size = binary_limit;
buffer.data = OffsetPointer(buffer.data, offset);
response.WriteBinary(buffer);