MPD: client/Client.hxx Source File

MPD 0.20.18

Client.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_CLIENT_H
21 #define MPD_CLIENT_H
22
23 #include "check.h"
24 #include "ClientMessage.hxx"
25 #include "command/CommandListBuilder.hxx"
26 #include "event/FullyBufferedSocket.hxx"
27 #include "event/TimeoutMonitor.hxx"
28 #include "Compiler.h"
29
30 #include <boost/intrusive/link_mode.hpp>
31 #include <boost/intrusive/list_hook.hpp>
32
33 #include <set>
34 #include <string>
35 #include <list>
36
37 #include <stddef.h>
38 #include <stdarg.h>
39
40 class SocketAddress;
41 class EventLoop;
42 class Path;
43 struct Partition;
44 class Database;
45 class Storage;
46
47 class Client final
48 : FullyBufferedSocket, TimeoutMonitor,
49 public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
50 public:
51 Partition &partition;
52 struct playlist &playlist;
53 struct PlayerControl &player_control;
54
55 unsigned permission;
56
58 int uid;
59
60 CommandListBuilder cmd_list;
61
62 unsigned int num; /* client number */
63
65 bool idle_waiting;
66
69 unsigned idle_flags;
70
72 unsigned idle_subscriptions;
73
77 std::set<std::string> subscriptions;
78
83 unsigned num_subscriptions;
84
88 std::list<ClientMessage> messages;
89
90 Client(EventLoop &loop, Partition &partition,
91 int fd, int uid, int num);
92
93 ~Client() {
94 if (FullyBufferedSocket::IsDefined())
95 FullyBufferedSocket::Close();
96 }
97
98 bool IsConnected() const {
99 return FullyBufferedSocket::IsDefined();
100 }
101
102 gcc_pure
103 bool IsExpired() const noexcept {
104 return !FullyBufferedSocket::IsDefined();
105 }
106
107 void Close();
108 void SetExpired();
109
110 bool Write(const void *data, size_t length);
111
115 bool Write(const char *data);
116
121 int GetUID() const {
122 return uid;
123 }
124
129 bool IsLocal() const {
130 return uid >= 0;
131 }
132
133 unsigned GetPermission() const {
134 return permission;
135 }
136
137 void SetPermission(unsigned _permission) {
138 permission = _permission;
139 }
140
144 void IdleNotify();
145 void IdleAdd(unsigned flags);
146 bool IdleWait(unsigned flags);
147
148 enum class SubscribeResult {
150 OK,
151
153 INVALID,
154
156 ALREADY,
157
159 FULL,
160 };
161
162 gcc_pure
163 bool IsSubscribed(const char *channel_name) const noexcept {
164 return subscriptions.find(channel_name) != subscriptions.end();
165 }
166
167 SubscribeResult Subscribe(const char *channel);
168 bool Unsubscribe(const char *channel);
169 void UnsubscribeAll();
170 bool PushMessage(const ClientMessage &msg);
171
183 void AllowFile(Path path_fs) const;
184
188 gcc_pure
189 const Database *GetDatabase() const noexcept;
190
194 const Database &GetDatabaseOrThrow() const;
195
196 gcc_pure
197 const Storage *GetStorage() const noexcept;
198
199 private:
200 /* virtual methods from class BufferedSocket */
201 virtual InputResult OnSocketInput(void *data, size_t length) override;
202 void OnSocketError(std::exception_ptr ep) override;
203 virtual void OnSocketClosed() override;
204
205 /* virtual methods from class TimeoutMonitor */
206 virtual void OnTimeout() override;
207 };
208
209 void
210 client_manager_init();
211
212 void
213 client_new(EventLoop &loop, Partition &partition,
214 int fd, SocketAddress address, int uid);
215
219 void client_puts(Client &client, const char *s);
220
224 void client_vprintf(Client &client, const char *fmt, va_list args);
225
229 gcc_printf(2,3)
230 void
231 client_printf(Client &client, const char *fmt, ...);
232
233 #endif
Client::Subscribe
SubscribeResult Subscribe(const char *channel)
Client::GetStorage
gcc_pure const Storage * GetStorage() const noexcept
Client::player_control
struct PlayerControl & player_control
Definition: Client.hxx:53
check.h
playlist
Definition: Playlist.hxx:34
Client::idle_flags
unsigned idle_flags
idle flags pending on this client, to be sent as soon as the client enters "idle" ...
Definition: Client.hxx:69
Client::num
unsigned int num
Definition: Client.hxx:62
FullyBufferedSocket::Close
void Close()
Definition: FullyBufferedSocket.hxx:43
CommandListBuilder.hxx
Client::messages
std::list< ClientMessage > messages
A list of messages this client has received.
Definition: Client.hxx:88
TimeoutMonitor
This class monitors a timeout.
Definition: TimeoutMonitor.hxx:37
CommandListBuilder
Definition: CommandListBuilder.hxx:28
SocketAddress
An OO wrapper for struct sockaddr.
Definition: SocketAddress.hxx:46
Client::Client
Client(EventLoop &loop, Partition &partition, int fd, int uid, int num)
Client::GetPermission
unsigned GetPermission() const
Definition: Client.hxx:133
Database
Definition: Interface.hxx:35
Client::IsSubscribed
gcc_pure bool IsSubscribed(const char *channel_name) const noexcept
Definition: Client.hxx:163
Client::Unsubscribe
bool Unsubscribe(const char *channel)
EventLoop
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:52
Storage
Definition: StorageInterface.hxx:45
Client::GetDatabaseOrThrow
const Database & GetDatabaseOrThrow() const
Wrapper for Instance::GetDatabaseOrThrow().
Client::IsLocal
bool IsLocal() const
Is this client running on the same machine, connected with a local (UNIX domain) socket?
Definition: Client.hxx:129
Partition
A partition of the Music Player Daemon.
Definition: Partition.hxx:42
BufferedSocket::InputResult
InputResult
Definition: BufferedSocket.hxx:78
Client::IdleAdd
void IdleAdd(unsigned flags)
gcc_printf
gcc_printf(2, 3) void client_printf(Client &client
Write a printf-like formatted string to the client.
Client::SetPermission
void SetPermission(unsigned _permission)
Definition: Client.hxx:137
Client::cmd_list
CommandListBuilder cmd_list
Definition: Client.hxx:60
client_vprintf
void client_vprintf(Client &client, const char *fmt, va_list args)
Write a printf-like formatted string to the client.
Client::GetDatabase
gcc_pure const Database * GetDatabase() const noexcept
Wrapper for Instance::GetDatabase().
Client::Close
void Close()
client_manager_init
void client_manager_init()
Client::SubscribeResult::FULL
too many subscriptions
Client::idle_subscriptions
unsigned idle_subscriptions
idle flags that the client wants to receive
Definition: Client.hxx:72
client_new
void client_new(EventLoop &loop, Partition &partition, int fd, SocketAddress address, int uid)
ClientMessage
A client-to-client message.
Definition: ClientMessage.hxx:36
Path
A path name in the native file system character set.
Definition: Path.hxx:39
SocketMonitor::IsDefined
bool IsDefined() const
Definition: SocketMonitor.hxx:83
Compiler.h
Client::UnsubscribeAll
void UnsubscribeAll()
Client::AllowFile
void AllowFile(Path path_fs) const
Is this client allowed to use the specified local file?
Client::IsConnected
bool IsConnected() const
Definition: Client.hxx:98
TimeoutMonitor.hxx
Client::num_subscriptions
unsigned num_subscriptions
The number of subscriptions in subscriptions.
Definition: Client.hxx:83
Client
Definition: Client.hxx:47
Client::SubscribeResult::ALREADY
already subscribed to this channel
Client::permission
unsigned permission
Definition: Client.hxx:55
Client::SubscribeResult
SubscribeResult
Definition: Client.hxx:148
PlayerControl
Definition: Control.hxx:100
FullyBufferedSocket.hxx
client_puts
void client_puts(Client &client, const char *s)
Write a C string to the client.
Client::Write
bool Write(const void *data, size_t length)
Client::SubscribeResult::INVALID
invalid channel name
Client::subscriptions
std::set< std::string > subscriptions
A list of channel names this client is subscribed to.
Definition: Client.hxx:77
Client::PushMessage
bool PushMessage(const ClientMessage &msg)
Client::IsExpired
gcc_pure bool IsExpired() const noexcept
Definition: Client.hxx:103
Client::~Client
~Client()
Definition: Client.hxx:93
Client::partition
Partition & partition
Definition: Client.hxx:51
Client::playlist
struct playlist & playlist
Definition: Client.hxx:52
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
Client::SetExpired
void SetExpired()
FullyBufferedSocket
A BufferedSocket specialization that adds an output buffer.
Definition: FullyBufferedSocket.hxx:31
Client::SubscribeResult::OK
success
Client::GetUID
int GetUID() const
returns the uid of the client process, or a negative value if the uid is unknown
Definition: Client.hxx:121
fmt
const char * fmt
Definition: Client.hxx:231
Client::uid
int uid
the uid of the client process, or -1 if unknown
Definition: Client.hxx:58
Client::IdleWait
bool IdleWait(unsigned flags)
ClientMessage.hxx
Client::idle_waiting
bool idle_waiting
is this client waiting for an "idle" response?
Definition: Client.hxx:65
Client::IdleNotify
void IdleNotify()
Send "idle" response to this client.