Listen on the group socket and handle requests

This commit is contained in:
Robbert van der Helm
2020-05-20 18:45:33 +02:00
parent 6d6d928838
commit 8eb01cb519
7 changed files with 136 additions and 45 deletions
+64 -6
View File
@@ -20,6 +20,8 @@
#include <boost/asio/read_until.hpp>
#include <regex>
#include "../../common/communication.h"
// FIXME: `std::filesystem` is broken in wineg++, at least under Wine 5.8. Any
// path operation will thrown an encoding related error
namespace fs = boost::filesystem;
@@ -29,6 +31,13 @@ namespace fs = boost::filesystem;
*/
std::string create_logger_prefix(const fs::path& socket_path);
// CreateThread() is great and allows you to pass a single value to the
// function, so we'll use this to pass both `this` and the parameters to the
// below thread function so it can do its thing.
using handle_host_plugin_parameters = std::pair<GroupBridge*, PluginParameters>;
uint32_t WINAPI handle_host_plugin_proxy(void* param);
StdIoCapture::StdIoCapture(boost::asio::io_context& io_context,
int file_descriptor)
: pipe(io_context),
@@ -54,10 +63,6 @@ StdIoCapture::~StdIoCapture() {
close(pipe_fd[0]);
}
bool PluginParameters::operator==(const PluginParameters& other) const {
return removeme == other.removeme;
}
GroupBridge::GroupBridge(boost::filesystem::path group_socket_path)
: logger(Logger::create_from_environment(
create_logger_prefix(group_socket_path))),
@@ -77,18 +82,58 @@ GroupBridge::GroupBridge(boost::filesystem::path group_socket_path)
}
void GroupBridge::handle_host_plugin(const PluginParameters& parameters) {
// TODO: Start the plugin,
// TODO: Start the plugin
// TODO: Allow this process to exit when the last plugin exits. Make sure
// that that doesn't cause any race conditions.
}
void GroupBridge::handle_incoming_connections() {
// TODO: Accept connections here
accept_requests();
logger.log("Now accepting incoming connections");
io_context.run();
}
void GroupBridge::accept_requests() {
group_socket_acceptor.async_accept(
[&](const boost::system::error_code& error,
boost::asio::local::stream_protocol::socket socket) {
// Stop the whole process when the socket gets closed unexpectedly
if (error.failed()) {
logger.log("Error while listening for incoming connections:");
logger.log(error.message());
io_context.stop();
}
// Read the parameters, and then host the plugin in this process
// just like if we would be hosting the plugin individually through
// `yabridge-hsot.exe`. Since we're using sockets there's no reason
// to send an acknowledgement back. One potential issue here is that
// it will be hard to tell whether a plugin has crashed before
// initialization, and that the sockets will never be accepted. For
// individually hosted plugins we poll whether the Wine process is
// still active so we can terminate early if it is not, but in this
// case the yabridge instance has to determine that this process is
// still running.
const auto parameters = read_object<PluginParameters>(socket);
// Collisions in the generated socket names should be very rare, but
// it could in theory happen
std::lock_guard lock(active_plugins_mutex);
assert(active_plugins.find(parameters) != active_plugins.end());
// CreateThread() doesn't support multiple arguments and requires
// manualy memory management.
handle_host_plugin_parameters* thread_params =
new std::pair<GroupBridge*, PluginParameters>(this, parameters);
active_plugins[parameters] =
Win32Thread(handle_host_plugin_proxy, &thread_params);
accept_requests();
});
}
void GroupBridge::async_log_pipe_lines(
boost::asio::posix::stream_descriptor& pipe,
boost::asio::streambuf& buffer,
@@ -135,3 +180,16 @@ std::string create_logger_prefix(const fs::path& socket_path) {
return "[" + socket_name + "] ";
}
uint32_t WINAPI handle_host_plugin_proxy(void* param) {
// The Win32 API only allows you to pass a void pointer to threads, so we
// need to use manual memory management.
auto thread_params = static_cast<handle_host_plugin_parameters*>(param);
GroupBridge* instance = thread_params->first;
PluginParameters& parameters = thread_params->second;
delete thread_params;
instance->handle_host_plugin(parameters);
return 0;
}
+21 -23
View File
@@ -24,21 +24,6 @@
#include "vst2.h"
// TODO: Replace this with a proper struct that contains the required arguments
// for creating a PluginBridge instance
struct PluginParameters {
int removeme;
bool operator==(const PluginParameters& p) const;
};
template <>
struct std::hash<PluginParameters> {
std::size_t operator()(PluginParameters const& params) const noexcept {
return std::hash<int>{}(params.removeme);
}
};
/**
* Encapsulate capturing the STDOUT or STDERR stream by opening a pipe and
* reopening the passed file descriptor as one of the ends of the newly opened
@@ -123,6 +108,8 @@ class GroupBridge {
* where `<wine_prefix_id>` is a numerical hash as explained in the
* `create_logger_prefix()` function in `./group.cpp`.
*
* @throw boost::system::system_error If we can't listen on the socket.
*
* @note Creating an `GroupBridge` instance has the side effect that the
* STDOUT and STDERR streams of the current process will be redirected to
* a pipe so they can be properly written to a log file.
@@ -132,10 +119,10 @@ class GroupBridge {
/**
* Host a new plugin within this process. Called by proxy using
* `handle_host_plugin_proxy()` in `./group.cpp` because the Win32
* `CreateThread` API only allows passing a single pointer to the function.
* Because we don't have access to our own thread handle, the thread that's
* listening on the group socket and that has created this thread will also
* add this thread to the `active_plugins` map.
* `CreateThread` API only allows passing a single pointer to the function
* and does not allow lambdas. Because we don't have access to our own
* thread handle, the thread that's listening on the group socket will have
* already added this thread to the `active_plugins` map.
*
* Once the plugin has exited, this thread will then remove itself from the
* `active_plugins` map. If this causes the vector to become empty, we will
@@ -158,6 +145,17 @@ class GroupBridge {
void handle_incoming_connections();
private:
/**
* Listen on the group socket for incoming requests to host a new plugin
* within this group process. This will asynchronously listen on the socket,
* and for any connection made it will retrieve a `PluginParameters` object
* containing information about the plugin to host and then spawn a new
* thread to start hosting that plugin.
*
* @see handle_host_plugin
*/
void accept_requests();
/**
* Continuously read from a pipe and write the output to the log file. Used
* with the IO streams captured by `stdout_redirect` and `stderr_redirect`.
@@ -206,11 +204,11 @@ class GroupBridge {
/**
* A map of threads that are currently hosting a plugin within this process.
* After a plugin has exited or its initialization has failed, the thread
* handling it will remove itself from this map.
* handling it will remove itself from this map. This is to keep track of
* the amount of plugins currently running with their associated thread
* handles.
*/
std::unordered_map<PluginParameters,
std::pair<Win32Thread, std::unique_ptr<Vst2Bridge>>>
active_plugins;
std::unordered_map<PluginParameters, Win32Thread> active_plugins;
/**
* A mutex to prevent two threads from simultaneously accessing the plugins
* map, and also to prevent `handle_host_plugin()` from terminating the