• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_STREAM_PIPE_H_
2 #define SRC_STREAM_PIPE_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "stream_base.h"
7 
8 namespace node {
9 
10 class StreamPipe : public AsyncWrap {
11  public:
12   ~StreamPipe() override;
13 
14   void Unpipe(bool is_in_deletion = false);
15 
16   static v8::Maybe<StreamPipe*> New(StreamBase* source,
17                                     StreamBase* sink,
18                                     v8::Local<v8::Object> obj);
19   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
20   static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
21   static void Unpipe(const v8::FunctionCallbackInfo<v8::Value>& args);
22   static void IsClosed(const v8::FunctionCallbackInfo<v8::Value>& args);
23   static void PendingWrites(const v8::FunctionCallbackInfo<v8::Value>& args);
24 
25   SET_NO_MEMORY_INFO()
26   SET_MEMORY_INFO_NAME(StreamPipe)
27   SET_SELF_SIZE(StreamPipe)
28 
29  private:
30   StreamPipe(StreamBase* source, StreamBase* sink, v8::Local<v8::Object> obj);
31 
32   inline StreamBase* source();
33   inline StreamBase* sink();
34 
35   int pending_writes_ = 0;
36   bool is_reading_ = false;
37   bool is_eof_ = false;
38   bool is_closed_ = true;
39   bool sink_destroyed_ = false;
40   bool source_destroyed_ = false;
41   bool uses_wants_write_ = false;
42 
43   // Set a default value so that when we’re coming from Start(), we know
44   // that we don’t want to read just yet.
45   // This will likely need to be changed when supporting streams without
46   // `OnStreamWantsWrite()` support.
47   size_t wanted_data_ = 0;
48 
49   void ProcessData(size_t nread, std::unique_ptr<v8::BackingStore> bs);
50 
51   class ReadableListener : public StreamListener {
52    public:
53     uv_buf_t OnStreamAlloc(size_t suggested_size) override;
54     void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
55     void OnStreamDestroy() override;
56   };
57 
58   class WritableListener : public StreamListener {
59    public:
60     uv_buf_t OnStreamAlloc(size_t suggested_size) override;
61     void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
62     void OnStreamAfterWrite(WriteWrap* w, int status) override;
63     void OnStreamAfterShutdown(ShutdownWrap* w, int status) override;
64     void OnStreamWantsWrite(size_t suggested_size) override;
65     void OnStreamDestroy() override;
66   };
67 
68   ReadableListener readable_listener_;
69   WritableListener writable_listener_;
70 };
71 
72 }  // namespace node
73 
74 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
75 
76 #endif  // SRC_STREAM_PIPE_H_
77