Skip to content

Commit

Permalink
Networking checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Jul 5, 2024
1 parent 40cdada commit 6263fc0
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 332 deletions.
41 changes: 0 additions & 41 deletions include/cpprealm/internal/generic_network_transport.hpp

This file was deleted.

50 changes: 20 additions & 30 deletions include/cpprealm/networking/networking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,12 @@ namespace realm::networking {

};

// struct websocket_endpoint {
// // using port_type = std::uint_fast16_t;
// // /// Host address
// // std::string address;
// // /// Host port number
// // port_type port;
// // /// Includes access token in query.
// // std::string path;
// // /// Array of one or more websocket protocols
// std::vector<std::string> protocols;
// // /// true if SSL should be used
// // bool is_ssl;
// std::string url;
// /// Optional proxy config
// std::optional<internal::bridge::realm::sync_config::proxy_config> proxy_configuration;
// };

struct websocket_endpoint {
using port_type = std::uint_fast16_t;
/// Host address
std::string address;
/// Host port number
port_type port;
/// Includes access token in query.
std::string path;
/// Array of one or more websocket protocols
/// Array of one or more websocket protocols.
std::vector<std::string> protocols;
/// true if SSL should be used
bool is_ssl;
/// Optional proxy config
/// The websocket url to connect to.
std::string url;
/// Optional proxy config taken in from `realm::App::config`.
std::optional<internal::bridge::realm::sync_config::proxy_config> proxy_configuration;
};

Expand Down Expand Up @@ -157,12 +133,25 @@ namespace realm::networking {
RLM_ERR_WEBSOCKET_FATAL_ERROR = 4405,
};

// Interface for providing http transport
// Interface for providing http transport
struct http_transport_client {
virtual ~http_transport_client() = default;
virtual void send_request_to_server(const request& request,
std::function<void(const response&)>&& completion) = 0;

void set_proxy_configuration(::realm::internal::bridge::realm::sync_config::proxy_config proxy_config) {
m_proxy_config = proxy_config;
};

void set_custom_http_headers(std::map<std::string, std::string> http_headers) {
m_custom_http_headers = http_headers;
};

protected:
std::optional<std::map<std::string, std::string>> m_custom_http_headers;
std::optional<::realm::internal::bridge::realm::sync_config::proxy_config> m_proxy_config;
};

} //namespace realm::networking

