Skip to content

Commit

Permalink
RCORE-2181 Add a testing websocket server
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreams committed Aug 8, 2024
1 parent 13d8264 commit f41c07b
Show file tree
Hide file tree
Showing 9 changed files with 718 additions and 29 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ let notSyncServerSources: [String] = [
"realm/sync/network/default_socket.cpp",
"realm/sync/network/http.cpp",
"realm/sync/network/network.cpp",
"realm/sync/network/network_error.cpp",
"realm/sync/network/network_ssl.cpp",
"realm/sync/network/websocket.cpp",
"realm/sync/noinst/changeset_index.cpp",
Expand Down
2 changes: 2 additions & 0 deletions src/realm/sync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(SYNC_SOURCES
network/default_socket.cpp
network/http.cpp
network/network.cpp
network/network_error.cpp
network/network_ssl.cpp
network/websocket.cpp
)
Expand Down Expand Up @@ -59,6 +60,7 @@ set(SYNC_NETWORK_INSTALL_HEADERS
network/default_socket.hpp
network/http.hpp
network/network.hpp
network/network_error.hpp
network/network_ssl.hpp
network/websocket.hpp
network/websocket_error.hpp
Expand Down
30 changes: 2 additions & 28 deletions src/realm/sync/network/default_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <realm/sync/binding_callback_thread_observer.hpp>
#include <realm/sync/network/network.hpp>
#include <realm/sync/network/network_error.hpp>
#include <realm/sync/network/network_ssl.hpp>
#include <realm/sync/network/websocket.hpp>
#include <realm/util/basic_system_errors.hpp>
Expand Down Expand Up @@ -38,7 +39,7 @@ class DefaultWebSocketImpl final : public DefaultWebSocket, public Config {
{
m_websocket.async_write_binary(data.data(), data.size(),
[write_handler = std::move(handler)](std::error_code ec, size_t) {
write_handler(DefaultWebSocketImpl::get_status_from_util_error(ec));
write_handler(network::get_status_from_network_error(ec));
});
}

Expand Down Expand Up @@ -147,33 +148,6 @@ class DefaultWebSocketImpl final : public DefaultWebSocket, public Config {
return m_observer->websocket_binary_message_received(util::Span<const char>(ptr, size));
}

static Status get_status_from_util_error(std::error_code ec)
{
if (!ec) {
return Status::OK();
}
switch (ec.value()) {
case util::error::operation_aborted:
return {ErrorCodes::Error::OperationAborted, "Write operation cancelled"};
case util::error::address_family_not_supported:
[[fallthrough]];
case util::error::invalid_argument:
return {ErrorCodes::Error::InvalidArgument, ec.message()};
case util::error::no_memory:
return {ErrorCodes::Error::OutOfMemory, ec.message()};
case util::error::connection_aborted:
[[fallthrough]];
case util::error::connection_reset:
[[fallthrough]];
case util::error::broken_pipe:
[[fallthrough]];
case util::error::resource_unavailable_try_again:
return {ErrorCodes::Error::ConnectionClosed, ec.message()};
default:
return {ErrorCodes::Error::UnknownError, ec.message()};
}
}

void initiate_resolve();
void handle_resolve(std::error_code, network::Endpoint::List);
void initiate_tcp_connect(network::Endpoint::List, std::size_t);
Expand Down
3 changes: 2 additions & 1 deletion src/realm/sync/network/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <realm/util/optional.hpp>
#include <realm/util/basic_system_errors.hpp>
#include <realm/util/flat_map.hpp>
#include <realm/util/logger.hpp>
#include <realm/string_data.hpp>

Expand Down Expand Up @@ -158,7 +159,7 @@ class HeterogeneousCaseInsensitiveCompare {
};

/// Case-insensitive map suitable for storing HTTP headers.
using HTTPHeaders = std::map<std::string, std::string, HeterogeneousCaseInsensitiveCompare>;
using HTTPHeaders = util::FlatMap<std::string, std::string, HeterogeneousCaseInsensitiveCompare>;

struct HTTPRequest {
HTTPMethod method = HTTPMethod::Get;
Expand Down
60 changes: 60 additions & 0 deletions src/realm/sync/network/network_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*************************************************************************
*
* Copyright 2024 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#include "realm/sync/network/network_error.hpp"

#include "realm/util/basic_system_errors.hpp"
#include "realm/util/misc_ext_errors.hpp"
#include "realm/sync/network/network_ssl.hpp"

namespace realm::sync::network {

Status get_status_from_network_error(std::error_code ec)
{
if (!ec) {
return Status::OK();
}
if (ec == make_error_code(util::MiscExtErrors::end_of_input)) {
return {ErrorCodes::ConnectionClosed, ec.message()};
}
if (ec == ssl::Errors::tls_handshake_failed) {
return {ErrorCodes::TlsHandshakeFailed, ec.message()};
}
switch (ec.value()) {
case util::error::operation_aborted:
return {ErrorCodes::Error::OperationAborted, "Write operation cancelled"};
case util::error::address_family_not_supported:
[[fallthrough]];
case util::error::invalid_argument:
return {ErrorCodes::Error::InvalidArgument, ec.message()};
case util::error::no_memory:
return {ErrorCodes::Error::OutOfMemory, ec.message()};
case util::error::connection_aborted:
[[fallthrough]];
case util::error::connection_reset:
[[fallthrough]];
case util::error::broken_pipe:
[[fallthrough]];
case util::error::resource_unavailable_try_again:
return {ErrorCodes::Error::ConnectionClosed, ec.message()};
default:
return {ErrorCodes::Error::UnknownError, ec.message()};
}
}

} // namespace realm::sync::network
27 changes: 27 additions & 0 deletions src/realm/sync/network/network_error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*************************************************************************
*
* Copyright 2024 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#pragma once

#include <realm/status.hpp>

namespace realm::sync::network {

Status get_status_from_network_error(std::error_code ec);

} // namespace realm::sync::network
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ if(REALM_ENABLE_SYNC)
test_util_network_ssl.cpp
test_util_uri.cpp
test_util_websocket.cpp
websocket_server.cpp
)

set(SYNC_TEST_HEADERS
Expand All @@ -230,6 +231,7 @@ if(REALM_ENABLE_SYNC)
fuzz_tester.hpp
peer.hpp
sync_fixtures.hpp
websocket_server.hpp
)

file(GLOB SYNC_TEST_RESOURCES
Expand Down
Loading

0 comments on commit f41c07b

Please sign in to comment.