From f830a8023d10dfdf8572c805344ec8022d183af5 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sat, 4 Apr 2020 01:38:37 +0200 Subject: [PATCH] Updated some small stuff --- server/src/client/command_handler/misc.cpp | 2 +- server/src/client/command_handler/music.cpp | 82 ++++++++++++++------- server/src/music/MusicPlaylist.cpp | 2 +- 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/server/src/client/command_handler/misc.cpp b/server/src/client/command_handler/misc.cpp index 354f9a1..4a5d189 100644 --- a/server/src/client/command_handler/misc.cpp +++ b/server/src/client/command_handler/misc.cpp @@ -2342,7 +2342,7 @@ command_result ConnectedClient::handleCommandDummy_IpChange(ts::Command &cmd) { if(geoloc::provider) { auto loc = this->isAddressV4() ? geoloc::provider->resolveInfoV4(this->getPeerIp(), false) : geoloc::provider->resolveInfoV6(this->getPeerIp(), false); if(loc) { - logError(this->getServerId(), "[{}] Received new ip location. IP {} traced to {} ({}).", CLIENT_STR_LOG_PREFIX, this->getLoggingPeerIp(), loc->name, loc->identifier); + logMessage(this->getServerId(), "[{}] Received new ip location. IP {} traced to {} ({}).", CLIENT_STR_LOG_PREFIX, this->getLoggingPeerIp(), loc->name, loc->identifier); this->properties()[property::CLIENT_COUNTRY] = loc->identifier; server->notifyClientPropertyUpdates(_this.lock(), deque{property::CLIENT_COUNTRY}); new_country = loc->identifier; diff --git a/server/src/client/command_handler/music.cpp b/server/src/client/command_handler/music.cpp index 1b8e161..d2f620b 100644 --- a/server/src/client/command_handler/music.cpp +++ b/server/src/client/command_handler/music.cpp @@ -801,6 +801,44 @@ command_result ConnectedClient::handleCommandPlaylistClientDelPerm(ts::Command & return command_result{error::ok}; } +constexpr auto max_song_meta_info = 1024 * 512; + +inline size_t estimated_song_info_size(const std::shared_ptr& song, bool extract_metadata) { + return 128 + std::min(song->metadata.json_string.length(), (size_t) max_song_meta_info) + extract_metadata * 256; +} + +inline void fill_song_info(ts::command_builder_bulk bulk, const std::shared_ptr& song, bool extract_metadata) { + bulk.reserve(estimated_song_info_size(song, extract_metadata)); + + bulk.put("song_id", song->song_id); + bulk.put("song_invoker", song->invoker); + bulk.put("song_previous_song_id", song->previous_song_id); + bulk.put("song_url", song->original_url); + bulk.put("song_url_loader", song->url_loader); + bulk.put("song_loaded", song->metadata.is_loaded()); + if(song->metadata.json_string.length() > 1024 * 1024 * 512) { + logWarning(LOG_GENERAL, "Dropping song metadata because its way to big. ({}bytes)", song->metadata.json_string.size()); + } else { + bulk.put("song_metadata", song->metadata.json_string); + } + + if(extract_metadata) { + bulk.reserve(256, true); + auto metadata = song->metadata.loaded_data; + if(extract_metadata && song->metadata.is_loaded() && metadata) { + bulk.put("song_metadata_title", metadata->title); + bulk.put("song_metadata_description", metadata->description); + bulk.put("song_metadata_url", metadata->url); + bulk.put("song_metadata_length", metadata->length.count()); + if(auto thumbnail = static_pointer_cast<::music::ThumbnailUrl>(metadata->thumbnail); thumbnail && thumbnail->type() == ::music::THUMBNAIL_URL) { + bulk.put("song_metadata_thumbnail_url", thumbnail->url()); + } else { + bulk.put("song_metadata_thumbnail_url", "none"); + } + } + } +} + command_result ConnectedClient::handleCommandPlaylistSongList(ts::Command &cmd) { CMD_REF_SERVER(ref_server); CMD_RESET_IDLE; @@ -816,39 +854,29 @@ command_result ConnectedClient::handleCommandPlaylistSongList(ts::Command &cmd) if(songs.empty()) return command_result{error::database_empty_result}; - Command notify(this->notify_response_command("notifyplaylistsonglist")); - notify["playlist_id"] = playlist->playlist_id(); - + ts::command_builder result{this->notify_response_command("notifyplaylistsonglist")}; + result.put(0, "version", 2); /* to signalize that we're sending the response bulked */ auto extract_metadata = cmd.hasParm("extract-metadata"); - size_t index = 0; + size_t index{0}; for(const auto& song : songs) { - notify[index]["song_id"] = song->song_id; - notify[index]["song_invoker"] = song->invoker; - notify[index]["song_previous_song_id"] = song->previous_song_id; - notify[index]["song_url"] = song->original_url; - notify[index]["song_url_loader"] = song->url_loader; - notify[index]["song_loaded"] = song->metadata.is_loaded(); - notify[index]["song_metadata"] = song->metadata.json_string; - - if(extract_metadata) { - auto metadata = song->metadata.loaded_data; - if(extract_metadata && song->metadata.is_loaded() && metadata) { - notify[index]["song_metadata_title"] = metadata->title; - notify[index]["song_metadata_description"] = metadata->description; - notify[index]["song_metadata_url"] = metadata->url; - notify[index]["song_metadata_length"] = metadata->length.count(); - if(auto thumbnail = static_pointer_cast<::music::ThumbnailUrl>(metadata->thumbnail); thumbnail && thumbnail->type() == ::music::THUMBNAIL_URL) { - notify[index]["song_metadata_thumbnail_url"] = thumbnail->url(); - } else { - notify[index]["song_metadata_thumbnail_url"] = "none"; - } - } + if(index == 0) { + result.put(0, "playlist_id", playlist->playlist_id()); } + fill_song_info(result.bulk(index), song, extract_metadata); - index++; + if(this->getExternalType() == ClientType::CLIENT_TEAMSPEAK && result.current_size() + estimated_song_info_size(song, extract_metadata) > 128 * 1024) { + this->sendCommand(result); + result.reset(); + index = 0; + } else { + index++; + } } - this->sendCommand(notify); + if(index > 0) + this->sendCommand(result); + if(this->getExternalType() == ClientType::CLIENT_TEAMSPEAK) + this->sendCommand(ts::command_builder{"notifyplaylistsonglistfinished"}); return command_result{error::ok}; } diff --git a/server/src/music/MusicPlaylist.cpp b/server/src/music/MusicPlaylist.cpp index 2787502..5be44f3 100644 --- a/server/src/music/MusicPlaylist.cpp +++ b/server/src/music/MusicPlaylist.cpp @@ -351,7 +351,7 @@ void Playlist::destroy_tree() { } } -std::deque > Playlist::list_songs() { +std::deque> Playlist::list_songs() { unique_lock list_lock(this->playlist_lock); return this->_list_songs(list_lock); }