summaryrefslogtreecommitdiff
path: root/src/stream_pipe.h
blob: 51a33b7ef69776beb59c510e02ce3bc96ebd435b (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
#ifndef SRC_STREAM_PIPE_H_
#define SRC_STREAM_PIPE_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "stream_base.h"

namespace node {

class StreamPipe : public AsyncWrap {
 public:
  StreamPipe(StreamBase* source, StreamBase* sink, v8::Local<v8::Object> obj);
  ~StreamPipe();

  void Unpipe();

  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
  static void Unpipe(const v8::FunctionCallbackInfo<v8::Value>& args);

  SET_NO_MEMORY_INFO()
  SET_MEMORY_INFO_NAME(StreamPipe)
  SET_SELF_SIZE(StreamPipe)

 private:
  inline StreamBase* source();
  inline StreamBase* sink();

  inline void ShutdownWritable();
  inline void FlushToWritable();

  bool is_reading_ = false;
  bool is_writing_ = false;
  bool is_eof_ = false;
  bool is_closed_ = true;
  bool sink_destroyed_ = false;
  bool source_destroyed_ = false;

  // Set a default value so that when we’re coming from Start(), we know
  // that we don’t want to read just yet.
  // This will likely need to be changed when supporting streams without
  // `OnStreamWantsWrite()` support.
  size_t wanted_data_ = 0;

  void ProcessData(size_t nread, const uv_buf_t& buf);

  class ReadableListener : public StreamListener {
   public:
    uv_buf_t OnStreamAlloc(size_t suggested_size) override;
    void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
    void OnStreamDestroy() override;
  };

  class WritableListener : public StreamListener {
   public:
    uv_buf_t OnStreamAlloc(size_t suggested_size) override;
    void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
    void OnStreamAfterWrite(WriteWrap* w, int status) override;
    void OnStreamAfterShutdown(ShutdownWrap* w, int status) override;
    void OnStreamWantsWrite(size_t suggested_size) override;
    void OnStreamDestroy() override;
  };

  ReadableListener readable_listener_;
  WritableListener writable_listener_;
};

}  // namespace node

#endif

#endif  // SRC_STREAM_PIPE_H_