coming
天行健 君子以自强不息
记录我的成长
文章目录
该类管理远程的音乐资料库并提供编辑远程音乐资料库的接口,定义于src/musikcore/library/RemoteLibrary.h,继承了ILibrary、IMessageTarget、Listener、IResourceLocator类,并通过继承模板类std::enable_shared_from_this使得this指针成为shared_ptr指针。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| class RemoteLibrary : public ILibrary, public mymusic::core::runtime::IMessageTarget, public std::enable_shared_from_this<RemoteLibrary>, public mymusic::core::net::WebSocketClient::Listener, public ILibrary::IResourceLocator { public: using Client = mymusic::core::net::WebSocketClient; using Query = std::shared_ptr<mymusic::core::db::ISerializableQuery>; using MessageQueue = mymusic::core::runtime::IMessageQueue; static ILibraryPtr Create(std::string name, int id, MessageQueue* messageQueue); RemoteLibrary(std::string name, int id, MessageQueue* messageQueue); RemoteLibrary(const RemoteLibrary&) = delete; virtual ~RemoteLibrary();
int Enqueue(QueryPtr query, Callback = Callback()) override; int EnqueueAndWait(QueryPtr query, size_t timeoutMs = kWaitIndefinite, Callback = Callback()) override; mymusic::core::IIndexer *Indexer() override; int Id() override; const std::string& Name() override; void SetMessageQueue(mymusic::core::runtime::IMessageQueue& queue) override; mymusic::core::runtime::IMessageQueue& GetMessageQueue() noexcept override { return *messageQueue; } ILibrary::IResourceLocator& GetResourceLocator() noexcept override { return *this; } bool IsConfigured() override; ConnectionState GetConnectionState() const override { return this->connectionState; } Type GetType() const noexcept override { return Type::Remote; } void Close() override;
void ProcessMessage(mymusic::core::runtime::IMessage &message) override;
void OnClientInvalidPassword(Client* client) override; void OnClientStateChanged(Client* client, State newState, State oldState) override; void OnClientQuerySucceeded(Client* client, const std::string& messageId, Query query) override; void OnClientQueryFailed(Client* client, const std::string& messageId, Query query, Client::QueryError reason) override;
std::string GetTrackUri(mymusic::core::sdk::ITrack* track, const std::string& defaultUri) override;
void ReloadConnectionFromPreferences(); const mymusic::core::net::WebSocketClient& WebSocketClient() const;
private: class QueryCompletedMessage;
struct QueryContext { std::shared_ptr<mymusic::core::db::ISerializableQuery> query; Callback callback; };
using QueryContextPtr = std::shared_ptr<QueryContext>; using QueryList = std::list<QueryContextPtr>; void RunQuery(QueryContextPtr context); void RunQueryOnLoopback(QueryContextPtr context); void RunQueryOnWebSocketClient(QueryContextPtr context); void OnQueryCompleted(const std::string& messageId, Query query); void OnQueryCompleted(QueryContextPtr context); void NotifyQueryCompleted(QueryContextPtr context); bool IsQueryInFlight(Query query); void ThreadProc();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| QueryContextPtr GetNextQuery(); QueryList queryQueue; mymusic::core::runtime::IMessageQueue* messageQueue; mymusic::core::net::WebSocketClient wsc; std::string identifier; int id; std::string name; std::unordered_map<std::string, QueryContextPtr> queriesInFlight; std::unique_ptr<std::thread> thread; std::condition_variable_any queueCondition, syncQueryCondition; std::recursive_mutex queueMutex; std::atomic<ConnectionState> connectionState{ ConnectionState::Disconnected }; std::atomic<bool> exit; };
|
本文代表个人观点,内容仅供参考。若有不恰当之处,望不吝赐教!