Bitcoin Core 29.99.0
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <interfaces/init.h>
6#include <ipc/capnp/context.h>
7#include <ipc/capnp/init.capnp.h>
8#include <ipc/capnp/init.capnp.proxy.h>
10#include <ipc/exception.h>
11#include <ipc/protocol.h>
12#include <kj/async.h>
13#include <logging.h>
14#include <mp/proxy-io.h>
15#include <mp/proxy-types.h>
16#include <mp/util.h>
17#include <util/threadnames.h>
18
19#include <cassert>
20#include <cerrno>
21#include <future>
22#include <memory>
23#include <mutex>
24#include <optional>
25#include <string>
26#include <sys/socket.h>
27#include <system_error>
28#include <thread>
29
30namespace ipc {
31namespace capnp {
32namespace {
33void IpcLogFn(bool raise, std::string message)
34{
35 LogDebug(BCLog::IPC, "%s\n", message);
36 if (raise) throw Exception(message);
37}
38
39class CapnpProtocol : public Protocol
40{
41public:
42 ~CapnpProtocol() noexcept(true)
43 {
44 m_loop_ref.reset();
45 if (m_loop_thread.joinable()) m_loop_thread.join();
46 assert(!m_loop);
47 };
48 std::unique_ptr<interfaces::Init> connect(int fd, const char* exe_name) override
49 {
50 startLoop(exe_name);
51 return mp::ConnectStream<messages::Init>(*m_loop, fd);
52 }
53 void listen(int listen_fd, const char* exe_name, interfaces::Init& init) override
54 {
55 startLoop(exe_name);
56 if (::listen(listen_fd, /*backlog=*/5) != 0) {
57 throw std::system_error(errno, std::system_category());
58 }
59 mp::ListenConnections<messages::Init>(*m_loop, listen_fd, init);
60 }
61 void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) override
62 {
63 assert(!m_loop);
65 m_loop.emplace(exe_name, &IpcLogFn, &m_context);
66 if (ready_fn) ready_fn();
67 mp::ServeStream<messages::Init>(*m_loop, fd, init);
68 m_parent_connection = &m_loop->m_incoming_connections.back();
69 m_loop->loop();
70 m_loop.reset();
71 }
72 void disconnectIncoming() override
73 {
74 if (!m_loop) return;
75 // Delete incoming connections, except the connection to a parent
76 // process (if there is one), since a parent process should be able to
77 // monitor and control this process, even during shutdown.
78 m_loop->sync([&] {
79 m_loop->m_incoming_connections.remove_if([this](mp::Connection& c) { return &c != m_parent_connection; });
80 });
81 }
82 void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
83 {
84 mp::ProxyTypeRegister::types().at(type)(iface).cleanup_fns.emplace_back(std::move(cleanup));
85 }
86 Context& context() override { return m_context; }
87 void startLoop(const char* exe_name)
88 {
89 if (m_loop) return;
90 std::promise<void> promise;
91 m_loop_thread = std::thread([&] {
92 util::ThreadRename("capnp-loop");
93 m_loop.emplace(exe_name, &IpcLogFn, &m_context);
94 m_loop_ref.emplace(*m_loop);
95 promise.set_value();
96 m_loop->loop();
97 m_loop.reset();
98 });
99 promise.get_future().wait();
100 }
101 Context m_context;
102 std::thread m_loop_thread;
104 std::optional<mp::EventLoop> m_loop;
108 std::optional<mp::EventLoopRef> m_loop_ref;
111};
112} // namespace
113
114std::unique_ptr<Protocol> MakeCapnpProtocol() { return std::make_unique<CapnpProtocol>(); }
115} // namespace capnp
116} // namespace ipc
Initial interface created when a process is first started, and used to give and get access to other i...
Definition: init.h:31
Object holding network & rpc state associated with either an incoming server connection,...
Definition: proxy-io.h:321
std::thread m_loop_thread
Definition: protocol.cpp:102
mp::Connection * m_parent_connection
Connection to parent, if this is a child process spawned by a parent process.
Definition: protocol.cpp:110
std::optional< mp::EventLoop > m_loop
EventLoop object which manages I/O events for all connections.
Definition: protocol.cpp:104
std::optional< mp::EventLoopRef > m_loop_ref
Reference to the same EventLoop.
Definition: protocol.cpp:108
Context m_context
Definition: protocol.cpp:101
#define LogDebug(category,...)
Definition: logging.h:381
@ IPC
Definition: logging.h:89
std::unique_ptr< Protocol > MakeCapnpProtocol()
Definition: protocol.cpp:114
Definition: ipc.h:12
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
Definition: util.cpp:48
thread_local ThreadContext g_thread_context
Definition: proxy.cpp:40
void ThreadRename(const std::string &)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:55
static Types & types()
Definition: proxy-types.h:740
std::string thread_name
Identifying string for debug.
Definition: proxy-io.h:548
assert(!tx.IsCoinBase())