7#include <ipc/capnp/init.capnp.h>
8#include <ipc/capnp/init.capnp.proxy.h>
14#include <mp/proxy-io.h>
15#include <mp/proxy-types.h>
26#include <sys/socket.h>
27#include <system_error>
33void IpcLogFn(
bool raise, std::string message)
36 if (raise)
throw Exception(message);
39class CapnpProtocol :
public Protocol
42 ~CapnpProtocol() noexcept(true)
45 std::unique_lock<std::mutex> lock(
m_loop->m_mutex);
46 m_loop->removeClient(lock);
51 std::unique_ptr<interfaces::Init> connect(
int fd,
const char* exe_name)
override
54 return mp::ConnectStream<messages::Init>(*
m_loop, fd);
59 if (::listen(listen_fd, 5) != 0) {
60 throw std::system_error(errno, std::system_category());
62 mp::ListenConnections<messages::Init>(*
m_loop, listen_fd,
init);
64 void serve(
int fd,
const char* exe_name,
interfaces::Init&
init,
const std::function<
void()>& ready_fn = {})
override
67 mp::g_thread_context.thread_name = mp::ThreadName(exe_name);
69 if (ready_fn) ready_fn();
70 mp::ServeStream<messages::Init>(*
m_loop, fd,
init);
74 void addCleanup(std::type_index type,
void* iface, std::function<
void()> cleanup)
override
76 mp::ProxyTypeRegister::types().at(type)(iface).cleanup.emplace_back(std::move(cleanup));
78 Context& context()
override {
return m_context; }
79 void startLoop(
const char* exe_name)
82 std::promise<void> promise;
87 std::unique_lock<std::mutex> lock(
m_loop->m_mutex);
94 promise.get_future().wait();
Initial interface created when a process is first started, and used to give and get access to other i...
std::thread m_loop_thread
std::optional< mp::EventLoop > m_loop
#define LogDebug(category,...)
std::unique_ptr< Protocol > MakeCapnpProtocol()
void ThreadRename(const std::string &)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.