Bitcoin Core  22.99.0
P2P Digital Currency
tokenpipe.h
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 #ifndef BITCOIN_UTIL_TOKENPIPE_H
6 #define BITCOIN_UTIL_TOKENPIPE_H
7 
8 #ifndef WIN32
9 
10 #include <cstdint>
11 #include <optional>
12 
15 {
16 private:
17  int m_fd = -1;
18 
19 public:
20  TokenPipeEnd(int fd = -1);
21  ~TokenPipeEnd();
22 
24  enum Status {
25  TS_ERR = -1,
26  TS_EOS = -2,
27  };
28 
36  int TokenWrite(uint8_t token);
37 
45  int TokenRead();
46 
49  void Close();
50 
53  bool IsOpen() { return m_fd != -1; }
54 
55  // Move-only class.
57  {
58  m_fd = other.m_fd;
59  other.m_fd = -1;
60  }
62  {
63  Close();
64  m_fd = other.m_fd;
65  other.m_fd = -1;
66  return *this;
67  }
68  TokenPipeEnd(const TokenPipeEnd&) = delete;
69  TokenPipeEnd& operator=(const TokenPipeEnd&) = delete;
70 };
71 
75 class TokenPipe
76 {
77 private:
78  int m_fds[2] = {-1, -1};
79 
80  TokenPipe(int fds[2]) : m_fds{fds[0], fds[1]} {}
81 
82 public:
83  ~TokenPipe();
84 
88  static std::optional<TokenPipe> Make();
89 
94 
99 
102  void Close();
103 
104  // Move-only class.
106  {
107  for (int i = 0; i < 2; ++i) {
108  m_fds[i] = other.m_fds[i];
109  other.m_fds[i] = -1;
110  }
111  }
113  {
114  Close();
115  for (int i = 0; i < 2; ++i) {
116  m_fds[i] = other.m_fds[i];
117  other.m_fds[i] = -1;
118  }
119  return *this;
120  }
121  TokenPipe(const TokenPipe&) = delete;
122  TokenPipe& operator=(const TokenPipe&) = delete;
123 };
124 
125 #endif // WIN32
126 
127 #endif // BITCOIN_UTIL_TOKENPIPE_H
TokenPipeEnd::m_fd
int m_fd
Definition: tokenpipe.h:17
TokenPipeEnd::~TokenPipeEnd
~TokenPipeEnd()
Definition: tokenpipe.cpp:33
TokenPipeEnd::TokenRead
int TokenRead()
Read token from endpoint.
Definition: tokenpipe.cpp:56
TokenPipe
An interprocess or interthread pipe for sending tokens (one-byte values) over.
Definition: tokenpipe.h:75
TokenPipeEnd::Close
void Close()
Explicit close function.
Definition: tokenpipe.cpp:76
TokenPipeEnd::TokenPipeEnd
TokenPipeEnd(TokenPipeEnd &&other)
Definition: tokenpipe.h:56
TokenPipe::TokenPipe
TokenPipe(int fds[2])
Definition: tokenpipe.h:80
TokenPipe::m_fds
int m_fds[2]
Definition: tokenpipe.h:78
TokenPipeEnd
One end of a token pipe.
Definition: tokenpipe.h:14
TokenPipeEnd::TokenWrite
int TokenWrite(uint8_t token)
Write token to endpoint.
Definition: tokenpipe.cpp:38
TokenPipeEnd::TokenPipeEnd
TokenPipeEnd(int fd=-1)
Definition: tokenpipe.cpp:29
TokenPipe::TokenPipe
TokenPipe(TokenPipe &&other)
Definition: tokenpipe.h:105
TokenPipe::Make
static std::optional< TokenPipe > Make()
Create a new pipe.
Definition: tokenpipe.cpp:82
TokenPipe::operator=
TokenPipe & operator=(TokenPipe &&other)
Definition: tokenpipe.h:112
TokenPipeEnd::TS_ERR
@ TS_ERR
I/O error.
Definition: tokenpipe.h:25
TokenPipe::TakeWriteEnd
TokenPipeEnd TakeWriteEnd()
Take the write end of this pipe.
Definition: tokenpipe.cpp:22
TokenPipeEnd::TS_EOS
@ TS_EOS
Unexpected end of stream.
Definition: tokenpipe.h:26
TokenPipeEnd::IsOpen
bool IsOpen()
Return whether endpoint is open.
Definition: tokenpipe.h:53
TokenPipe::TakeReadEnd
TokenPipeEnd TakeReadEnd()
Take the read end of this pipe.
Definition: tokenpipe.cpp:15
TokenPipe::Close
void Close()
Close and end of the pipe that hasn't been moved out.
Definition: tokenpipe.cpp:102
TokenPipeEnd::operator=
TokenPipeEnd & operator=(TokenPipeEnd &&other)
Definition: tokenpipe.h:61
TokenPipeEnd::Status
Status
Return value constants for TokenWrite and TokenRead.
Definition: tokenpipe.h:24
TokenPipe::~TokenPipe
~TokenPipe()
Definition: tokenpipe.cpp:97