Client: move message functions into the class
This commit is contained in:
parent
c2d5ce0ca2
commit
75ba961e97
@ -141,7 +141,7 @@ src_mpd_SOURCES = \
|
||||
src/ClientRead.cxx \
|
||||
src/ClientWrite.cxx \
|
||||
src/ClientMessage.cxx src/ClientMessage.hxx \
|
||||
src/ClientSubscribe.cxx src/ClientSubscribe.hxx \
|
||||
src/ClientSubscribe.cxx \
|
||||
src/ClientFile.cxx src/ClientFile.hxx \
|
||||
src/Listen.cxx src/Listen.hxx \
|
||||
src/LogInit.cxx src/LogInit.hxx \
|
||||
|
@ -86,11 +86,6 @@ public:
|
||||
return FullyBufferedSocket::IsDefined();
|
||||
}
|
||||
|
||||
gcc_pure
|
||||
bool IsSubscribed(const char *channel_name) const {
|
||||
return subscriptions.find(channel_name) != subscriptions.end();
|
||||
}
|
||||
|
||||
gcc_pure
|
||||
bool IsExpired() const {
|
||||
return !FullyBufferedSocket::IsDefined();
|
||||
@ -132,6 +127,30 @@ public:
|
||||
void IdleAdd(unsigned flags);
|
||||
bool IdleWait(unsigned flags);
|
||||
|
||||
enum class SubscribeResult {
|
||||
/** success */
|
||||
OK,
|
||||
|
||||
/** invalid channel name */
|
||||
INVALID,
|
||||
|
||||
/** already subscribed to this channel */
|
||||
ALREADY,
|
||||
|
||||
/** too many subscriptions */
|
||||
FULL,
|
||||
};
|
||||
|
||||
gcc_pure
|
||||
bool IsSubscribed(const char *channel_name) const {
|
||||
return subscriptions.find(channel_name) != subscriptions.end();
|
||||
}
|
||||
|
||||
SubscribeResult Subscribe(const char *channel);
|
||||
bool Unsubscribe(const char *channel);
|
||||
void UnsubscribeAll();
|
||||
bool PushMessage(const ClientMessage &msg);
|
||||
|
||||
private:
|
||||
/* virtual methods from class BufferedSocket */
|
||||
virtual InputResult OnSocketInput(void *data, size_t length) override;
|
||||
|
@ -18,72 +18,75 @@
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "ClientSubscribe.hxx"
|
||||
#include "ClientInternal.hxx"
|
||||
#include "Idle.hxx"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
enum client_subscribe_result
|
||||
client_subscribe(Client &client, const char *channel)
|
||||
bool Unsubscribe(const char *channel);
|
||||
void UnsubscribeAll();
|
||||
bool PushMessage(const ClientMessage &msg);
|
||||
|
||||
Client::SubscribeResult
|
||||
Client::Subscribe(const char *channel)
|
||||
{
|
||||
assert(channel != nullptr);
|
||||
|
||||
if (!client_message_valid_channel_name(channel))
|
||||
return CLIENT_SUBSCRIBE_INVALID;
|
||||
return Client::SubscribeResult::INVALID;
|
||||
|
||||
if (client.num_subscriptions >= CLIENT_MAX_SUBSCRIPTIONS)
|
||||
return CLIENT_SUBSCRIBE_FULL;
|
||||
if (num_subscriptions >= CLIENT_MAX_SUBSCRIPTIONS)
|
||||
return Client::SubscribeResult::FULL;
|
||||
|
||||
auto r = client.subscriptions.insert(channel);
|
||||
auto r = subscriptions.insert(channel);
|
||||
if (!r.second)
|
||||
return CLIENT_SUBSCRIBE_ALREADY;
|
||||
return Client::SubscribeResult::ALREADY;
|
||||
|
||||
++client.num_subscriptions;
|
||||
++num_subscriptions;
|
||||
|
||||
idle_add(IDLE_SUBSCRIPTION);
|
||||
|
||||
return CLIENT_SUBSCRIBE_OK;
|
||||
return Client::SubscribeResult::OK;
|
||||
}
|
||||
|
||||
bool
|
||||
client_unsubscribe(Client &client, const char *channel)
|
||||
Client::Unsubscribe(const char *channel)
|
||||
{
|
||||
const auto i = client.subscriptions.find(channel);
|
||||
if (i == client.subscriptions.end())
|
||||
const auto i = subscriptions.find(channel);
|
||||
if (i == subscriptions.end())
|
||||
return false;
|
||||
|
||||
assert(client.num_subscriptions > 0);
|
||||
assert(num_subscriptions > 0);
|
||||
|
||||
client.subscriptions.erase(i);
|
||||
--client.num_subscriptions;
|
||||
subscriptions.erase(i);
|
||||
--num_subscriptions;
|
||||
|
||||
idle_add(IDLE_SUBSCRIPTION);
|
||||
|
||||
assert((client.num_subscriptions == 0) ==
|
||||
client.subscriptions.empty());
|
||||
assert((num_subscriptions == 0) ==
|
||||
subscriptions.empty());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
client_unsubscribe_all(Client &client)
|
||||
Client::UnsubscribeAll()
|
||||
{
|
||||
client.subscriptions.clear();
|
||||
client.num_subscriptions = 0;
|
||||
subscriptions.clear();
|
||||
num_subscriptions = 0;
|
||||
}
|
||||
|
||||
bool
|
||||
client_push_message(Client &client, const ClientMessage &msg)
|
||||
Client::PushMessage(const ClientMessage &msg)
|
||||
{
|
||||
if (client.messages.size() >= CLIENT_MAX_MESSAGES ||
|
||||
!client.IsSubscribed(msg.GetChannel()))
|
||||
if (messages.size() >= CLIENT_MAX_MESSAGES ||
|
||||
!IsSubscribed(msg.GetChannel()))
|
||||
return false;
|
||||
|
||||
if (client.messages.empty())
|
||||
client.IdleAdd(IDLE_MESSAGE);
|
||||
if (messages.empty())
|
||||
IdleAdd(IDLE_MESSAGE);
|
||||
|
||||
client.messages.push_back(msg);
|
||||
messages.push_back(msg);
|
||||
return true;
|
||||
}
|
||||
|
@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MPD_CLIENT_SUBSCRIBE_HXX
|
||||
#define MPD_CLIENT_SUBSCRIBE_HXX
|
||||
|
||||
#include "Compiler.h"
|
||||
|
||||
class Client;
|
||||
class ClientMessage;
|
||||
|
||||
enum client_subscribe_result {
|
||||
/** success */
|
||||
CLIENT_SUBSCRIBE_OK,
|
||||
|
||||
/** invalid channel name */
|
||||
CLIENT_SUBSCRIBE_INVALID,
|
||||
|
||||
/** already subscribed to this channel */
|
||||
CLIENT_SUBSCRIBE_ALREADY,
|
||||
|
||||
/** too many subscriptions */
|
||||
CLIENT_SUBSCRIBE_FULL,
|
||||
};
|
||||
|
||||
enum client_subscribe_result
|
||||
client_subscribe(Client &client, const char *channel);
|
||||
|
||||
bool
|
||||
client_unsubscribe(Client &client, const char *channel);
|
||||
|
||||
void
|
||||
client_unsubscribe_all(Client &client);
|
||||
|
||||
bool
|
||||
client_push_message(Client &client, const ClientMessage &msg);
|
||||
|
||||
#endif
|
@ -19,7 +19,6 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "MessageCommands.hxx"
|
||||
#include "ClientSubscribe.hxx"
|
||||
#include "Client.hxx"
|
||||
#include "ClientList.hxx"
|
||||
#include "Instance.hxx"
|
||||
@ -37,28 +36,29 @@ handle_subscribe(Client &client, gcc_unused int argc, char *argv[])
|
||||
{
|
||||
assert(argc == 2);
|
||||
|
||||
switch (client_subscribe(client, argv[1])) {
|
||||
case CLIENT_SUBSCRIBE_OK:
|
||||
switch (client.Subscribe(argv[1])) {
|
||||
case Client::SubscribeResult::OK:
|
||||
return COMMAND_RETURN_OK;
|
||||
|
||||
case CLIENT_SUBSCRIBE_INVALID:
|
||||
case Client::SubscribeResult::INVALID:
|
||||
command_error(client, ACK_ERROR_ARG,
|
||||
"invalid channel name");
|
||||
return COMMAND_RETURN_ERROR;
|
||||
|
||||
case CLIENT_SUBSCRIBE_ALREADY:
|
||||
case Client::SubscribeResult::ALREADY:
|
||||
command_error(client, ACK_ERROR_EXIST,
|
||||
"already subscribed to this channel");
|
||||
return COMMAND_RETURN_ERROR;
|
||||
|
||||
case CLIENT_SUBSCRIBE_FULL:
|
||||
case Client::SubscribeResult::FULL:
|
||||
command_error(client, ACK_ERROR_EXIST,
|
||||
"subscription list is full");
|
||||
return COMMAND_RETURN_ERROR;
|
||||
}
|
||||
|
||||
/* unreachable */
|
||||
return COMMAND_RETURN_OK;
|
||||
assert(false);
|
||||
gcc_unreachable();
|
||||
}
|
||||
|
||||
enum command_return
|
||||
@ -66,7 +66,7 @@ handle_unsubscribe(Client &client, gcc_unused int argc, char *argv[])
|
||||
{
|
||||
assert(argc == 2);
|
||||
|
||||
if (client_unsubscribe(client, argv[1]))
|
||||
if (client.Unsubscribe(argv[1]))
|
||||
return COMMAND_RETURN_OK;
|
||||
else {
|
||||
command_error(client, ACK_ERROR_NO_EXIST,
|
||||
@ -124,7 +124,7 @@ handle_send_message(Client &client,
|
||||
bool sent = false;
|
||||
const ClientMessage msg(argv[1], argv[2]);
|
||||
for (const auto &c : *instance->client_list)
|
||||
if (client_push_message(*c, msg))
|
||||
if (c->PushMessage(msg))
|
||||
sent = true;
|
||||
|
||||
if (sent)
|
||||
|
Loading…
Reference in New Issue
Block a user