From a9cb12b745c099edef622ed5dd1a84852a1bc699 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 3 Apr 2019 20:02:59 +0200 Subject: [PATCH] Client: make almost all attributes `private` --- src/client/Client.hxx | 17 ++++++++++++++++- src/client/ClientNew.cxx | 7 ++++--- src/command/MessageCommands.cxx | 17 ++++++++--------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/client/Client.hxx b/src/client/Client.hxx index f6677b145..68c146077 100644 --- a/src/client/Client.hxx +++ b/src/client/Client.hxx @@ -56,7 +56,6 @@ class Client final Partition *partition; -public: unsigned permission; /** the uid of the client process, or -1 if unknown */ @@ -76,11 +75,14 @@ public: /** idle flags that the client wants to receive */ unsigned idle_subscriptions; +public: + // TODO: make this attribute "private" /** * The tags this client is interested in. */ TagMask tag_mask = TagMask::All(); +private: /** * A list of channel names this client is subscribed to. */ @@ -97,6 +99,7 @@ public: */ std::list messages; +public: Client(EventLoop &loop, Partition &partition, UniqueSocketDescriptor fd, int uid, unsigned _permission, @@ -175,11 +178,23 @@ public: return subscriptions.find(channel_name) != subscriptions.end(); } + const auto &GetSubscriptions() const noexcept { + return subscriptions; + } + SubscribeResult Subscribe(const char *channel) noexcept; bool Unsubscribe(const char *channel) noexcept; void UnsubscribeAll() noexcept; bool PushMessage(const ClientMessage &msg) noexcept; + template + void ConsumeMessages(F &&f) { + while (!messages.empty()) { + f(messages.front()); + messages.pop_front(); + } + } + /** * Is this client allowed to use the specified local file? * diff --git a/src/client/ClientNew.cxx b/src/client/ClientNew.cxx index 85ce97aa3..5f1f36470 100644 --- a/src/client/ClientNew.cxx +++ b/src/client/ClientNew.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * 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); + const unsigned num = next_client_num++; Client *client = new Client(loop, partition, std::move(fd), uid, permission, - next_client_num++); + num); client_list.Add(*client); FormatInfo(client_domain, "[%u] opened from %s", - client->num, remote.c_str()); + num, remote.c_str()); } void diff --git a/src/command/MessageCommands.cxx b/src/command/MessageCommands.cxx index 78f7f3790..fd3592fb0 100644 --- a/src/command/MessageCommands.cxx +++ b/src/command/MessageCommands.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * 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()); std::set channels; - for (const auto &c : *client.GetInstance().client_list) - channels.insert(c.subscriptions.begin(), - c.subscriptions.end()); + for (const auto &c : *client.GetInstance().client_list) { + const auto &subscriptions = c.GetSubscriptions(); + channels.insert(subscriptions.begin(), + subscriptions.end()); + } for (const auto &channel : channels) r.Format("channel: %s\n", channel.c_str()); @@ -94,13 +96,10 @@ handle_read_messages(Client &client, { assert(args.empty()); - while (!client.messages.empty()) { - const ClientMessage &msg = client.messages.front(); - + client.ConsumeMessages([&r](const auto &msg){ r.Format("channel: %s\nmessage: %s\n", msg.GetChannel(), msg.GetMessage()); - client.messages.pop_front(); - } + }); return CommandResult::OK; }