config.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 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. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #ifndef REALM_SYNC_CONFIG_HPP
  19. #define REALM_SYNC_CONFIG_HPP
  20. #include <realm/util/assert.hpp>
  21. #include <realm/util/optional.hpp>
  22. #include <realm/util/network.hpp>
  23. #include <functional>
  24. #include <memory>
  25. #include <string>
  26. #include <map>
  27. #include <array>
  28. #include <system_error>
  29. #include <unordered_map>
  30. namespace realm {
  31. class Group;
  32. class SyncUser;
  33. class SyncSession;
  34. namespace bson {
  35. class Bson;
  36. }
  37. struct SyncError {
  38. std::error_code error_code;
  39. std::string message;
  40. bool is_fatal;
  41. std::unordered_map<std::string, std::string> user_info;
  42. /// The sync server may send down an error that the client does not recognize,
  43. /// whether because of a version mismatch or an oversight. It is still valuable
  44. /// to expose these errors so that users can do something about them.
  45. bool is_unrecognized_by_client = false;
  46. SyncError(std::error_code error_code, std::string message, bool is_fatal)
  47. : error_code(std::move(error_code))
  48. , message(std::move(message))
  49. , is_fatal(is_fatal)
  50. {
  51. }
  52. static constexpr const char c_original_file_path_key[] = "ORIGINAL_FILE_PATH";
  53. static constexpr const char c_recovery_file_path_key[] = "RECOVERY_FILE_PATH";
  54. /// The error is a client error, which applies to the client and all its sessions.
  55. bool is_client_error() const;
  56. /// The error is a protocol error, which may either be connection-level or session-level.
  57. bool is_connection_level_protocol_error() const;
  58. /// The error is a connection-level protocol error.
  59. bool is_session_level_protocol_error() const;
  60. /// The error indicates a client reset situation.
  61. bool is_client_reset_requested() const;
  62. };
  63. using SyncSessionErrorHandler = void(std::shared_ptr<SyncSession>, SyncError);
  64. enum class ClientResyncMode : unsigned char {
  65. // Enable automatic client resync without local transaction recovery
  66. DiscardLocal = 1,
  67. // Fire a client reset error
  68. Manual = 2,
  69. };
  70. enum class ReconnectMode {
  71. /// This is the mode that should always be used in production. In this
  72. /// mode the client uses a scheme for determining a reconnect delay that
  73. /// prevents it from creating too many connection requests in a short
  74. /// amount of time (i.e., a server hammering protection mechanism).
  75. normal,
  76. /// For testing purposes only.
  77. ///
  78. /// Never reconnect automatically after the connection is closed due to
  79. /// an error. Allow immediate reconnect if the connection was closed
  80. /// voluntarily (e.g., due to sessions being abandoned).
  81. ///
  82. /// In this mode, Client::cancel_reconnect_delay() and
  83. /// Session::cancel_reconnect_delay() can still be used to trigger
  84. /// another reconnection attempt (with no delay) after an error has
  85. /// caused the connection to be closed.
  86. testing
  87. };
  88. enum class SyncSessionStopPolicy {
  89. Immediately, // Immediately stop the session as soon as all Realms/Sessions go out of scope.
  90. LiveIndefinitely, // Never stop the session.
  91. AfterChangesUploaded, // Once all Realms/Sessions go out of scope, wait for uploads to complete and stop.
  92. };
  93. struct SyncConfig {
  94. struct ProxyConfig {
  95. using port_type = util::network::Endpoint::port_type;
  96. enum class Type { HTTP, HTTPS } type;
  97. std::string address;
  98. port_type port;
  99. };
  100. using SSLVerifyCallback = bool(const std::string& server_address, ProxyConfig::port_type server_port,
  101. const char* pem_data, size_t pem_size, int preverify_ok, int depth);
  102. std::shared_ptr<SyncUser> user;
  103. std::string partition_value;
  104. SyncSessionStopPolicy stop_policy = SyncSessionStopPolicy::AfterChangesUploaded;
  105. std::function<SyncSessionErrorHandler> error_handler;
  106. bool client_validate_ssl = true;
  107. util::Optional<std::string> ssl_trust_certificate_path;
  108. std::function<SSLVerifyCallback> ssl_verify_callback;
  109. util::Optional<ProxyConfig> proxy_config;
  110. // If true, upload/download waits are canceled on any sync error and not just fatal ones
  111. bool cancel_waits_on_nonfatal_error = false;
  112. util::Optional<std::string> authorization_header_name;
  113. std::map<std::string, std::string> custom_http_headers;
  114. // The name of the directory which Realms should be backed up to following
  115. // a client reset
  116. util::Optional<std::string> recovery_directory;
  117. ClientResyncMode client_resync_mode = ClientResyncMode::DiscardLocal;
  118. explicit SyncConfig(std::shared_ptr<SyncUser> user, bson::Bson partition);
  119. explicit SyncConfig(std::shared_ptr<SyncUser> user, std::string partition);
  120. explicit SyncConfig(std::shared_ptr<SyncUser> user, const char* partition);
  121. };
  122. } // namespace realm
  123. #endif // REALM_SYNC_CONFIG_HPP