Skip to content

Commit

Permalink
fix(patchbay): timeout in cases where server does not respond (closes V…
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed May 22, 2024
1 parent 7eacac5 commit 4edb48a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.21)
project(venmic LANGUAGES CXX VERSION 3.4.4)
project(venmic LANGUAGES CXX VERSION 3.5.0)

# --------------------------------------------------------------------------------------------------------
# Library options
Expand Down Expand Up @@ -87,7 +87,7 @@ include("cmake/cpm.cmake")

CPMFindPackage(
NAME rohrkabel
VERSION 5.1
VERSION 6.0
GIT_REPOSITORY "https://github.com/Curve/rohrkabel"
)

Expand All @@ -111,7 +111,7 @@ CPMFindPackage(

CPMFindPackage(
NAME glaze
VERSION 2.6.1
VERSION 2.6.5
GIT_REPOSITORY "https://github.com/stephenberry/glaze"
)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"private": false,
"license": "MPL-2.0",
"author": "Curve (https://github.com/Curve)",
"version": "3.4.4",
"version": "3.5.0",
"main": "./lib/index.js",
"types": "./lib/module.d.ts",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion private/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ namespace vencord
{
};

struct abort
{
};

struct ready
{
bool success{true};
};

using pw_recipe = pw::recipe<list_nodes, link_options, unset_target, quit>;
using pw_recipe = pw::recipe<list_nodes, link_options, unset_target, quit, abort>;
using cr_recipe = cr::recipe<std::vector<node>, ready>;
} // namespace vencord
1 change: 1 addition & 0 deletions private/patchbay.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace vencord
std::jthread thread;

public:
std::stop_source update_source;
std::unique_ptr<pw_recipe::sender> sender;
std::unique_ptr<cr_recipe::receiver> receiver;

Expand Down
21 changes: 21 additions & 0 deletions server/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <httplib.h>

#include <exception>
#include <glaze/glaze.hpp>

#include <vencord/logger.hpp>
Expand Down Expand Up @@ -49,6 +51,25 @@ int main(int argc, char **args)

httplib::Server server;

server.set_exception_handler(
[&](const auto &, auto &, auto exception)
{
try
{
std::rethrow_exception(exception);
}
catch (const std::exception &ex)
{
logger::get()->error("Encountered error: {}", ex.what());
}
catch (...)
{
logger::get()->error("Encountered error: <Unknown>");
}

server.stop();
});

server.Post("/list",
[](const auto &req, auto &response)
{
Expand Down
2 changes: 0 additions & 2 deletions src/patchbay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ namespace vencord

void patchbay::link(link_options options)
{
logger::get()->trace(R"([patchbay] (link) request: "{}")", glz::write_json(options));
m_impl->sender->send(std::move(options));
}

void patchbay::unlink()
{
logger::get()->trace("[patchbay] unlink requested");
m_impl->sender->send(unset_target{});
}

Expand Down
34 changes: 32 additions & 2 deletions src/patchbay.impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ namespace vencord

thread = std::jthread{thread_start, std::move(pw_receiver), cr_sender};

if (receiver->recv_as<ready>().success)
auto response = receiver->recv_timeout_as<ready>(std::chrono::seconds(1));

if (!response.has_value())
{
sender->send(abort{});
response = receiver->recv_as<ready>();
}

if (response->success)
{
logger::get()->trace("[patchbay] (init) pw_receiver is ready");
return;
Expand Down Expand Up @@ -589,6 +597,15 @@ namespace vencord
core->context()->loop()->quit();
}

template <>
void patchbay::impl::receive([[maybe_unused]] cr_recipe::sender &, [[maybe_unused]] abort &)
{
should_exit = true;

update_source.request_stop();
core->context()->loop()->quit();
}

void patchbay::impl::start(pw_recipe::receiver receiver, cr_recipe::sender sender)
{
auto loop = pw::main_loop::create();
Expand All @@ -611,16 +628,29 @@ namespace vencord
receive(sender, message);
});

auto future = core->update();
update_source = future.stop_source();

auto success = future.get();

if (!success.value_or(false))
{
sender.send(ready{false});
return;
}

auto listener = registry->listen();

listener.on<pw::registry_event::global_removed>([this](std::uint32_t id) { del_global(id); });
listener.on<pw::registry_event::global>([this](auto global) { add_global(global); });

sender.send(ready{});
sender.send(ready{true});

while (!should_exit)
{
loop->run();
}

logger::get()->trace("[patchbay] (main_loop) finished");
}
} // namespace vencord

0 comments on commit 4edb48a

Please sign in to comment.