1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (c) 2004-2021 musikcube team 4 // 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions are met: 9 // 10 // * Redistributions of source code must retain the above copyright notice, 11 // this list of conditions and the following disclaimer. 12 // 13 // * Redistributions in binary form must reproduce the above copyright 14 // notice, this list of conditions and the following disclaimer in the 15 // documentation and/or other materials provided with the distribution. 16 // 17 // * Neither the name of the author nor the names of other contributors may 18 // be used to endorse or promote products derived from this software 19 // without specific prior written permission. 20 // 21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 // POSSIBILITY OF SUCH DAMAGE. 32 // 33 ////////////////////////////////////////////////////////////////////////////// 34 35 #pragma once 36 37 #include <musikcore/config.h> 38 39 #pragma warning(push, 0) 40 #include <websocketpp/config/asio_no_tls_client.hpp> 41 #include <websocketpp/config/asio_client.hpp> 42 #include <websocketpp/client.hpp> 43 #pragma warning(pop) 44 45 #include <atomic> 46 #include <memory> 47 #include <functional> 48 #include <system_error> 49 50 namespace musik { namespace core { namespace net { 51 52 class RawWebSocketClient { 53 public: 54 using PlainTextClient = websocketpp::client<websocketpp::config::asio_client>; 55 using PlainTextClientPtr = std::unique_ptr<PlainTextClient>; 56 using TlsClient = websocketpp::client<websocketpp::config::asio_tls_client>; 57 using TlsClientPtr = std::unique_ptr<TlsClient>; 58 using SslContext = std::shared_ptr<boost::asio::ssl::context>; 59 using Message = websocketpp::config::asio_client::message_type::ptr; 60 using Connection = websocketpp::connection_hdl; 61 62 using OpenHandler = std::function<void(Connection)>; 63 using FailHandler = std::function<void(Connection)>; 64 using MessageHandler = std::function<void(Connection, Message)>; 65 using CloseHandler = std::function<void(Connection)>; 66 using SendMessageErrorHandler = std::function<void(std::error_code)>; 67 68 enum class Mode: int { 69 PlainText = 0, 70 TLS = 1 71 }; 72 73 RawWebSocketClient(boost::asio::io_service& io); 74 RawWebSocketClient(const RawWebSocketClient&) = delete; 75 ~RawWebSocketClient(); 76 77 void SetMode(Mode mode); 78 void SetOpenHandler(OpenHandler openHandler); 79 void SetFailHandler(FailHandler failHandler); 80 void SetMessageHandler(MessageHandler messageHandler); 81 void SetCloseHandler(CloseHandler closeHandler); 82 void SetSendMessageErrorHandler(SendMessageErrorHandler errorHandler); 83 void Send(Connection connection, const std::string& message); 84 void SetPongTimeout(long timeoutMs); 85 void Connect(const std::string& uri); 86 void Run(); 87 88 private: 89 90 Mode mode; 91 TlsClientPtr tlsClient; 92 PlainTextClientPtr plainTextClient; 93 SendMessageErrorHandler sendMessageErrorHandler; 94 }; 95 96 } } } 97 98