Realm C++ SDK Version v2.2.0

websocket.hpp

1
2//
3// Copyright 2024 Realm Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19#ifndef CPPREALM_NETWORKING_WEBSOCKET_HPP
20#define CPPREALM_NETWORKING_WEBSOCKET_HPP
21
22#include <cpprealm/internal/bridge/status.hpp>
23#include <cpprealm/internal/bridge/realm.hpp>
24
25#ifndef REALMCXX_VERSION_MAJOR
26#include <cpprealm/version_numbers.hpp>
27#endif
28
29#include <chrono>
30#include <optional>
31
32namespace realm {
33 namespace sync {
34 class SyncSocketProvider;
35 struct WebSocketInterface;
36 struct WebSocketObserver;
37 }
38
39 namespace util {
40 class Logger;
41 }
42}
43
44namespace realm::networking {
45
48 std::vector<std::string> protocols;
50 std::string url;
51 };
52
53 enum class websocket_err_codes {
54 ok = 1000,
55 going_away = 1001,
56 protocol_error = 1002,
57 unsupported_data = 1003,
58 websocket_reserved = 1004,
59 no_status_received = 1005,
60 abnormal_closure = 1006,
61 invalid_payload_data = 1007,
62 policy_violation = 1008,
63 message_too_big = 1009,
64 invalid_extension = 1010,
65 invalid_server_error = 1011,
66 TLS_handshake_failed = 1015,
67
68 unauthorized = 4001,
69 forbidden = 4002,
70 moved_permanently = 4003,
71 client_too_old = 4004,
72 client_too_new = 4005,
73 protocol_mismatch = 4006,
74
75 resolve_failed = 4400,
76 connection_failed = 4401,
77 read_error = 4402,
78 write_error = 4403,
79 retry_error = 4404,
80 fatal_error = 4405,
81 };
82
84 struct websocket_interface;
85 struct websocket_observer;
86
113 public:
114
116 using FunctionHandler = std::function<void(status)>;
117
128 struct timer {
130 virtual ~timer() = default;
133 virtual void cancel() = 0;
134 };
135
137 using sync_timer = std::unique_ptr<sync_socket_provider::timer>;
138
142 virtual ~sync_socket_provider() = default;
143
149 virtual std::unique_ptr<websocket_interface> connect(std::unique_ptr<websocket_observer> observer,
150 websocket_endpoint && endpoint) = 0;
151
174 virtual void post(FunctionHandler&& handler) = 0;
175
190 virtual sync_timer create_timer(std::chrono::milliseconds delay, FunctionHandler&& handler) = 0;
191 };
192
201 virtual ~websocket_interface() = default;
202
203 using FunctionHandler = std::function<void(status)>;
204
215 virtual void async_write_binary(std::string_view data, FunctionHandler&& handler) = 0;
216 };
217
221 virtual ~websocket_observer() = default;
222
229 virtual void websocket_connected_handler(const std::string& protocol) = 0;
230
234 virtual void websocket_error_handler() = 0;
235
247 virtual bool websocket_binary_message_received(std::string_view data) = 0;
248
262 virtual bool websocket_closed_handler(bool was_clean, websocket_err_codes error_code,
263 std::string_view message) = 0;
264 };
265
272 std::optional<std::map<std::string, std::string>> custom_http_headers;
276 std::optional<::realm::internal::bridge::realm::sync_config::proxy_config> proxy_config;
277
278 using SSLVerifyCallback = bool(const std::string& server_address,
279 internal::bridge::realm::sync_config::proxy_config::port_type server_port,
280 const char* pem_data, size_t pem_size, int preverify_ok, int depth);
288 std::optional<std::string> ssl_trust_certificate_path;
294 std::function<SSLVerifyCallback> ssl_verify_callback;
295 };
298 ~default_socket_provider() = default;
299
300 std::unique_ptr<websocket_interface> connect(std::unique_ptr<websocket_observer>, websocket_endpoint &&) override;
301 void post(FunctionHandler&&) override;
302 sync_timer create_timer(std::chrono::milliseconds delay, FunctionHandler&&) override;
303
304 protected:
305 configuration m_configuration;
306 private:
307 void initialize();
308 std::unique_ptr<::realm::sync::SyncSocketProvider> m_provider;
309 };
310}
311
312#endif //CPPREALM_NETWORKING_WEBSOCKET_HPP
Definition: websocket.hpp:112
virtual std::unique_ptr< websocket_interface > connect(std::unique_ptr< websocket_observer > observer, websocket_endpoint &&endpoint)=0
std::unique_ptr< sync_socket_provider::timer > sync_timer
Other class typedefs.
Definition: websocket.hpp:137
std::function< void(status)> FunctionHandler
Function handler typedef.
Definition: websocket.hpp:116
virtual sync_timer create_timer(std::chrono::milliseconds delay, FunctionHandler &&handler)=0
virtual void post(FunctionHandler &&handler)=0
Definition: status.hpp:63
bool client_validate_ssl
Definition: websocket.hpp:284
std::optional<::realm::internal::bridge::realm::sync_config::proxy_config > proxy_config
Definition: websocket.hpp:276
std::function< SSLVerifyCallback > ssl_verify_callback
Definition: websocket.hpp:294
std::optional< std::map< std::string, std::string > > custom_http_headers
Definition: websocket.hpp:272
std::optional< std::string > ssl_trust_certificate_path
Definition: websocket.hpp:288
Built in websocket provider.
Definition: websocket.hpp:267
sync_timer create_timer(std::chrono::milliseconds delay, FunctionHandler &&) override
Definition: websocket.cpp:84
void post(FunctionHandler &&) override
Definition: websocket.cpp:78
std::unique_ptr< websocket_interface > connect(std::unique_ptr< websocket_observer >, websocket_endpoint &&) override
Definition: websocket.cpp:71
virtual ~timer()=default
Cancels the timer and destroys the timer instance.
Definition: websocket.hpp:46
std::vector< std::string > protocols
Array of one or more websocket protocols.
Definition: websocket.hpp:48
std::string url
The websocket url to connect to.
Definition: websocket.hpp:50
Definition: websocket.hpp:198
virtual void async_write_binary(std::string_view data, FunctionHandler &&handler)=0
Definition: websocket.hpp:220
virtual void websocket_connected_handler(const std::string &protocol)=0
virtual bool websocket_binary_message_received(std::string_view data)=0
virtual bool websocket_closed_handler(bool was_clean, websocket_err_codes error_code, std::string_view message)=0