|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <memory> |
| 8 | +#include <functional> |
| 9 | +#include "signalrclient/connection_state.h" |
| 10 | +#include "signalrclient/trace_level.h" |
| 11 | +#include "signalrclient/log_writer.h" |
| 12 | +#include "signalrclient/signalr_client_config.h" |
| 13 | +#include "signalrclient/transfer_format.h" |
| 14 | +#include "signalrclient/http_client.h" |
| 15 | +#include "signalrclient/websocket_client.h" |
| 16 | + |
| 17 | +namespace signalr |
| 18 | +{ |
| 19 | + class connection_impl; |
| 20 | + |
| 21 | + class connection |
| 22 | + { |
| 23 | + public: |
| 24 | + typedef std::function<void(std::string&&)> message_received_handler; |
| 25 | + |
| 26 | + explicit connection(const std::string& url, trace_level trace_level = trace_level::info, std::shared_ptr<log_writer> log_writer = nullptr, |
| 27 | + std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory = nullptr, |
| 28 | + std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory = nullptr, bool skip_negotiation = false); |
| 29 | + |
| 30 | + ~connection(); |
| 31 | + |
| 32 | + connection(const connection&) = delete; |
| 33 | + |
| 34 | + connection& operator=(const connection&) = delete; |
| 35 | + |
| 36 | + void start(std::function<void(std::exception_ptr)> callback) noexcept; |
| 37 | + |
| 38 | + void send(const std::string& data, transfer_format transfer_format, std::function<void(std::exception_ptr)> callback) noexcept; |
| 39 | + |
| 40 | + void set_message_received(const message_received_handler& message_received_callback); |
| 41 | + void set_disconnected(const std::function<void(std::exception_ptr)>& disconnected_callback); |
| 42 | + |
| 43 | + void set_client_config(const signalr_client_config& config); |
| 44 | + |
| 45 | + void stop(std::function<void(std::exception_ptr)> callback, std::exception_ptr exception) noexcept; |
| 46 | + |
| 47 | + connection_state get_connection_state() const noexcept; |
| 48 | + std::string get_connection_id() const; |
| 49 | + |
| 50 | + private: |
| 51 | + // The recommended smart pointer to use when doing pImpl is the `std::unique_ptr`. However |
| 52 | + // we are capturing the m_pImpl instance in the lambdas used by tasks which can outlive |
| 53 | + // the connection instance. Using `std::shared_ptr` guarantees that we won't be using |
| 54 | + // a deleted object if the task is run after the `connection` instance goes away. |
| 55 | + std::shared_ptr<connection_impl> m_pImpl; |
| 56 | + }; |
| 57 | +} |
0 commit comments