Inside Bitcoin
Opening the selected file…
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_UTIL_TOKENPIPE_H6#define BITCOIN_UTIL_TOKENPIPE_H8#ifndef WIN3210#include <cstdint>11#include <optional>13/** One end of a token pipe. */14class TokenPipeEnd15{16private:17int m_fd = -1;19public:20TokenPipeEnd(int fd = -1);21~TokenPipeEnd();23/** Return value constants for TokenWrite and TokenRead. */24enum Status {25TS_ERR = -1, //!< I/O error26TS_EOS = -2, //!< Unexpected end of stream27};29/** Write token to endpoint.30*31* @returns 0 If successful.32* <0 if error:33* TS_ERR If an error happened.34* TS_EOS If end of stream happened.35*/36int TokenWrite(uint8_t token);38/** Read token from endpoint.39*40* @returns >=0 Token value, if successful.41* <0 if error:42* TS_ERR If an error happened.43* TS_EOS If end of stream happened.44*/45int TokenRead();47/** Explicit close function.48*/49void Close();51/** Return whether endpoint is open.52*/53bool IsOpen() { return m_fd != -1; }55// Move-only class.56TokenPipeEnd(TokenPipeEnd&& other)57{58m_fd = other.m_fd;59other.m_fd = -1;60}61TokenPipeEnd& operator=(TokenPipeEnd&& other)62{63Close();64m_fd = other.m_fd;65other.m_fd = -1;66return *this;67}68TokenPipeEnd(const TokenPipeEnd&) = delete;69TokenPipeEnd& operator=(const TokenPipeEnd&) = delete;70};72/** An interprocess or interthread pipe for sending tokens (one-byte values)73* over.74*/75class TokenPipe76{77private:78int m_fds[2] = {-1, -1};80TokenPipe(int fds[2]) : m_fds{fds[0], fds[1]} {}82public:83~TokenPipe();85/** Create a new pipe.86* @returns The created TokenPipe, or an empty std::nullopt in case of error.87*/88static std::optional<TokenPipe> Make();90/** Take the read end of this pipe. This can only be called once,91* as the object will be moved out.92*/93TokenPipeEnd TakeReadEnd();95/** Take the write end of this pipe. This should only be called once,96* as the object will be moved out.97*/98TokenPipeEnd TakeWriteEnd();100/** Close and end of the pipe that hasn't been moved out.101*/102void Close();104// Move-only class.105TokenPipe(TokenPipe&& other)106{107for (int i = 0; i < 2; ++i) {108m_fds[i] = other.m_fds[i];109other.m_fds[i] = -1;110}111}112TokenPipe& operator=(TokenPipe&& other)113{114Close();115for (int i = 0; i < 2; ++i) {116m_fds[i] = other.m_fds[i];117other.m_fds[i] = -1;118}119return *this;120}121TokenPipe(const TokenPipe&) = delete;122TokenPipe& operator=(const TokenPipe&) = delete;123};125#endif // WIN32127#endif // BITCOIN_UTIL_TOKENPIPE_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.