MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 {
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 {
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;
190
194 gcc_pure
195 const Database &GetDatabaseOrThrow() const;
196
197 gcc_pure
198 const Storage *GetStorage() const;
199
200 private:
201 /* virtual methods from class BufferedSocket */
202 virtual InputResult OnSocketInput(void *data, size_t length) override;
203 void OnSocketError(std::exception_ptr ep) override;
204 virtual void OnSocketClosed() override;
205
206 /* virtual methods from class TimeoutMonitor */
207 virtual void OnTimeout() override;
208 };
209
210 void
211 client_manager_init();
212
213 void
214 client_new(EventLoop &loop, Partition &partition,
215 int fd, SocketAddress address, int uid);
216
220 void client_puts(Client &client, const char *s);
221
225 void client_vprintf(Client &client, const char *fmt, va_list args);
226
230 gcc_printf(2,3)
231 void
232 client_printf(Client &client, const char *fmt, ...);
233
234 #endif
Client::Subscribe
SubscribeResult Subscribe(const char *channel)
Client::player_control
struct PlayerControl & player_control
Definition: Client.hxx:53
check.h
playlist
Definition: Playlist.hxx:34
Client::GetPermission
unsigned GetPermission() const
Definition: Client.hxx:133
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)
Database
Definition: Interface.hxx:35
Client::Unsubscribe
bool Unsubscribe(const char *channel)
EventLoop
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
Storage
Definition: StorageInterface.hxx:46
Partition
A partition of the Music Player Daemon.
Definition: Partition.hxx:42
Client::IsConnected
bool IsConnected() const
Definition: Client.hxx:98
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::GetUID
int GetUID() const
returns the uid of the client process, or a negative value if the uid is unknown
Definition: Client.hxx:121
Client::Close
void Close()
SocketMonitor::IsDefined
bool IsDefined() const
Definition: SocketMonitor.hxx:83
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::IsSubscribed
gcc_pure bool IsSubscribed(const char *channel_name) const
Definition: Client.hxx:163
client_new
void client_new(EventLoop &loop, Partition &partition, int fd, SocketAddress address, int uid)
Client::AllowFile
void AllowFile(Path path_fs) const
Is this client allowed to use the specified local file?
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
Compiler.h
Client::GetDatabase
gcc_pure const Database * GetDatabase() const
Wrapper for Instance::GetDatabase().
Client::UnsubscribeAll
void UnsubscribeAll()
Client::IsExpired
gcc_pure bool IsExpired() const
Definition: Client.hxx:103
Client::GetStorage
gcc_pure const Storage * GetStorage() const
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
Client::GetDatabaseOrThrow
gcc_pure const Database & GetDatabaseOrThrow() const
Wrapper for Instance::GetDatabaseOrThrow().
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::~Client
~Client()
Definition: Client.hxx:93
Client::IsLocal
bool IsLocal() const
Is this client running on the same machine, connected with a local (UNIX domain) socket?
Definition: Client.hxx:129
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
fmt
const char * fmt
Definition: Client.hxx:232
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.
partition
const Partition & partition
Definition: Count.hxx:34