Skip to content

Commit 4dec96f

Browse files
committed
feat: Reduce the impact on foobar2000 startup time
1 parent 10e71dd commit 4dec96f

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

main.cpp

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using json = nlohmann::json;
1212

1313
DECLARE_COMPONENT_VERSION(
1414
"LDDC Desktop Lyrics",
15-
"0.0.1",
15+
"0.0.2",
1616
"Foobar2000 plugin for displaying lyrics on the desktop."
1717
);
1818

@@ -73,9 +73,9 @@ class LDDCDesktopLyricsInitQuit : public initquit {
7373

7474
void on_init() override {
7575
lddc = new LDDCDesktopLyrics();
76-
lddc->initialize();
7776
play_callback_impl = new play_callback_impl_class(lddc);
7877
play_callback_manager::get()->register_callback(play_callback_impl, play_callback::flag_on_playback_all, true);
78+
lddc->initialize();
7979
}
8080

8181
void on_quit() override {
@@ -93,14 +93,14 @@ class LDDCDesktopLyricsInitQuit : public initquit {
9393

9494
class LDDCDesktopLyrics {
9595
public:
96-
LDDCDesktopLyrics() : sock(INVALID_SOCKET), id(0), version(0), command_thread(nullptr) {}
96+
LDDCDesktopLyrics() : sock(INVALID_SOCKET), id(0), version(0), command_thread(nullptr), start_thread(nullptr) {}
9797

9898
void initialize() {
9999
if (!load_command_line()) {
100100
MessageBox(NULL, L"Failed to load command line from info.json,Please run the LDDC main program at least once", L"Error", MB_OK | MB_ICONERROR);
101101
return;
102102
}
103-
start_service();
103+
start_thread = new std::thread(&LDDCDesktopLyrics::start_service, this);
104104
}
105105

106106
void shutdown() {
@@ -241,7 +241,7 @@ class LDDCDesktopLyricsInitQuit : public initquit {
241241
{"info", {
242242
{"name", "foo_lddc"},
243243
{"repo", "https://github.com/chenmozhijin/foo_lddc"},
244-
{"ver", "0.0.1"},
244+
{"ver", "0.0.2"},
245245
}}
246246
};
247247

@@ -266,6 +266,12 @@ class LDDCDesktopLyricsInitQuit : public initquit {
266266
}
267267

268268
command_thread = new std::thread(&LDDCDesktopLyrics::process_commands, this);
269+
270+
inited = true;
271+
for (json task : tasks) {
272+
send_task(task);
273+
}
274+
tasks.clear();
269275
}
270276

271277
bool connect_to_service(int port) {
@@ -294,12 +300,8 @@ class LDDCDesktopLyricsInitQuit : public initquit {
294300
return true;
295301
}
296302

297-
bool ensure_connected() {
298-
return sock != INVALID_SOCKET;
299-
}
300-
301303
void send_message(const std::string& message) {
302-
if (!ensure_connected()) {
304+
if (sock == INVALID_SOCKET) {
303305
return;
304306
}
305307

@@ -308,13 +310,23 @@ class LDDCDesktopLyricsInitQuit : public initquit {
308310
send(sock, message.c_str(), static_cast<int>(message.length()), 0);
309311
}
310312

313+
void send_task(json& task) {
314+
if (inited) {
315+
task["id"] = id;
316+
send_message(task.dump());
317+
}
318+
else {
319+
tasks.push_back(task);
320+
}
321+
}
322+
311323
bool read_message(std::string& response_str) {
312324
uint32_t length = 0;
313325

314326
// 读取剩余的消息内容
315-
if (!msg_buffer.empty()) {
316-
response_str = msg_buffer;
317-
msg_buffer.clear();
327+
if (!received_msg_buffer.empty()) {
328+
response_str = received_msg_buffer;
329+
received_msg_buffer.clear();
318330
return true;
319331
}
320332

@@ -337,8 +349,8 @@ class LDDCDesktopLyricsInitQuit : public initquit {
337349
std::vector<char> remaining_buffer(remaining_length);
338350
result = recv(sock, remaining_buffer.data(), remaining_length, 0);
339351
if (result > 0) {
340-
// 将剩余内容保存到 msg_buffer
341-
msg_buffer.assign(remaining_buffer.begin(), remaining_buffer.end());
352+
// 将剩余内容保存到 received_msg_buffer
353+
received_msg_buffer.assign(remaining_buffer.begin(), remaining_buffer.end());
342354
}
343355
}
344356

@@ -404,9 +416,13 @@ class LDDCDesktopLyricsInitQuit : public initquit {
404416
SOCKET sock;
405417
int id;
406418
int version;
419+
bool inited = false;
407420
std::string command_line;
408-
std::string msg_buffer; // 用于保存剩余消息的缓冲区
421+
std::string received_msg_buffer; // 用于保存剩余消息的缓冲区
409422
std::thread* command_thread; // 用于处理命令的线程
423+
std::thread* start_thread; // 用于启动服务的线程
424+
std::vector<json> tasks; // 用于保存未发送的task
425+
410426

411427
enum {
412428
REQUIRED_VERSION = 1
@@ -429,7 +445,7 @@ class LDDCDesktopLyricsInitQuit : public initquit {
429445
{"playback_time", static_cast<int>(static_cast<double>(playback_control::get()->playback_get_position()) * 1000)}
430446
};
431447

432-
lddc->send_message(msg.dump());
448+
lddc->send_task(msg);
433449
}
434450

435451
void on_playback_new_track(metadb_handle_ptr p_track) override {
@@ -438,7 +454,6 @@ class LDDCDesktopLyricsInitQuit : public initquit {
438454
file_info_impl info;
439455
if (p_track->get_info(info)) {
440456
json msg = {
441-
{"id", lddc->get_id()},
442457
{"task", "chang_music"},
443458
{"path", p_track->get_path()},
444459
{"duration" , static_cast<int>(info.get_length() * 1000)},
@@ -473,47 +488,44 @@ class LDDCDesktopLyricsInitQuit : public initquit {
473488
msg["track"] = nullptr;
474489
}
475490

476-
lddc->send_message(msg.dump());
491+
lddc->send_task(msg);
477492
}
478493
}
479494

480495
void on_playback_stop(play_control::t_stop_reason p_reason) override {
481496
if (!lddc) return;
482497

483498
json msg = {
484-
{"id", lddc->get_id()},
485499
{"task", "stop"},
486500
{"send_time", get_unix_time()},
487501
{"playback_time", static_cast<int>(static_cast<double>(playback_control::get()->playback_get_position()) * 1000)}
488502
};
489503

490-
lddc->send_message(msg.dump());
504+
lddc->send_task(msg);
491505
}
492506

493507
void on_playback_seek(double p_time) override {
494508
if (!lddc) return;
495509

496510
json msg = {
497-
{"id", lddc->get_id()},
498511
{"task", "sync"},
499512
{"send_time", get_unix_time()},
500513
{"playback_time", static_cast<int>(static_cast<double>(playback_control::get()->playback_get_position()) * 1000)}
501514
};
502515

503-
lddc->send_message(msg.dump());
516+
lddc->send_task(msg);
504517
}
505518

506519
void on_playback_pause(bool p_state) override {
507520
if (!lddc) return;
508521

509522
json msg = {
510-
{"id", lddc->get_id()},
511523
{"task",p_state ? "pause" : "proceed"},
512524
{"send_time", get_unix_time()},
513525
{"playback_time", static_cast<int>(static_cast<double>(playback_control::get()->playback_get_position()) * 1000)}
514526
};
515527

516-
lddc->send_message(msg.dump());
528+
lddc->send_task(msg);
517529
}
518530

519531
void on_playback_edited(metadb_handle_ptr) override {}
@@ -526,13 +538,12 @@ class LDDCDesktopLyricsInitQuit : public initquit {
526538
if (!lddc) return;
527539

528540
json msg = {
529-
{"id", lddc->get_id()},
530541
{"task","sync"},
531542
{"send_time", get_unix_time()},
532543
{"playback_time", static_cast<int>(static_cast<double>(playback_control::get()->playback_get_position()) * 1000)}
533544
};
534545

535-
lddc->send_message(msg.dump());
546+
lddc->send_task(msg);
536547
}
537548

538549
void on_volume_change(float) override {}

0 commit comments

Comments
 (0)