Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulling changes from unreallink #456

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion rd-cpp/src/rd_core_cpp/src/main/std/to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
#include <atomic>
#include <future>
#include <locale>
#if defined(_MSC_VER) || defined(__APPLE__)
#include <codecvt>
#else
#include <limits>
#include <iconv.h>
#endif

#include <thirdparty.hpp>

Expand All @@ -29,12 +34,63 @@ inline std::string to_string(const char* val)
return val;
}

#if defined(_MSC_VER) || defined(__APPLE__)
template<class I, class E, class S>
struct codecvt : std::codecvt<I, E, S>
{
~codecvt()
{ }
};

inline std::string to_string(std::wstring const& val)
{
using convert_type = std::codecvt_utf8<wchar_t>;
#if defined(__APPLE__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
using convert_type = codecvt<wchar_t, char, std::mbstate_t>;
std::wstring_convert<convert_type> converter;
return converter.to_bytes(val);
#if defined(__APPLE__)
#pragma clang diagnostic pop
#endif
}
#else
const std::string conv_error("Conversion Error");
inline std::string to_string(const std::wstring& wstr) {
std::string result;
if (wstr.empty()) {
return result;
}
// Order of arguments is to, from
auto icvt = iconv_open("UTF-8", "WCHAR_T");
// CentOS is not happy with -1
if (std::numeric_limits<iconv_t>::max() == icvt) {
return conv_error;
}

// I hope this does not modify the incoming buffer
wchar_t* non_const_in = const_cast<wchar_t*>(wstr.c_str());
char* iconv_in = reinterpret_cast<char*>(non_const_in);
size_t iconv_in_bytes = wstr.length() * sizeof(wchar_t);
// Temp buffer, assume every code point converts into 3 bytes, this should be enough
// We do not convert terminating zeros
const size_t buffer_len = wstr.length() * 3;
auto buffer = std::make_unique<char[]>(buffer_len);

char* iconv_out = buffer.get();
size_t iconv_out_bytes = buffer_len;
auto ret = iconv(icvt, &iconv_in, &iconv_in_bytes, &iconv_out, &iconv_out_bytes);
if (static_cast<size_t>(-1) == ret) {
result = conv_error;
} else {
size_t converted_len = buffer_len - iconv_out_bytes;
result.assign(buffer.get(), converted_len);
}
iconv_close(icvt);
return result;
}
#endif

inline std::string to_string(std::thread::id const& id)
{
Expand Down
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/base/IRdReactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace rd
* \brief A non-root node in an object graph which can be synchronized with its remote copy over a network or
* a similar connection, and which allows to subscribe to its changes.
*/
class RD_FRAMEWORK_API IRdReactive : public virtual IRdBindable
class RD_FRAMEWORK_API IRdReactive
{
public:
/**
Expand Down
3 changes: 2 additions & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/base/IWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace rd
{
class RdReactiveBase;
/**
* \brief Sends and receives serialized object data over a network or a similar connection.
*/
Expand Down Expand Up @@ -45,7 +46,7 @@ class RD_FRAMEWORK_API IWire
* \param lifetime lifetime of subscription.
* \param entity to be subscripted
*/
virtual void advise(Lifetime lifetime, IRdReactive const* entity) const = 0;
virtual void advise(Lifetime lifetime, RdReactiveBase const* entity) const = 0;
};
} // namespace rd
#if defined(_MSC_VER)
Expand Down
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/base/WireBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace rd
{
void WireBase::advise(Lifetime lifetime, const IRdReactive* entity) const
void WireBase::advise(Lifetime lifetime, const RdReactiveBase* entity) const
{
message_broker.advise_on(lifetime, entity);
}
Expand Down
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/base/WireBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RD_FRAMEWORK_API WireBase : public IWire
virtual ~WireBase() = default;
// endregion

void advise(Lifetime lifetime, IRdReactive const* entity) const override;
virtual void advise(Lifetime lifetime, RdReactiveBase const* entity) const override;
};
} // namespace rd

Expand Down
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/ext/ExtWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ExtWire::ExtWire()
});
}

void ExtWire::advise(Lifetime lifetime, IRdReactive const* entity) const
void ExtWire::advise(Lifetime lifetime, RdReactiveBase const* entity) const
{
realWire->advise(lifetime, entity);
}
Expand Down
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/ext/ExtWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RD_FRAMEWORK_API ExtWire final : public IWire

mutable IWire const* realWire = nullptr;

