coming
天行健 君子以自强不息
记录我的成长
文章目录
WebSocketClient类就是负责程序与远程音乐资料库的连接。它定义于src/musikcore/net/WebSocketClient.h,继承自类IMessageTarget,接收程序的请求并与远程音乐资料库交互。
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 99
| class WebSocketClient: public musik::core::runtime::IMessageTarget { public: using ClientPtr = std::unique_ptr<RawWebSocketClient>; using ClientMessage = websocketpp::config::asio_client::message_type::ptr; using Connection = websocketpp::connection_hdl; using Query = std::shared_ptr<musik::core::db::ISerializableQuery>; enum class State: int { Disconnected = 0, Connecting = 1, Authenticating = 2, Connected = 3, Disconnecting = 4, }; enum class QueryError: int { QueryFailed = 1, Disconnected = 2, AuthFailed = 3, QueryNotFound = 4, ParseFailed = 5, }; enum class ConnectionError : int { None = 0, InvalidPassword = 1, IncompatibleVersion = 2, ConnectionFailed = 3, ClosedByServer = 4, }; class Listener { public: using Client = WebSocketClient; using State = Client::State; using QueryError = Client::QueryError; virtual void OnClientInvalidPassword(Client* client) = 0; virtual void OnClientStateChanged(Client* client, State newState, State oldState) = 0; virtual void OnClientQuerySucceeded(Client* client, const std::string& messageId, Query query) = 0; virtual void OnClientQueryFailed(Client* client, const std::string& messageId, Query query, QueryError result) = 0; }; WebSocketClient( musik::core::runtime::IMessageQueue* messageQueue, Listener* listener);
WebSocketClient(const WebSocketClient&) = delete; virtual ~WebSocketClient(); void Connect( const std::string& host, unsigned short port, const std::string& password, bool useTls); void Reconnect(); void Disconnect(); State ConnectionState() const; ConnectionError LastConnectionError() const; std::string LastServerVersion() const; std::string Uri() const; std::string EnqueueQuery(Query query); void SetMessageQueue(musik::core::runtime::IMessageQueue* messageQueue);
void ProcessMessage(musik::core::runtime::IMessage& message) override;
private: void SetState(State state); void InvalidatePendingQueries(); void SendPendingQueries(); void SetDisconnected(ConnectionError errorCode); ClientPtr rawClient; Connection connection; boost::asio::io_service io; std::unique_ptr<std::thread> thread;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| mutable std::recursive_mutex mutex; bool useTls{ false }; std::string uri, password; std::unordered_map<std::string, Query> messageIdToQuery; std::atomic<bool> quit{ false }; ConnectionError connectionError{ ConnectionError::None }; std::string serverVersion; State state{ State::Disconnected }; Listener* listener{ nullptr }; musik::core::runtime::IMessageQueue* messageQueue; };
|
WebSocketClient类用到了类RawWebSocketClient,它是对websocketpp::client<websocketpp::config::asio_client>
与websocketpp::client<websocketpp::config::asio_tls_client>
的封装。
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
| class RawWebSocketClient { public: using PlainTextClient = websocketpp::client<websocketpp::config::asio_client>; using PlainTextClientPtr = std::unique_ptr<PlainTextClient>; using TlsClient = websocketpp::client<websocketpp::config::asio_tls_client>; using TlsClientPtr = std::unique_ptr<TlsClient>; using SslContext = std::shared_ptr<boost::asio::ssl::context>; using Message = websocketpp::config::asio_client::message_type::ptr; using Connection = websocketpp::connection_hdl;
using OpenHandler = std::function<void(Connection)>; using FailHandler = std::function<void(Connection)>; using MessageHandler = std::function<void(Connection, Message)>; using CloseHandler = std::function<void(Connection)>; using SendMessageErrorHandler = std::function<void(std::error_code)>; enum class Mode: int { PlainText = 0, TLS = 1 }; RawWebSocketClient(boost::asio::io_service& io); RawWebSocketClient(const RawWebSocketClient&) = delete; ~RawWebSocketClient(); void SetMode(Mode mode); void SetOpenHandler(OpenHandler openHandler); void SetFailHandler(FailHandler failHandler); void SetMessageHandler(MessageHandler messageHandler); void SetCloseHandler(CloseHandler closeHandler); void SetSendMessageErrorHandler(SendMessageErrorHandler errorHandler); void Send(Connection connection, const std::string& message); void SetPongTimeout(long timeoutMs); void Connect(const std::string& uri); void Run();
private: Mode mode; TlsClientPtr tlsClient; PlainTextClientPtr plainTextClient; SendMessageErrorHandler sendMessageErrorHandler; };
|
WebSocketClient类用到了几个静态函数create*Request
生成各种请求。
本文代表个人观点,内容仅供参考。若有不恰当之处,望不吝赐教!