7#include <ipc/capnp/init.capnp.h>
8#include <ipc/capnp/init.capnp.proxy.h>
26#include <sys/socket.h>
27#include <system_error>
34mp::Log GetRequestedIPCLogLevel()
43void IpcLogFn(mp::LogMessage message)
45 switch (message.
level) {
63 throw Exception(message.
message);
71class CapnpProtocol :
public Protocol
74 ~CapnpProtocol() noexcept(true)
77 if (m_loop_thread.joinable()) m_loop_thread.join();
80 std::unique_ptr<interfaces::Init> connect(
int fd,
const char* exe_name)
override
85 void listen(
int listen_fd,
const char* exe_name, interfaces::Init& init)
override
88 if (::listen(listen_fd, 5) != 0) {
89 throw std::system_error(errno, std::system_category());
93 void serve(
int fd,
const char* exe_name, interfaces::Init& init,
const std::function<
void()>& ready_fn = {})
override
97 mp::LogOptions opts = {
99 .log_level = GetRequestedIPCLogLevel()
101 m_loop.emplace(exe_name, std::move(opts), &m_context);
102 if (ready_fn) ready_fn();
104 m_parent_connection = &m_loop->m_incoming_connections.back();
108 void disconnectIncoming()
override
115 m_loop->m_incoming_connections.remove_if([
this](mp::Connection& c) {
return &c != m_parent_connection; });
118 void addCleanup(std::type_index type,
void* iface, std::function<
void()> cleanup)
override
122 Context& context()
override {
return m_context; }
123 void startLoop(
const char* exe_name)
126 std::promise<void> promise;
127 m_loop_thread = std::thread([&] {
129 mp::LogOptions opts = {
131 .log_level = GetRequestedIPCLogLevel()
133 m_loop.emplace(exe_name, std::move(opts), &m_context);
134 m_loop_ref.emplace(*m_loop);
139 promise.get_future().wait();
142 std::thread m_loop_thread;
144 std::optional<mp::EventLoop> m_loop;
148 std::optional<mp::EventLoopRef> m_loop_ref;
150 mp::Connection* m_parent_connection{
nullptr};
#define LogTrace(category,...)
#define LogDebug(category,...)
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
std::unique_ptr< Protocol > MakeCapnpProtocol()
void ServeStream(EventLoop &loop, int fd, InitImpl &init)
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
ThreadContext g_thread_context
std::unique_ptr< ProxyClient< InitInterface > > ConnectStream(EventLoop &loop, int fd)
Log
Log flags. Update stringify function if changed!
void ListenConnections(EventLoop &loop, int fd, InitImpl &init)
void ThreadRename(const std::string &)
Log level
The severity level of this message.
std::string message
Message to be logged.
std::string thread_name
Identifying string for debug.