URY playd
C++ minimalist audio player
io.hpp
Go to the documentation of this file.
1 // This file is part of playd.
2 // playd is licensed under the MIT licence: see LICENSE.txt.
3 
10 #ifndef PLAYD_IO_CORE_HPP
11 #define PLAYD_IO_CORE_HPP
12 
13 #include <ostream>
14 #include <set>
15 
16 // Use the same ssize_t as libmpg123 on Windows.
17 #ifndef _MSC_VER
18 typedef long ssize_t;
19 #define _SSIZE_T_
20 #define _SSIZE_T_DEFINED
21 #endif
22 #include <uv.h>
23 
24 #include "player.hpp"
25 #include "response.hpp"
26 #include "tokeniser.hpp"
27 
28 class Player;
29 class Connection;
30 
39 class IoCore : public ResponseSink
40 {
41 public:
47  explicit IoCore(Player &player);
48 
50  IoCore(const IoCore &) = delete;
51 
53  IoCore &operator=(const IoCore &) = delete;
54 
63  void Run(const std::string &host, const std::string &port);
64 
65  //
66  // Connection API
67  //
68 
80  void Accept(uv_stream_t *server);
81 
88  void Remove(size_t id);
89 
95  void UpdatePlayer();
96 
97  void Respond(size_t id, const Response &response) const override;
98 
100  void Shutdown();
101 
102 private:
104  static const uint16_t PLAYER_UPDATE_PERIOD;
105 
106  uv_loop_t *loop;
107  uv_signal_t sigint;
108  uv_tcp_t server;
109  uv_timer_t updater;
110 
112 
114  std::vector<std::shared_ptr<Connection>> pool;
115 
118  std::vector<size_t> free_list;
119 
127  void InitAcceptor(const std::string &address, const std::string &port);
128 
130  void InitUpdateTimer();
131 
138  void InitSignals();
139 
140  //
141  // Connection pool handling
142  //
143 
150  size_t NextConnectionID();
151 
159  void ExpandPool();
160 
161  //
162  // Response dispatch
163  //
164 
169  void Broadcast(const Response &response) const;
170 
176  void Unicast(size_t id, const Response &response) const;
177 };
178 
187 {
188 public:
196  Connection(IoCore &parent, uv_tcp_t *tcp, Player &player, size_t id);
197 
202  ~Connection();
203 
205  Connection(const Connection &) = delete;
206 
208  Connection &operator=(const Connection &) = delete;
209 
214  void Respond(const Response &response);
215 
221  void Read(ssize_t nread, const uv_buf_t *buf);
222 
229  void Shutdown();
230 
236  void Depool();
237 
243  std::string Name();
244 
245 private:
248 
250  uv_tcp_t *tcp;
251 
254 
257 
259  size_t id;
260 
266  Response RunCommand(const std::vector<std::string> &msg);
267 };
268 
269 #endif // PLAYD_IO_CORE_HPP
size_t NextConnectionID()
Acquires the next available connection ID.
Definition: io.cpp:223
A response.
Definition: response.hpp:23
Player & player
The Player to which finished commands should be sent.
Definition: io.hpp:256
void UpdatePlayer()
Performs a player update cycle.
Definition: io.cpp:274
A string tokeniser.
Definition: tokeniser.hpp:24
std::vector< size_t > free_list
A list of free 1-indexed slots inside pool.
Definition: io.hpp:118
IoCore & parent
The pool on which this connection is running.
Definition: io.hpp:247
void Broadcast(const Response &response) const
Sends the given response to all connections.
Definition: io.cpp:316
void InitSignals()
Initialises playd&#39;s signal handling.
Definition: io.cpp:372
size_t id
The Connection&#39;s ID in the connection pool.
Definition: io.hpp:259
uv_signal_t sigint
The libuv handle for the Ctrl-C signal.
Definition: io.hpp:107
Abstract class for anything that can be sent a response.
Definition: response.hpp:117
Player & player
The player.
Definition: io.hpp:111
void Unicast(size_t id, const Response &response) const
Sends the given response to the identified connection.
Definition: io.cpp:327
void Respond(size_t id, const Response &response) const override
Outputs a response.
Definition: io.cpp:305
void Accept(uv_stream_t *server)
Accepts a new connection.
Definition: io.cpp:190
void InitAcceptor(const std::string &address, const std::string &port)
Initialises a TCP acceptor on the given address and port.
Definition: io.cpp:349
void Run(const std::string &host, const std::string &port)
Runs the reactor.
Definition: io.cpp:174
uv_tcp_t server
The libuv handle for the TCP server.
Definition: io.hpp:108
IoCore & operator=(const IoCore &)=delete
Deleted copy-assignment.
void InitUpdateTimer()
Sets up a periodic timer to run the playd update loop.
Definition: io.cpp:338
Declaration of the Player class, and associated types.
std::vector< std::shared_ptr< Connection > > pool
The set of connections inside this IoCore.
Definition: io.hpp:114
void ExpandPool()
Adds a new connection slot to the connection pool.
Definition: io.cpp:243
Declaration of classes pertaining to responses to the client.
uv_tcp_t * tcp
The libuv handle for the TCP connection.
Definition: io.hpp:250
void Shutdown()
Shuts down the IoCore by terminating all IO loop tasks.
Definition: io.cpp:280
IoCore(Player &player)
Constructs an IoCore.
Definition: io.cpp:170
Declaration of the Tokeniser class.
Tokeniser tokeniser
The Tokeniser to which data read on this connection should be sent.
Definition: io.hpp:253
A TCP connection from a client.
Definition: io.hpp:186
uv_loop_t * loop
The loop this IoCore is using.
Definition: io.hpp:106
A Player contains a loaded audio file and a command API for manipulating it.
Definition: player.hpp:31
void Remove(size_t id)
Removes a connection.
Definition: io.cpp:260
The IO core, which services input, routes responses, and executes the Player update routine periodica...
Definition: io.hpp:39
static const uint16_t PLAYER_UPDATE_PERIOD
The period between player updates.
Definition: io.hpp:104
uv_timer_t updater
The libuv handle for the update timer.
Definition: io.hpp:109