namespace realm {
Expand All @@ -183,7 +172,8 @@ namespace realm::internal::networking {
::realm::app::Response to_core_response(const ::realm::networking::response&);

::realm::sync::WebSocketEndpoint to_core_websocket_endpoint(const ::realm::networking::websocket_endpoint & ep);
::realm::networking::websocket_endpoint to_websocket_endpoint(const ::realm::sync::WebSocketEndpoint& ep);
::realm::networking::websocket_endpoint to_websocket_endpoint(const ::realm::sync::WebSocketEndpoint& ep,
std::optional<::realm::internal::bridge::realm::sync_config::proxy_config> pc);
} //namespace realm::internal::networking

#endif//CPPREALM_NETWORKING_HPP
32 changes: 12 additions & 20 deletions include/cpprealm/networking/platform_networking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define CPPREALM_PLATFORM_NETWORKING_HPP

#include <cpprealm/internal/bridge/status.hpp>
#include <cpprealm/internal/generic_network_transport.hpp>
#include <cpprealm/networking/networking.hpp>

#ifndef REALMCXX_VERSION_MAJOR
#include <cpprealm/version_numbers.hpp>
Expand All @@ -42,15 +42,6 @@ namespace realm {

namespace realm::networking {

/**
* Class used for setting a callback which is used
* to pass network configuration options to the sync_socket_provider
* before the websocket establishes a connection.
*/
struct websocket_event_handler {
std::function<websocket_endpoint(websocket_endpoint&&)> on_connect;
};

/// The WebSocket base class that is used by the SyncClient to send data over the
/// WebSocket connection with the server. This is the class that is returned by
/// SyncSocketProvider::connect() when a connection to an endpoint is requested.
Expand Down Expand Up @@ -233,6 +224,14 @@ namespace realm::networking {
std::shared_ptr<http_transport_client> make_http_client();
void set_http_client_factory(std::function<std::shared_ptr<http_transport_client>()>&&);

struct default_http_transport : public ::realm::networking::http_transport_client {
default_http_transport() = default;
~default_http_transport() = default;

void send_request_to_server(const ::realm::networking::request& request,
std::function<void(const ::realm::networking::response&)>&& completion);
};

struct default_socket : public websocket_interface {
default_socket(std::unique_ptr<::realm::sync::WebSocketInterface>&&);
~default_socket() = default;
Expand All @@ -243,31 +242,24 @@ namespace realm::networking {
std::shared_ptr<::realm::sync::WebSocketInterface> m_ws_interface;
};


struct default_socket_provider : public sync_socket_provider {

default_socket_provider();

~default_socket_provider() = default;

std::unique_ptr<websocket_interface> connect(std::unique_ptr<websocket_observer>, websocket_endpoint &&) override;

void post(FunctionHandler&&) override;

SyncTimer create_timer(std::chrono::milliseconds delay, FunctionHandler&&) override;

private:
std::shared_ptr<::realm::sync::SyncSocketProvider> m_provider;
};
}

namespace realm::internal::networking {
std::shared_ptr<realm::sync::SyncSocketProvider> default_sync_socket_provider_factory(const std::shared_ptr<util::Logger>& logger,
const std::string& user_agent_binding_info,
const std::string& user_agent_application_info);

std::shared_ptr<app::GenericNetworkTransport> create_http_client_shim(const std::shared_ptr<::realm::networking::http_transport_client>&);

std::unique_ptr<::realm::sync::SyncSocketProvider> create_sync_socket_provider_shim(const std::shared_ptr<::realm::networking::sync_socket_provider>& provider);
std::unique_ptr<::realm::sync::SyncSocketProvider> create_sync_socket_provider_shim(const std::shared_ptr<::realm::networking::sync_socket_provider>& provider,
std::optional<internal::bridge::realm::sync_config::proxy_config> proxy);
}

#endif//CPPREALM_PLATFORM_NETWORKING_HPP
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ set(HEADERS
../include/cpprealm/internal/bridge/timestamp.hpp
../include/cpprealm/internal/bridge/utils.hpp
../include/cpprealm/internal/bridge/uuid.hpp
../include/cpprealm/internal/generic_network_transport.hpp
../include/cpprealm/internal/type_info.hpp
../include/cpprealm/networking/networking.hpp
../include/cpprealm/networking/platform_networking.hpp
Expand Down
4 changes: 2 additions & 2 deletions src/cpprealm/analytics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#endif

#include <cpprealm/app.hpp>
#include <cpprealm/internal/generic_network_transport.hpp>
#include <cpprealm/networking/platform_networking.hpp>

#include <realm/object-store/sync/generic_network_transport.hpp>
#include <realm/util/base64.hpp>
Expand Down Expand Up @@ -273,7 +273,7 @@ namespace realm {
std::stringstream json_ss;
json_ss << post_data;
auto json_str = json_ss.str();
auto transport = std::make_unique<internal::networking::DefaultTransport>();
auto transport = std::make_unique<networking::default_http_transport>();

std::vector<char> buffer;
buffer.resize(5000);
Expand Down
23 changes: 12 additions & 11 deletions src/cpprealm/app.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <cpprealm/app.hpp>
#include <cpprealm/internal/bridge/status.hpp>
#include <cpprealm/internal/generic_network_transport.hpp>
#include <cpprealm/networking/platform_networking.hpp>

#ifndef REALMCXX_VERSION_MAJOR
Expand Down Expand Up @@ -494,22 +493,24 @@ namespace realm {
client_config.user_agent_application_info = config.app_id;
app_config.app_id = config.app_id;

// if (config.websocket_event_handler) {
// auto websocket_provider = ::realm::internal::networking::default_sync_socket_provider_factory(util::Logger::get_default_logger(),
// client_config.user_agent_binding_info,
// client_config.user_agent_application_info,
// config.websocket_event_handler);
// client_config.socket_provider = websocket_provider;
// }
// Websocket provider configuration
if (config.sync_socket_provider) {
client_config.socket_provider = ::realm::internal::networking::create_sync_socket_provider_shim(config.sync_socket_provider);
client_config.socket_provider = ::realm::internal::networking::create_sync_socket_provider_shim(config.sync_socket_provider,
config.proxy_configuration);
}

// HTTP Transport configuration
if (config.http_transport_client) {
app_config.transport = internal::networking::create_http_client_shim(config.http_transport_client);
} else {
app_config.transport = internal::networking::create_http_client_shim(std::make_shared<internal::networking::DefaultTransport>(config.custom_http_headers,
config.proxy_configuration));
app_config.transport = internal::networking::create_http_client_shim(std::make_shared<networking::default_http_transport>());
}

if (config.proxy_configuration) {
config.http_transport_client->set_proxy_configuration(*config.proxy_configuration);
}
if (config.custom_http_headers) {
config.http_transport_client->set_custom_http_headers(*config.custom_http_headers);
}

app_config.base_url = config.base_url;
Expand Down
18 changes: 6 additions & 12 deletions src/cpprealm/internal/curl/network_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
////////////////////////////////////////////////////////////////////////////

#include <cpprealm/app.hpp>
#include <cpprealm/internal/generic_network_transport.hpp>
#include <cpprealm/networking/platform_networking.hpp>

#include <realm/object-store/sync/generic_network_transport.hpp>

#include <curl/curl.h>

namespace realm::internal::networking {
namespace realm::networking {

namespace {

Expand Down Expand Up @@ -90,7 +90,7 @@ namespace realm::internal::networking {
}

static ::realm::networking::response do_http_request(const ::realm::networking::request& request,
const std::optional<bridge::realm::sync_config::proxy_config>& proxy_config = std::nullopt)
const std::optional<internal::bridge::realm::sync_config::proxy_config>& proxy_config = std::nullopt)
{
CurlGlobalGuard curl_global_guard;
auto curl = curl_easy_init();
Expand Down Expand Up @@ -168,14 +168,8 @@ namespace realm::internal::networking {
}
} // namespace

DefaultTransport::DefaultTransport(const std::optional<std::map<std::string, std::string>>& custom_http_headers,
const std::optional<bridge::realm::sync_config::proxy_config>& proxy_config) {
m_custom_http_headers = custom_http_headers;
m_proxy_config = proxy_config;
}

void DefaultTransport::send_request_to_server(const ::realm::networking::request& request,
std::function<void(const ::realm::networking::response&)>&& completion_block) {
void default_http_transport::send_request_to_server(const ::realm::networking::request& request,
std::function<void(const ::realm::networking::response&)>&& completion_block) {
{
if (m_custom_http_headers) {
auto req_copy = request;
Expand All @@ -186,4 +180,4 @@ namespace realm::internal::networking {
completion_block(do_http_request(request, m_proxy_config));
}
}
} // namespace realm::internal::networking
} // namespace realm::networking
Loading

0 comments on commit 6263fc0

Please sign in to comment.