Skip to content

Commit 3e33d17

Browse files
committed
Add ipc::Context and ipc::capnp::Context structs
These are currently empty structs but they will be used to pass some function and object pointers from bitcoin application code to IPC hooks that run, for example, when a remote object is created or destroyed, or a new process is created.
1 parent 1704bbf commit 3e33d17

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,11 @@ EXTRA_DIST += $(libbitcoin_ipc_mpgen_input)
840840
if BUILD_MULTIPROCESS
841841
LIBBITCOIN_IPC=libbitcoin_ipc.a
842842
libbitcoin_ipc_a_SOURCES = \
843+
ipc/capnp/context.h \
843844
ipc/capnp/init-types.h \
844845
ipc/capnp/protocol.cpp \
845846
ipc/capnp/protocol.h \
847+
ipc/context.h \
846848
ipc/exception.h \
847849
ipc/interfaces.cpp \
848850
ipc/process.cpp \

src/interfaces/ipc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <memory>
1010
#include <typeindex>
1111

12+
namespace ipc {
13+
struct Context;
14+
} // namespace ipc
15+
1216
namespace interfaces {
1317
class Init;
1418

@@ -58,6 +62,9 @@ class Ipc
5862
addCleanup(typeid(Interface), &iface, std::move(cleanup));
5963
}
6064

65+
//! IPC context struct accessor (see struct definition for more description).
66+
virtual ipc::Context& context() = 0;
67+
6168
protected:
6269
//! Internal implementation of public addCleanup method (above) as a
6370
//! type-erased virtual function, since template functions can't be virtual.

src/ipc/capnp/context.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#ifndef BITCOIN_IPC_CAPNP_CONTEXT_H
6+
#define BITCOIN_IPC_CAPNP_CONTEXT_H
7+
8+
#include <ipc/context.h>
9+
10+
namespace ipc {
11+
namespace capnp {
12+
//! Cap'n Proto context struct. Generally the parent ipc::Context struct should
13+
//! be used instead of this struct to give all IPC protocols access to
14+
//! application state, so there aren't unnecessary differences between IPC
15+
//! protocols. But this specialized struct can be used to pass capnp-specific
16+
//! function and object types to capnp hooks.
17+
struct Context : ipc::Context
18+
{
19+
};
20+
} // namespace capnp
21+
} // namespace ipc
22+
23+
#endif // BITCOIN_IPC_CAPNP_CONTEXT_H

src/ipc/capnp/protocol.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <interfaces/init.h>
6+
#include <ipc/capnp/context.h>
67
#include <ipc/capnp/init.capnp.h>
78
#include <ipc/capnp/init.capnp.proxy.h>
89
#include <ipc/capnp/protocol.h>
@@ -54,7 +55,7 @@ class CapnpProtocol : public Protocol
5455
{
5556
assert(!m_loop);
5657
mp::g_thread_context.thread_name = mp::ThreadName(exe_name);
57-
m_loop.emplace(exe_name, &IpcLogFn, nullptr);
58+
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
5859
mp::ServeStream<messages::Init>(*m_loop, fd, init);
5960
m_loop->loop();
6061
m_loop.reset();
@@ -63,13 +64,14 @@ class CapnpProtocol : public Protocol
6364
{
6465
mp::ProxyTypeRegister::types().at(type)(iface).cleanup.emplace_back(std::move(cleanup));
6566
}
67+
Context& context() override { return m_context; }
6668
void startLoop(const char* exe_name)
6769
{
6870
if (m_loop) return;
6971
std::promise<void> promise;
7072
m_loop_thread = std::thread([&] {
7173
util::ThreadRename("capnp-loop");
72-
m_loop.emplace(exe_name, &IpcLogFn, nullptr);
74+
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
7375
{
7476
std::unique_lock<std::mutex> lock(m_loop->m_mutex);
7577
m_loop->addClient(lock);
@@ -80,6 +82,7 @@ class CapnpProtocol : public Protocol
8082
});
8183
promise.get_future().wait();
8284
}
85+
Context m_context;
8386
std::thread m_loop_thread;
8487
std::optional<mp::EventLoop> m_loop;
8588
};

src/ipc/context.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#ifndef BITCOIN_IPC_CONTEXT_H
6+
#define BITCOIN_IPC_CONTEXT_H
7+
8+
namespace ipc {
9+
//! Context struct used to give IPC protocol implementations or implementation
10+
//! hooks access to application state, in case they need to run extra code that
11+
//! isn't needed within a single process, like code copying global state from an
12+
//! existing process to a new process when it's initialized, or code dealing
13+
//! with shared objects that are created or destroyed remotely.
14+
struct Context
15+
{
16+
};
17+
} // namespace ipc
18+
19+
#endif // BITCOIN_IPC_CONTEXT_H

src/ipc/interfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class IpcImpl : public interfaces::Ipc
6060
{
6161
m_protocol->addCleanup(type, iface, std::move(cleanup));
6262
}
63+
Context& context() override { return m_protocol->context(); }
6364
const char* m_exe_name;
6465
const char* m_process_argv0;
6566
interfaces::Init& m_init;

src/ipc/protocol.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <typeindex>
1313

1414
namespace ipc {
15+
struct Context;
16+
1517
//! IPC protocol interface for calling IPC methods over sockets.
1618
//!
1719
//! There may be different implementations of this interface for different IPC
@@ -33,6 +35,9 @@ class Protocol
3335
//! Add cleanup callback to interface that will run when the interface is
3436
//! deleted.
3537
virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;
38+
39+
//! Context accessor.
40+
virtual Context& context() = 0;
3641
};
3742
} // namespace ipc
3843

0 commit comments

Comments
 (0)