|
| 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_INTERFACES_IPC_H |
| 6 | +#define BITCOIN_INTERFACES_IPC_H |
| 7 | + |
| 8 | +#include <functional> |
| 9 | +#include <memory> |
| 10 | +#include <typeindex> |
| 11 | + |
| 12 | +namespace interfaces { |
| 13 | +class Init; |
| 14 | + |
| 15 | +//! Interface providing access to interprocess-communication (IPC) |
| 16 | +//! functionality. |
| 17 | +class Ipc |
| 18 | +{ |
| 19 | +public: |
| 20 | + virtual ~Ipc() = default; |
| 21 | + |
| 22 | + //! Spawn a child process returning pointer to its Init interface. |
| 23 | + virtual std::unique_ptr<Init> spawnProcess(const char* exe_name) = 0; |
| 24 | + |
| 25 | + //! If this is a spawned process, block and handle requests from the parent |
| 26 | + //! process by forwarding them to this process's Init interface, then return |
| 27 | + //! true. If this is not a spawned child process, return false. |
| 28 | + virtual bool startSpawnedProcess(int argc, char* argv[], int& exit_status) = 0; |
| 29 | + |
| 30 | + //! Add cleanup callback to remote interface that will run when the |
| 31 | + //! interface is deleted. |
| 32 | + template<typename Interface> |
| 33 | + void addCleanup(Interface& iface, std::function<void()> cleanup) |
| 34 | + { |
| 35 | + addCleanup(typeid(Interface), &iface, std::move(cleanup)); |
| 36 | + } |
| 37 | + |
| 38 | +protected: |
| 39 | + //! Internal implementation of public addCleanup method (above) as a |
| 40 | + //! type-erased virtual function, since template functions can't be virtual. |
| 41 | + virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0; |
| 42 | +}; |
| 43 | + |
| 44 | +//! Return implementation of Ipc interface. |
| 45 | +std::unique_ptr<Ipc> MakeIpc(const char* exe_name, const char* process_argv0, Init& init); |
| 46 | +} // namespace interfaces |
| 47 | + |
| 48 | +#endif // BITCOIN_INTERFACES_IPC_H |
0 commit comments