Bitcoin Core  27.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
One end of a token pipe.
Definition: tokenpipe.h:15
TokenPipeEnd(const TokenPipeEnd &)=delete
TokenPipeEnd(TokenPipeEnd &&other)
Definition: tokenpipe.h:56
TokenPipeEnd(int fd=-1)
Definition: tokenpipe.cpp:31
Status
Return value constants for TokenWrite and TokenRead.
Definition: tokenpipe.h:24
@ TS_ERR
I/O error.
Definition: tokenpipe.h:25
@ TS_EOS
Unexpected end of stream.
Definition: tokenpipe.h:26
bool IsOpen()
Return whether endpoint is open.
Definition: tokenpipe.h:53
TokenPipeEnd & operator=(TokenPipeEnd &&other)
Definition: tokenpipe.h:61
int TokenWrite(uint8_t token)
Write token to endpoint.
Definition: tokenpipe.cpp:40
void Close()
Explicit close function.
Definition: tokenpipe.cpp:78
TokenPipeEnd & operator=(const TokenPipeEnd &)=delete
int TokenRead()
Read token from endpoint.
Definition: tokenpipe.cpp:58
An interprocess or interthread pipe for sending tokens (one-byte values) over.
Definition: tokenpipe.h:76
void Close()
Close and end of the pipe that hasn't been moved out.
Definition: tokenpipe.cpp:104
TokenPipeEnd TakeReadEnd()
Take the read end of this pipe.
Definition: tokenpipe.cpp:17
TokenPipeEnd TakeWriteEnd()
Take the write end of this pipe.
Definition: tokenpipe.cpp:24
int m_fds[2]
Definition: tokenpipe.h:78
static std::optional< TokenPipe > Make()
Create a new pipe.
Definition: tokenpipe.cpp:84
TokenPipe & operator=(TokenPipe &&other)
Definition: tokenpipe.h:112
TokenPipe(TokenPipe &&other)
Definition: tokenpipe.h:105
TokenPipe(int fds[2])
Definition: tokenpipe.h:80
TokenPipe & operator=(const TokenPipe &)=delete
TokenPipe(const TokenPipe &)=delete