-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathconnection_impl.h
94 lines (75 loc) · 4.83 KB
/
connection_impl.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#pragma once
#include <mutex>
#include <atomic>
#include "signalrclient/http_client.h"
#include "signalrclient/trace_level.h"
#include "signalrclient/connection_state.h"
#include "signalrclient/signalr_client_config.h"
#include "transport_factory.h"
#include "logger.h"
#include "negotiation_response.h"
#include "cancellation_token_source.h"
namespace signalr
{
class websocket_client;
// Note:
// Factory methods and private constructors prevent from using this class incorrectly. Because this class
// derives from `std::enable_shared_from_this` the instance has to be owned by a `std::shared_ptr` whenever
// a member method calls `std::shared_from_this()` otherwise the behavior is undefined. Therefore constructors
// are private to disallow creating instances directly and factory methods return `std::shared_ptr<connection_impl>`.
class connection_impl : public std::enable_shared_from_this<connection_impl>
{
public:
static std::shared_ptr<connection_impl> create(const std::string& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory, std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory, bool skip_negotiation = false);
connection_impl(const connection_impl&) = delete;
connection_impl& operator=(const connection_impl&) = delete;
~connection_impl();
void start(transfer_format transfer_format, std::function<void(std::exception_ptr)> callback) noexcept;
void send(const std::string &data, std::function<void(std::exception_ptr)> callback) noexcept;
void stop(std::function<void(std::exception_ptr)> callback, std::exception_ptr exception) noexcept;
connection_state get_connection_state() const noexcept;
std::string get_connection_id() const noexcept;
void set_message_received(const std::function<void(std::string&&)>& message_received);
void set_disconnected(const std::function<void(std::exception_ptr)>& disconnected);
void set_client_config(const signalr_client_config& config);
private:
std::shared_ptr<scheduler> m_scheduler;
std::string m_base_url;
std::atomic<connection_state> m_connection_state;
logger m_logger;
std::shared_ptr<transport> m_transport;
std::unique_ptr<transport_factory> m_transport_factory;
bool m_skip_negotiation;
std::exception_ptr m_stop_error;
std::function<void(std::string&&)> m_message_received;
std::function<void(std::exception_ptr)> m_disconnected;
signalr_client_config m_signalr_client_config;
transfer_format m_transfer_format;
std::shared_ptr<cancellation_token_source> m_disconnect_cts;
std::mutex m_stop_lock;
cancellation_token_source m_start_completed_event;
std::string m_connection_id;
std::string m_connection_token;
std::function<std::shared_ptr<http_client>(const signalr_client_config&)> m_http_client_factory;
connection_impl(const std::string& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory, std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory, bool skip_negotiation);
void start_transport(const std::string& url, std::function<void(std::shared_ptr<transport>, std::exception_ptr)> callback);
void send_connect_request(const std::shared_ptr<transport>& transport,
const std::string& url, std::function<void(std::exception_ptr)> callback);
void start_negotiate(const std::string& url, std::function<void(std::exception_ptr)> callback);
void start_negotiate_internal(const std::string& url, int redirect_count, std::function<void(std::shared_ptr<transport> transport, std::exception_ptr)> callback);
void process_response(std::string&& response);
void shutdown(std::function<void(std::exception_ptr)> callback, bool is_dtor = false);
void stop_connection(std::exception_ptr);
bool change_state(connection_state old_state, connection_state new_state);
connection_state change_state(connection_state new_state);
void handle_connection_state_change(connection_state old_state, connection_state new_state);
void invoke_message_received(std::string&& message);
static std::string translate_connection_state(connection_state state);
void ensure_disconnected(const std::string& error_message) const;
};
}