Line | Count | Source |
1 | | #ifndef SRC_NODE_BOB_H_ |
2 | | #define SRC_NODE_BOB_H_ |
3 | | |
4 | | #include <functional> |
5 | | |
6 | | namespace node { |
7 | | namespace bob { |
8 | | |
9 | | constexpr size_t kMaxCountHint = 16; |
10 | | |
11 | | // Negative status codes indicate error conditions. |
12 | | enum Status : int { |
13 | | // Indicates that an attempt was made to pull after end. |
14 | | STATUS_EOS = 0, |
15 | | |
16 | | // Indicates that there is additional data available |
17 | | // and the consumer may continue to pull. |
18 | | STATUS_CONTINUE = 1, |
19 | | |
20 | | // Indicates that there is no additional data available |
21 | | // but the stream has not ended. The consumer should not |
22 | | // continue to pull but may resume pulling later when |
23 | | // data is available. |
24 | | STATUS_BLOCK = 2, |
25 | | |
26 | | // Indicates that there is no additional data available |
27 | | // but the stream has not ended and the source will call |
28 | | // next again later when data is available. STATUS_WAIT |
29 | | // must not be used with the OPTIONS_SYNC option. |
30 | | STATUS_WAIT = 3, |
31 | | }; |
32 | | |
33 | | enum Options : int { |
34 | | OPTIONS_NONE = 0, |
35 | | |
36 | | // Indicates that the consumer is requesting the end |
37 | | // of the stream. |
38 | | OPTIONS_END = 1, |
39 | | |
40 | | // Indicates that the consumer requires the source to |
41 | | // invoke Next synchronously. If the source is |
42 | | // unable to provide data immediately but the |
43 | | // stream has not yet ended, it should call Next |
44 | | // using STATUS_BLOCK. When not set, the source |
45 | | // may call Next asynchronously. |
46 | | OPTIONS_SYNC = 2 |
47 | | }; |
48 | | |
49 | | // There are Sources and there are Consumers. |
50 | | // |
51 | | // Consumers get data by calling Source::Pull, |
52 | | // optionally passing along a status and allocated |
53 | | // buffer space for the source to fill, and a Next |
54 | | // function the Source will call when data is |
55 | | // available. |
56 | | // |
57 | | // The source calls Next to deliver the data. It can |
58 | | // choose to either use the allocated buffer space |
59 | | // provided by the consumer or it can allocate its own |
60 | | // buffers and push those instead. If it allocates |
61 | | // its own, it can send a Done function that the |
62 | | // Sink will call when it is done consuming the data. |
63 | | using Done = std::function<void(size_t)>; |
64 | | template <typename T> |
65 | | using Next = std::function<void(int, const T*, size_t count, Done done)>; |
66 | | |
67 | | template <typename T> |
68 | | class Source { |
69 | | public: |
70 | 2 | virtual ~Source() = default; node::bob::Source<node::DataQueue::Vec>::~Source() Line | Count | Source | 70 | 2 | virtual ~Source() = default; |
Unexecuted instantiation: node::bob::Source<ngtcp2_vec>::~Source() |
71 | | virtual int Pull( |
72 | | Next<T> next, |
73 | | int options, |
74 | | T* data, |
75 | | size_t count, |
76 | | size_t max_count_hint = kMaxCountHint) = 0; |
77 | | }; |
78 | | |
79 | | |
80 | | template <typename T> |
81 | | class SourceImpl : public Source<T> { |
82 | | public: |
83 | | int Pull( |
84 | | Next<T> next, |
85 | | int options, |
86 | | T* data, |
87 | | size_t count, |
88 | | size_t max_count_hint = kMaxCountHint) override; |
89 | | |
90 | | bool is_eos() const { return eos_; } |
91 | | |
92 | | protected: |
93 | | virtual int DoPull( |
94 | | Next<T> next, |
95 | | int options, |
96 | | T* data, |
97 | | size_t count, |
98 | | size_t max_count_hint) = 0; |
99 | | |
100 | | private: |
101 | | bool eos_ = false; |
102 | | }; |
103 | | |
104 | | } // namespace bob |
105 | | } // namespace node |
106 | | |
107 | | #endif // SRC_NODE_BOB_H_ |