client/Process: convert functions to Client methods

This commit is contained in:
Max Kellermann 2019-04-03 20:03:17 +02:00
parent 9f79d034b3
commit 380f73c112
4 changed files with 41 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2003-2018 The Music Player Daemon Project * Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -21,6 +21,7 @@
#define MPD_CLIENT_H #define MPD_CLIENT_H
#include "ClientMessage.hxx" #include "ClientMessage.hxx"
#include "command/CommandResult.hxx"
#include "command/CommandListBuilder.hxx" #include "command/CommandListBuilder.hxx"
#include "tag/Mask.hxx" #include "tag/Mask.hxx"
#include "event/FullyBufferedSocket.hxx" #include "event/FullyBufferedSocket.hxx"
@ -226,6 +227,11 @@ public:
const Storage *GetStorage() const noexcept; const Storage *GetStorage() const noexcept;
private: private:
CommandResult ProcessCommandList(bool list_ok,
std::list<std::string> &&list) noexcept;
CommandResult ProcessLine(char *line) noexcept;
/* virtual methods from class BufferedSocket */ /* virtual methods from class BufferedSocket */
InputResult OnSocketInput(void *data, size_t length) noexcept override; InputResult OnSocketInput(void *data, size_t length) noexcept override;
void OnSocketError(std::exception_ptr ep) noexcept override; void OnSocketError(std::exception_ptr ep) noexcept override;

View File