void advise(Lifetime lifetime, IRdReactive const* entity) const override;
void advise(Lifetime lifetime, RdReactiveBase const* entity) const override;

void send(RdId const& id, std::function<void(Buffer& buffer)> writer) const override;
};
Expand Down
12 changes: 6 additions & 6 deletions rd-cpp/src/rd_framework_cpp/src/main/protocol/MessageBroker.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "protocol/MessageBroker.h"

#include "base/RdReactiveBase.h"
#include "spdlog/sinks/stdout_color_sinks.h"

namespace rd
Expand All @@ -13,7 +14,7 @@ static void execute(const IRdReactive* that, Buffer msg)
that->on_wire_received(std::move(msg));
}

void MessageBroker::invoke(const IRdReactive* that, Buffer msg, bool sync) const
void MessageBroker::invoke(const RdReactiveBase* that, Buffer msg, bool sync) const
{
if (sync)
{
Expand Down Expand Up @@ -51,7 +52,7 @@ void MessageBroker::dispatch(RdId id, Buffer message) const

{ // synchronized recursively
std::lock_guard<decltype(lock)> guard(lock);
IRdReactive const* s = subscriptions[id];
RdReactiveBase const* s = subscriptions[id];
if (s == nullptr)
{
auto it = broker.find(id);
Expand All @@ -64,7 +65,7 @@ void MessageBroker::dispatch(RdId id, Buffer message) const

auto action = [this, it, id]() mutable {
auto& current = it->second;
IRdReactive const* subscription = subscriptions[id];
RdReactiveBase const* subscription = subscriptions[id];

optional<Buffer> message;
{
Expand Down Expand Up @@ -127,7 +128,7 @@ void MessageBroker::dispatch(RdId id, Buffer message) const
// }
}

void MessageBroker::advise_on(Lifetime lifetime, IRdReactive const* entity) const
void MessageBroker::advise_on(Lifetime lifetime, RdReactiveBase const* entity) const
{
RD_ASSERT_MSG(!entity->get_id().isNull(), ("id is null for entities: " + std::string(typeid(*entity).name())))

Expand All @@ -138,8 +139,7 @@ void MessageBroker::advise_on(Lifetime lifetime, IRdReactive const* entity) cons
if (!lifetime->is_terminated())
{
auto key = entity->get_id();
IRdReactive const* value = entity;
subscriptions[key] = value;
subscriptions[key] = entity;
lifetime->add_action([this, key]() { subscriptions.erase(key); });
}
}
Expand Down
8 changes: 5 additions & 3 deletions rd-cpp/src/rd_framework_cpp/src/main/protocol/MessageBroker.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace rd
{
class RdReactiveBase;

class RD_FRAMEWORK_API Mq
{
public:
Expand All @@ -42,14 +44,14 @@ class RD_FRAMEWORK_API MessageBroker final
{
private:
IScheduler* default_scheduler = nullptr;
mutable rd::unordered_map<RdId, IRdReactive const*> subscriptions;
mutable rd::unordered_map<RdId, RdReactiveBase const*> subscriptions;
mutable rd::unordered_map<RdId, Mq> broker;

mutable std::recursive_mutex lock;

static std::shared_ptr<spdlog::logger> logger;

void invoke(const IRdReactive* that, Buffer msg, bool sync = false) const;
void invoke(const RdReactiveBase* that, Buffer msg, bool sync = false) const;

public:
// region ctor/dtor
Expand All @@ -59,7 +61,7 @@ class RD_FRAMEWORK_API MessageBroker final

void dispatch(RdId id, Buffer message) const;

void advise_on(Lifetime lifetime, IRdReactive const* entity) const;
void advise_on(Lifetime lifetime, RdReactiveBase const* entity) const;
};
} // namespace rd
#if defined(_MSC_VER)
Expand Down
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_framework_cpp/src/main/task/RdCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class RdCall : public virtual RdReactiveBase, public ISerializable
* \param request value to deliver
* \return result of remote invoking
*/
WiredRdTask<TRes, ResSer> sync(TReq const& request, std::chrono::milliseconds timeout = 200ms) const
WiredRdTask<TRes, ResSer> sync(TReq const& request, std::chrono::milliseconds timeout = std::chrono::milliseconds(200)) const
{
auto task = start_internal(request, true, &SynchronousScheduler::Instance());
auto time_at_start = std::chrono::system_clock::now();
Expand Down
Loading