Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_IPC_PROTOCOL_H6#define BITCOIN_IPC_PROTOCOL_H8#include <interfaces/init.h>9#include <ipc/util.h>11#include <functional>12#include <memory>13#include <typeindex>15namespace ipc {16struct Context;18//! IPC protocol interface for calling IPC methods over sockets.19//!20//! There may be different implementations of this interface for different IPC21//! protocols (e.g. Cap'n Proto, gRPC, JSON-RPC, or custom protocols).22class Protocol23{24public:25virtual ~Protocol() = default;27//! Return Init interface that forwards requests over given connection28//! stream. Socket communication is handled on a background thread.29//!30//! @note It could be potentially useful in the future to add31//! std::function<void()> on_disconnect callback argument here. But there32//! isn't an immediate need, because the protocol implementation can clean33//! up its own state (calling ProxyServer destructors, etc) on disconnect,34//! and any client calls will just throw ipc::Exception errors after a35//! disconnect.36virtual std::unique_ptr<interfaces::Init> connect(mp::Stream stream) = 0;38//! Listen for connections on provided socket id, accept them, and handle39//! requests on accepted connections. This method doesn't block, and40//! performs I/O on a background thread.41virtual void listen(mp::SocketId listen_fd, interfaces::Init& init) = 0;43//! Handle requests from a stream provided by the make_stream callback,44//! forwarding them to the provided Init interface. Socket communication is45//! handled on the current thread, and this call blocks until the socket is46//! closed. A callback is used to specify the stream because this method47//! initializes the event loop and it may not be possible to create the48//! stream before the event loop is initialized.49//!50//! @note: If this method is called, it needs to be called before connect()51//! or listen() methods, because for ease of implementation this method is52//! inflexible and always runs the event loop in the foreground thread. It53//! can share its event loop with the other methods but can't share an event54//! loop that was created by them. This isn't a problem because serve() is55//! only called by spawned child processes that call it immediately to56//! communicate back with parent processes.57virtual void serve(interfaces::Init& init, const std::function<mp::Stream()>& make_stream) = 0;59//! Make stream object from socket id.60virtual mp::Stream makeStream(mp::SocketId socket) = 0;62//! Disconnect any incoming connections that are still connected.63virtual void disconnectIncoming() = 0;65//! Add cleanup callback to interface that will run when the interface is66//! deleted.67virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;69//! Context accessor.70virtual Context& context() = 0;71};72} // namespace ipc74#endif // BITCOIN_IPC_PROTOCOL_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.