aboutsummaryrefslogtreecommitdiff
path: root/src/util/tokenpipe.h
blob: f56be93a38c93875ba4ca2dcb5ba0234a402b8b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_UTIL_TOKENPIPE_H
#define BITCOIN_UTIL_TOKENPIPE_H

#ifndef WIN32

#include <cstdint>
#include <optional>

/** One end of a token pipe. */
class TokenPipeEnd
{
private:
    int m_fd = -1;

public:
    TokenPipeEnd(int fd = -1);
    ~TokenPipeEnd();

    /** Return value constants for TokenWrite and TokenRead. */
    enum Status {
        TS_ERR = -1, //!< I/O error
        TS_EOS = -2, //!< Unexpected end of stream
    };

    /** Write token to endpoint.
     *
     * @returns 0       If successful.
     *          <0 if error:
     *            TS_ERR  If an error happened.
     *            TS_EOS  If end of stream happened.
     */
    int TokenWrite(uint8_t token);

    /** Read token from endpoint.
     *
     * @returns >=0     Token value, if successful.
     *          <0 if error:
     *            TS_ERR  If an error happened.
     *            TS_EOS  If end of stream happened.
     */
    int TokenRead();

    /** Explicit close function.
     */
    void Close();

    /** Return whether endpoint is open.
     */
    bool IsOpen() { return m_fd != -1; }

    // Move-only class.
    TokenPipeEnd(TokenPipeEnd&& other)
    {
        m_fd = other.m_fd;
        other.m_fd = -1;
    }
    TokenPipeEnd& operator=(TokenPipeEnd&& other)
    {
        Close();
        m_fd = other.m_fd;
        other.m_fd = -1;
        return *this;
    }
    TokenPipeEnd(const TokenPipeEnd&) = delete;
    TokenPipeEnd& operator=(const TokenPipeEnd&) = delete;
};

/** An interprocess or interthread pipe for sending tokens (one-byte values)
 * over.
 */
class TokenPipe
{
private:
    int m_fds[2] = {-1, -1};

    TokenPipe(int fds[2]) : m_fds{fds[0], fds[1]} {}

public:
    ~TokenPipe();

    /** Create a new pipe.
     * @returns The created TokenPipe, or an empty std::nullopt in case of error.
     */
    static std::optional<TokenPipe> Make();

    /** Take the read end of this pipe. This can only be called once,
     * as the object will be moved out.
     */
    TokenPipeEnd TakeReadEnd();

    /** Take the write end of this pipe. This should only be called once,
     * as the object will be moved out.
     */
    TokenPipeEnd TakeWriteEnd();

    /** Close and end of the pipe that hasn't been moved out.
     */
    void Close();

    // Move-only class.
    TokenPipe(TokenPipe&& other)
    {
        for (int i = 0; i < 2; ++i) {
            m_fds[i] = other.m_fds[i];
            other.m_fds[i] = -1;
        }
    }
    TokenPipe& operator=(TokenPipe&& other)
    {
        Close();
        for (int i = 0; i < 2; ++i) {
            m_fds[i] = other.m_fds[i];
            other.m_fds[i] = -1;
        }
        return *this;
    }
    TokenPipe(const TokenPipe&) = delete;
    TokenPipe& operator=(const TokenPipe&) = delete;
};

#endif // WIN32

#endif // BITCOIN_UTIL_TOKENPIPE_H