Client: make almost all attributes `private`

This commit is contained in:
Max Kellermann 2019-04-03 20:02:59 +02:00
parent 380f73c112
commit a9cb12b745
3 changed files with 28 additions and 13 deletions

View File

@ -56,7 +56,6 @@ class Client final
Partition *partition; Partition *partition;
public:
unsigned permission; unsigned permission;
/** the uid of the client process, or -1 if unknown */ /** the uid of the client process, or -1 if unknown */
@ -76,11 +75,14 @@ public:
/** idle flags that the client wants to receive */ /** idle flags that the client wants to receive */
unsigned idle_subscriptions; unsigned idle_subscriptions;
public:
// TODO: make this attribute "private"
/** /**
* The tags this client is interested in. * The tags this client is interested in.
*/ */
TagMask tag_mask = TagMask::All(); TagMask tag_mask = TagMask::All();
private:
/** /**
* A list of channel names this client is subscribed to. * A list of channel names this client is subscribed to.
*/ */
@ -97,6 +99,7 @@ public:
*/ */
std::list<ClientMessage> messages; std::list<ClientMessage> messages;
public:
Client(EventLoop &loop, Partition &partition, Client(EventLoop &loop, Partition &partition,
UniqueSocketDescriptor fd, int uid, UniqueSocketDescriptor fd, int uid,
unsigned _permission, unsigned _permission,
@ -175,11 +178,23 @@ public:
return subscriptions.find(channel_name) != subscriptions.end(); return subscriptions.find(channel_name) != subscriptions.end();
} }
const auto &GetSubscriptions() const noexcept {
return subscriptions;
}
SubscribeResult Subscribe(const char *channel) noexcept; SubscribeResult Subscribe(const char *channel) noexcept;
bool Unsubscribe(const char *channel) noexcept; bool Unsubscribe(const char *channel) noexcept;
void UnsubscribeAll() noexcept; void UnsubscribeAll() noexcept;
bool PushMessage(const ClientMessage &msg) noexcept; bool PushMessage(const ClientMessage &msg) noexcept;
template<typename F>
void ConsumeMessages(F &&f) {
while (!messages.empty()) {
f(messages.front());
messages.pop_front();
}
}
/** /**
* Is this client allowed to use the specified local file? * Is this client allowed to use the specified local file?
* *

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
@ -70,14 +70,15 @@ client_new(EventLoop &loop, Partition &partition,
(void)fd.Write(GREETING, sizeof(GREETING) - 1); (void)fd.Write(GREETING, sizeof(GREETING) - 1);
const unsigned num = next_client_num++;
Client *client = new Client(loop, partition, std::move(fd), uid, Client *client = new Client(loop, partition, std::move(fd), uid,
permission, permission,
next_client_num++); num);
client_list.Add(*client); client_list.Add(*client);
FormatInfo(client_domain, "[%u] opened from %s", FormatInfo(client_domain, "[%u] opened from %s",
client->num, remote.c_str()); num, remote.c_str());
} }
void void

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
@ -78,9 +78,11 @@ handle_channels(Client &client, gcc_unused Request args, Response &r)
assert(args.empty()); assert(args.empty());
std::set<std::string> channels; std::set<std::string> channels;
for (const auto &c : *client.GetInstance().client_list) for (const auto &c : *client.GetInstance().client_list) {
channels.insert(c.subscriptions.begin(), const auto &subscriptions = c.GetSubscriptions();
c.subscriptions.end()); channels.insert(subscriptions.begin(),
subscriptions.end());
}
for (const auto &channel : channels) for (const auto &channel : channels)
r.Format("channel: %s\n", channel.c_str()); r.Format("channel: %s\n", channel.c_str());
@ -94,13 +96,10 @@ handle_read_messages(Client &client,
{ {
assert(args.empty()); assert(args.empty());
while (!client.messages.empty()) { client.ConsumeMessages([&r](const auto &msg){
const ClientMessage &msg = client.messages.front();
r.Format("channel: %s\nmessage: %s\n", r.Format("channel: %s\nmessage: %s\n",
msg.GetChannel(), msg.GetMessage()); msg.GetChannel(), msg.GetMessage());
client.messages.pop_front(); });
}
return CommandResult::OK; return CommandResult::OK;
} }