@ -21,7 +21,6 @@
#define MPD_CLIENT_INTERNAL_HXX #define MPD_CLIENT_INTERNAL_HXX
#include "Client.hxx" #include "Client.hxx"
#include "command/CommandResult.hxx"
#include <chrono> #include <chrono>
@ -34,7 +33,4 @@ extern std::chrono::steady_clock::duration client_timeout;
extern size_t client_max_command_list_size; extern size_t client_max_command_list_size;
extern size_t client_max_output_buffer_size; extern size_t client_max_output_buffer_size;
CommandResult
client_process_line(Client &client, char *line) noexcept;
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2003-2018 The Music Player Daemon Project * Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -28,38 +28,38 @@
#define CLIENT_LIST_OK_MODE_BEGIN "command_list_ok_begin" #define CLIENT_LIST_OK_MODE_BEGIN "command_list_ok_begin"
#define CLIENT_LIST_MODE_END "command_list_end" #define CLIENT_LIST_MODE_END "command_list_end"
static CommandResult inline CommandResult
client_process_command_list(Client &client, bool list_ok, Client::ProcessCommandList(bool list_ok,
std::list<std::string> &&list) noexcept std::list<std::string> &&list) noexcept
{ {
CommandResult ret = CommandResult::OK; CommandResult ret = CommandResult::OK;
unsigned num = 0; unsigned n = 0;
for (auto &&i : list) { for (auto &&i : list) {
char *cmd = &*i.begin(); char *cmd = &*i.begin();
FormatDebug(client_domain, "process command \"%s\"", cmd); FormatDebug(client_domain, "process command \"%s\"", cmd);
ret = command_process(client, num++, cmd); ret = command_process(*this, n++, cmd);
FormatDebug(client_domain, "command returned %i", int(ret)); FormatDebug(client_domain, "command returned %i", int(ret));
if (ret != CommandResult::OK || client.IsExpired()) if (ret != CommandResult::OK || IsExpired())
break; break;
else if (list_ok) else if (list_ok)
client.Write("list_OK\n"); Write("list_OK\n");
} }
return ret; return ret;
} }
CommandResult CommandResult
client_process_line(Client &client, char *line) noexcept Client::ProcessLine(char *line) noexcept
{ {
CommandResult ret; CommandResult ret;
if (StringIsEqual(line, "noidle")) { if (StringIsEqual(line, "noidle")) {
if (client.idle_waiting) { if (idle_waiting) {
/* send empty idle response and leave idle mode */ /* send empty idle response and leave idle mode */
client.idle_waiting = false; idle_waiting = false;
command_success(client); command_success(*this);
} }
/* do nothing if the client wasn't idling: the client /* do nothing if the client wasn't idling: the client
@ -67,44 +67,43 @@ client_process_line(Client &client, char *line) noexcept
client_idle_notify(), which he can now evaluate */ client_idle_notify(), which he can now evaluate */
return CommandResult::OK; return CommandResult::OK;
} else if (client.idle_waiting) { } else if (idle_waiting) {
/* during idle mode, clients must not send anything /* during idle mode, clients must not send anything
except "noidle" */ except "noidle" */
FormatWarning(client_domain, FormatWarning(client_domain,
"[%u] command \"%s\" during idle", "[%u] command \"%s\" during idle",
client.num, line); num, line);
return CommandResult::CLOSE; return CommandResult::CLOSE;
} }
if (client.cmd_list.IsActive()) { if (cmd_list.IsActive()) {
if (StringIsEqual(line, CLIENT_LIST_MODE_END)) { if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
FormatDebug(client_domain, FormatDebug(client_domain,
"[%u] process command list", "[%u] process command list",
client.num); num);
auto &&cmd_list = client.cmd_list.Commit(); auto &&list = cmd_list.Commit();
ret = client_process_command_list(client, ret = ProcessCommandList(cmd_list.IsOKMode(),
client.cmd_list.IsOKMode(), std::move(list));
std::move(cmd_list));
FormatDebug(client_domain, FormatDebug(client_domain,
"[%u] process command " "[%u] process command "
"list returned %i", client.num, int(ret)); "list returned %i", num, int(ret));
if (ret == CommandResult::CLOSE || if (ret == CommandResult::CLOSE ||
client.IsExpired()) IsExpired())
return CommandResult::CLOSE; return CommandResult::CLOSE;
if (ret == CommandResult::OK) if (ret == CommandResult::OK)
command_success(client); command_success(*this);
client.cmd_list.Reset(); cmd_list.Reset();
} else { } else {
if (!client.cmd_list.Add(line)) { if (!cmd_list.Add(line)) {
FormatWarning(client_domain, FormatWarning(client_domain,
"[%u] command list size " "[%u] command list size "
"is larger than the max (%lu)", "is larger than the max (%lu)",
client.num, num,
(unsigned long)client_max_command_list_size); (unsigned long)client_max_command_list_size);
return CommandResult::CLOSE; return CommandResult::CLOSE;
} }
@ -113,10 +112,10 @@ client_process_line(Client &client, char *line) noexcept
} }
} else { } else {
if (StringIsEqual(line, CLIENT_LIST_MODE_BEGIN)) { if (StringIsEqual(line, CLIENT_LIST_MODE_BEGIN)) {
client.cmd_list.Begin(false); cmd_list.Begin(false);
ret = CommandResult::OK; ret = CommandResult::OK;
} else if (StringIsEqual(line, CLIENT_LIST_OK_MODE_BEGIN)) { } else if (StringIsEqual(line, CLIENT_LIST_OK_MODE_BEGIN)) {
client.cmd_list.Begin(true); cmd_list.Begin(true);
ret = CommandResult::OK; ret = CommandResult::OK;
} else if (IsUpperAlphaASCII(*line)) { } else if (IsUpperAlphaASCII(*line)) {
/* no valid MPD command begins with an upper /* no valid MPD command begins with an upper
@ -124,23 +123,22 @@ client_process_line(Client &client, char *line) noexcept
HTTP request */ HTTP request */
FormatWarning(client_domain, FormatWarning(client_domain,
"[%u] malformed command \"%s\"", "[%u] malformed command \"%s\"",
client.num, line); num, line);
ret = CommandResult::CLOSE; ret = CommandResult::CLOSE;
} else { } else {
FormatDebug(client_domain, FormatDebug(client_domain,
"[%u] process command \"%s\"", "[%u] process command \"%s\"",
client.num, line); num, line);
ret = command_process(client, 0, line); ret = command_process(*this, 0, line);
FormatDebug(client_domain, FormatDebug(client_domain,
"[%u] command returned %i", "[%u] command returned %i",
client.num, int(ret)); num, int(ret));
if (ret == CommandResult::CLOSE || if (ret == CommandResult::CLOSE || IsExpired())
client.IsExpired())
return CommandResult::CLOSE; return CommandResult::CLOSE;
if (ret == CommandResult::OK) if (ret == CommandResult::OK)
command_success(client); command_success(*this);
} }
} }

View File

@ -43,7 +43,7 @@ Client::OnSocketInput(void *data, size_t length) noexcept
/* terminate the string at the end of the line */ /* terminate the string at the end of the line */
*end = 0; *end = 0;
CommandResult result = client_process_line(*this, p); CommandResult result = ProcessLine(p);
switch (result) { switch (result) {
case CommandResult::OK: case CommandResult::OK:
case CommandResult::IDLE: case CommandResult::IDLE: