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"
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
49  public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
50 public:
52  struct playlist &playlist;
54 
55  unsigned permission;
56 
58  int uid;
59 
61 
62  unsigned int num; /* client number */
63 
66 
69  unsigned idle_flags;
70 
73 
77  std::set<std::string> subscriptions;
78 
84 
88  std::list<ClientMessage> messages;
89 
91  int fd, int uid, int num);
92 
93  ~Client() {
96  }
97 
98  bool IsConnected() const {
100  }
101 
102  gcc_pure
103  bool IsExpired() const noexcept {
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
211 
212 void
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
SubscribeResult Subscribe(const char *channel)
gcc_pure const Storage * GetStorage() const noexcept
struct PlayerControl & player_control
Definition: Client.hxx:53
unsigned idle_flags
idle flags pending on this client, to be sent as soon as the client enters "idle" ...
Definition: Client.hxx:69
unsigned int num
Definition: Client.hxx:62
std::list< ClientMessage > messages
A list of messages this client has received.
Definition: Client.hxx:88
This class monitors a timeout.
An OO wrapper for struct sockaddr.
Client(EventLoop &loop, Partition &partition, int fd, int uid, int num)
unsigned GetPermission() const
Definition: Client.hxx:133
gcc_pure bool IsSubscribed(const char *channel_name) const noexcept
Definition: Client.hxx:163
bool Unsubscribe(const char *channel)
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:52
const Database & GetDatabaseOrThrow() const
Wrapper for Instance::GetDatabaseOrThrow().
bool IsLocal() const
Is this client running on the same machine, connected with a local (UNIX domain) socket?
Definition: Client.hxx:129
A partition of the Music Player Daemon.
Definition: Partition.hxx:42
void IdleAdd(unsigned flags)
gcc_printf(2, 3) void client_printf(Client &client
Write a printf-like formatted string to the client.
void SetPermission(unsigned _permission)
Definition: Client.hxx:137
CommandListBuilder cmd_list
Definition: Client.hxx:60
void client_vprintf(Client &client, const char *fmt, va_list args)
Write a printf-like formatted string to the client.
gcc_pure const Database * GetDatabase() const noexcept
Wrapper for Instance::GetDatabase().
void Close()
void client_manager_init()
too many subscriptions
unsigned idle_subscriptions
idle flags that the client wants to receive
Definition: Client.hxx:72
void client_new(EventLoop &loop, Partition &partition, int fd, SocketAddress address, int uid)
A client-to-client message.
A path name in the native file system character set.
Definition: Path.hxx:39
bool IsDefined() const
void UnsubscribeAll()
void AllowFile(Path path_fs) const
Is this client allowed to use the specified local file?
bool IsConnected() const
Definition: Client.hxx:98
unsigned num_subscriptions
The number of subscriptions in subscriptions.
Definition: Client.hxx:83
already subscribed to this channel
unsigned permission
Definition: Client.hxx:55
SubscribeResult
Definition: Client.hxx:148
void client_puts(Client &client, const char *s)
Write a C string to the client.
bool Write(const void *data, size_t length)
std::set< std::string > subscriptions
A list of channel names this client is subscribed to.
Definition: Client.hxx:77
bool PushMessage(const ClientMessage &msg)
gcc_pure bool IsExpired() const noexcept
Definition: Client.hxx:103
~Client()
Definition: Client.hxx:93
Partition & partition
Definition: Client.hxx:51
struct playlist & playlist
Definition: Client.hxx:52
#define gcc_pure
Definition: Compiler.h:116
void SetExpired()
A BufferedSocket specialization that adds an output buffer.
int GetUID() const
returns the uid of the client process, or a negative value if the uid is unknown
Definition: Client.hxx:121
const char * fmt
Definition: Client.hxx:231
int uid
the uid of the client process, or -1 if unknown
Definition: Client.hxx:58
bool IdleWait(unsigned flags)
bool idle_waiting
is this client waiting for an "idle" response?
Definition: Client.hxx:65
void IdleNotify()
Send "idle" response to this client.