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