]>
Commit | Line | Data |
---|---|---|
45abc127 JP |
1 | From b1b5cd6bbb1e0dea35e7b86429ff3e04fd67db9f Mon Sep 17 00:00:00 2001 |
2 | From: Jan Palus <atler@pld-linux.org> | |
3 | Date: Wed, 23 Dec 2020 23:31:20 +0100 | |
4 | Subject: [PATCH 1/5] feat(mpd): split noidle into noidle and recv_idle | |
5 | ||
6 | --- | |
7 | include/adapters/mpd.hpp | 3 ++- | |
8 | src/adapters/mpd.cpp | 15 ++++++++++++--- | |
9 | src/modules/mpd.cpp | 3 ++- | |
10 | 3 files changed, 16 insertions(+), 5 deletions(-) | |
11 | ||
12 | diff --git a/include/adapters/mpd.hpp b/include/adapters/mpd.hpp | |
13 | index 2a152b01c..2e9b438e8 100644 | |
14 | --- a/include/adapters/mpd.hpp | |
15 | +++ b/include/adapters/mpd.hpp | |
16 | @@ -91,7 +91,8 @@ namespace mpd { | |
17 | ||
18 | int get_fd(); | |
19 | void idle(); | |
20 | - int noidle(); | |
21 | + bool noidle(); | |
22 | + int recv_idle(); | |
23 | ||
24 | unique_ptr<mpdstatus> get_status(); | |
25 | unique_ptr<mpdstatus> get_status_safe(); | |
26 | diff --git a/src/adapters/mpd.cpp b/src/adapters/mpd.cpp | |
27 | index 4a5438ad5..083427a9c 100644 | |
28 | --- a/src/adapters/mpd.cpp | |
29 | +++ b/src/adapters/mpd.cpp | |
30 | @@ -201,12 +201,21 @@ namespace mpd { | |
31 | } | |
32 | } | |
33 | ||
34 | - int mpdconnection::noidle() { | |
35 | + bool mpdconnection::noidle() { | |
36 | + check_connection(m_connection.get()); | |
37 | + bool success = true; | |
38 | + if (m_idle) { | |
39 | + success = mpd_send_noidle(m_connection.get()); | |
40 | + } | |
41 | + return success; | |
42 | + } | |
43 | + | |
44 | + int mpdconnection::recv_idle() { | |
45 | check_connection(m_connection.get()); | |
46 | int flags = 0; | |
47 | - if (m_idle && mpd_send_noidle(m_connection.get())) { | |
48 | - m_idle = false; | |
49 | + if (m_idle) { | |
50 | flags = mpd_recv_idle(m_connection.get(), true); | |
51 | + m_idle = false; | |
52 | mpd_response_finish(m_connection.get()); | |
53 | check_errors(m_connection.get()); | |
54 | } | |
55 | diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp | |
56 | index ea867f281..1ba33c81c 100644 | |
57 | --- a/src/modules/mpd.cpp | |
58 | +++ b/src/modules/mpd.cpp | |
59 | @@ -182,7 +182,8 @@ namespace modules { | |
60 | m_mpd->idle(); | |
61 | ||
62 | int idle_flags = 0; | |
63 | - if ((idle_flags = m_mpd->noidle()) != 0) { | |
64 | + m_mpd->noidle(); | |
65 | + if ((idle_flags = m_mpd->recv_idle()) != 0) { | |
66 | // Update status on every event | |
67 | m_status->update(idle_flags, m_mpd.get()); | |
68 | return true; | |
69 | ||
70 | From 1e2d86354cf3553df202e3fddc848ee54b2ff872 Mon Sep 17 00:00:00 2001 | |
71 | From: Jan Palus <atler@pld-linux.org> | |
72 | Date: Fri, 25 Dec 2020 01:15:30 +0100 | |
73 | Subject: [PATCH 2/5] feat(mpd): reduce wakeups/network traffic when not | |
74 | tracking time | |
75 | ||
76 | --- | |
77 | include/adapters/mpd.hpp | 1 + | |
78 | src/adapters/mpd.cpp | 15 +++++++++++++++ | |
79 | src/modules/mpd.cpp | 16 ++++++++++++---- | |
80 | 3 files changed, 28 insertions(+), 4 deletions(-) | |
81 | ||
82 | diff --git a/include/adapters/mpd.hpp b/include/adapters/mpd.hpp | |
83 | index 2e9b438e8..7f5a43e1b 100644 | |
84 | --- a/include/adapters/mpd.hpp | |
85 | +++ b/include/adapters/mpd.hpp | |
86 | @@ -93,6 +93,7 @@ namespace mpd { | |
87 | void idle(); | |
88 | bool noidle(); | |
89 | int recv_idle(); | |
90 | + int try_recv_idle(int timeout); | |
91 | ||
92 | unique_ptr<mpdstatus> get_status(); | |
93 | unique_ptr<mpdstatus> get_status_safe(); | |
94 | diff --git a/src/adapters/mpd.cpp b/src/adapters/mpd.cpp | |
95 | index 083427a9c..76f824115 100644 | |
96 | --- a/src/adapters/mpd.cpp | |
97 | +++ b/src/adapters/mpd.cpp | |
98 | @@ -1,5 +1,6 @@ | |
99 | #include <cassert> | |
100 | #include <csignal> | |
101 | +#include <poll.h> | |
102 | #include <thread> | |
103 | #include <utility> | |
104 | ||
105 | @@ -222,6 +223,20 @@ namespace mpd { | |
106 | return flags; | |
107 | } | |
108 | ||
109 | + int mpdconnection::try_recv_idle(int timeout) { | |
110 | + struct pollfd pfd = { m_fd, POLLIN, 0 }; | |
111 | + | |
112 | + int poll_ret = poll(&pfd, 1, timeout); | |
113 | + | |
114 | + if (poll_ret > 0) { | |
115 | + return recv_idle(); | |
116 | + } else if (poll_ret == 0) { | |
117 | + return 0; | |
118 | + } else { | |
119 | + throw mpd_exception("poll() returned error: "s + std::strerror(errno)); | |
120 | + } | |
121 | + } | |
122 | + | |
123 | unique_ptr<mpdstatus> mpdconnection::get_status() { | |
124 | check_prerequisites(); | |
125 | auto status = make_unique<mpdstatus>(this); | |
126 | diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp | |
127 | index 1ba33c81c..9c53378b8 100644 | |
128 | --- a/src/modules/mpd.cpp | |
129 | +++ b/src/modules/mpd.cpp | |
130 | @@ -178,12 +178,20 @@ namespace modules { | |
131 | m_status = m_mpd->get_status_safe(); | |
132 | } | |
133 | ||
134 | + bool track_time = (m_label_time || m_bar_progress) && m_status->match_state(mpdstate::PLAYING); | |
135 | try { | |
136 | m_mpd->idle(); | |
137 | ||
138 | - int idle_flags = 0; | |
139 | - m_mpd->noidle(); | |
140 | - if ((idle_flags = m_mpd->recv_idle()) != 0) { | |
141 | + int idle_flags; | |
142 | + | |
143 | + if (track_time) { | |
144 | + m_mpd->noidle(); | |
145 | + idle_flags = m_mpd->recv_idle(); | |
146 | + } else { | |
147 | + idle_flags = m_mpd->try_recv_idle(2000); | |
148 | + } | |
149 | + | |
150 | + if (idle_flags != 0) { | |
151 | // Update status on every event | |
152 | m_status->update(idle_flags, m_mpd.get()); | |
153 | return true; | |
154 | @@ -194,7 +202,7 @@ namespace modules { | |
155 | return def; | |
156 | } | |
157 | ||
158 | - if ((m_label_time || m_bar_progress) && m_status->match_state(mpdstate::PLAYING)) { | |
159 | + if (track_time) { | |
160 | auto now = chrono::system_clock::now(); | |
161 | auto diff = now - m_lastsync; | |
162 | ||
163 | ||
164 | From 139dca02ae02615cd331d8061ac7c0cae661be25 Mon Sep 17 00:00:00 2001 | |
165 | From: Jan Palus <atler@pld-linux.org> | |
166 | Date: Fri, 25 Dec 2020 01:23:34 +0100 | |
167 | Subject: [PATCH 3/5] feat(mpd): subscribe only to relevant event types | |
168 | ||
169 | --- | |
170 | src/adapters/mpd.cpp | 2 +- | |
171 | 1 file changed, 1 insertion(+), 1 deletion(-) | |
172 | ||
173 | diff --git a/src/adapters/mpd.cpp b/src/adapters/mpd.cpp | |
174 | index 76f824115..b4567e7f2 100644 | |
175 | --- a/src/adapters/mpd.cpp | |
176 | +++ b/src/adapters/mpd.cpp | |
177 | @@ -196,7 +196,7 @@ namespace mpd { | |
178 | void mpdconnection::idle() { | |
179 | check_connection(m_connection.get()); | |
180 | if (!m_idle) { | |
181 | - mpd_send_idle(m_connection.get()); | |
182 | + mpd_send_idle_mask(m_connection.get(), (mpd_idle) (MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS)); | |
183 | check_errors(m_connection.get()); | |
184 | m_idle = true; | |
185 | } | |
186 | ||
187 | From 00eeb73637ff1928c9621fe6008628f04a1cf655 Mon Sep 17 00:00:00 2001 | |
188 | From: Jan Palus <atler@pld-linux.org> | |
189 | Date: Fri, 25 Dec 2020 01:31:04 +0100 | |
190 | Subject: [PATCH 4/5] feat(mpd): minor optimization of happy code path | |
191 | ||
192 | --- | |
193 | src/modules/mpd.cpp | 8 +------- | |
194 | 1 file changed, 1 insertion(+), 7 deletions(-) | |
195 | ||
196 | diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp | |
197 | index 9c53378b8..702fbebf6 100644 | |
198 | --- a/src/modules/mpd.cpp | |
199 | +++ b/src/modules/mpd.cpp | |
200 | @@ -149,13 +149,7 @@ namespace modules { | |
201 | } | |
202 | ||
203 | bool mpd_module::has_event() { | |
204 | - bool def = false; | |
205 | - | |
206 | - if (!connected() && m_statebroadcasted == mpd::connection_state::CONNECTED) { | |
207 | - def = true; | |
208 | - } else if (connected() && m_statebroadcasted == mpd::connection_state::DISCONNECTED) { | |
209 | - def = true; | |
210 | - } | |
211 | + bool def = (m_statebroadcasted == (connected() ? mpd::connection_state::DISCONNECTED : mpd::connection_state::CONNECTED)); | |
212 | ||
213 | try { | |
214 | if (!m_mpd) { | |
215 | ||
216 | From b073db3569d887a1567a387c3ea71989ffede0cc Mon Sep 17 00:00:00 2001 | |
217 | From: Jan Palus <atler@pld-linux.org> | |
218 | Date: Fri, 25 Dec 2020 12:30:10 +0100 | |
219 | Subject: [PATCH 5/5] feat(mpd): slightly modify delay calculation between | |
220 | connection attempts | |
221 | ||
222 | wait 0.5s more on each attempt and cap it at 5s | |
223 | --- | |
224 | include/modules/mpd.hpp | 2 +- | |
225 | src/modules/mpd.cpp | 4 ++-- | |
226 | 2 files changed, 3 insertions(+), 3 deletions(-) | |
227 | ||
228 | diff --git a/include/modules/mpd.hpp b/include/modules/mpd.hpp | |
229 | index 04afc97e7..ed591fe42 100644 | |
230 | --- a/include/modules/mpd.hpp | |
231 | +++ b/include/modules/mpd.hpp | |
232 | @@ -90,7 +90,7 @@ namespace modules { | |
233 | chrono::system_clock::time_point m_lastsync{}; | |
234 | float m_synctime{1.0f}; | |
235 | ||
236 | - int m_quick_attempts{0}; | |
237 | + int m_connect_attempts{0}; | |
238 | ||
239 | // This flag is used to let thru a broadcast once every time | |
240 | // the connection state changes | |
241 | diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp | |
242 | index 702fbebf6..3394f42a9 100644 | |
243 | --- a/src/modules/mpd.cpp | |
244 | +++ b/src/modules/mpd.cpp | |
245 | @@ -141,10 +141,10 @@ namespace modules { | |
246 | ||
247 | void mpd_module::idle() { | |
248 | if (connected()) { | |
249 | - m_quick_attempts = 0; | |
250 | + m_connect_attempts = 0; | |
251 | sleep(80ms); | |
252 | } else { | |
253 | - sleep(m_quick_attempts++ < 5 ? 0.5s : 2s); | |
254 | + sleep(++m_connect_attempts < 10 ? m_connect_attempts * 0.5s : 5s); | |
255 | } | |
256 | } | |
257 |