1
#pragma once
2

            
3
#include "envoy/buffer/buffer.h"
4
#include "envoy/common/optref.h"
5
#include "envoy/common/pure.h"
6
#include "envoy/config/core/v3/base.pb.h"
7
#include "envoy/stream_info/filter_state.h"
8

            
9
namespace Envoy {
10
namespace Extensions {
11
namespace IoSocket {
12
namespace UserSpace {
13

            
14
// Shared state between peering user space IO handles.
15
class PassthroughState {
16
public:
17
96
  virtual ~PassthroughState() = default;
18

            
19
  /**
20
   * Initialize the passthrough state from the downstream. This should be
21
   * called at most once before `mergeInto`.
22
   */
23
  virtual void initialize(std::unique_ptr<envoy::config::core::v3::Metadata> metadata,
24
                          const StreamInfo::FilterState::Objects& filter_state_objects) PURE;
25

            
26
  /**
27
   * Merge the passthrough state into a recipient stream metadata and its
28
   * filter state. This should be called at most once after `initialize`.
29
   */
30
  virtual void mergeInto(envoy::config::core::v3::Metadata& metadata,
31
                         StreamInfo::FilterState& filter_state) PURE;
32
};
33

            
34
using PassthroughStateSharedPtr = std::shared_ptr<PassthroughState>;
35
using PassthroughStatePtr = std::unique_ptr<PassthroughState>;
36

            
37
/**
38
 * The interface for the peer as a writer and supplied read status query.
39
 */
40
class IoHandle {
41
public:
42
206
  virtual ~IoHandle() = default;
43

            
44
  /**
45
   * Called by the peer to indicate that it will not send any more data.
46
   */
47
  virtual void setEof() PURE;
48

            
49
  /**
50
   * @return true if the peer has indicated that it will not send any more data.
51
   */
52
  virtual bool hasReceivedEof() const PURE;
53

            
54
  /**
55
   * Raised when peer is destroyed. Sending any more data to the peer will fail.
56
   */
57
  virtual void onPeerDestroy() PURE;
58

            
59
  /**
60
   * Notify that consumable data arrived. The consumable data can be data in the receive buffer, or
61
   * the end of stream event.
62
   */
63
  virtual void setNewDataAvailable() PURE;
64

            
65
  /**
66
   * @return the buffer holding data received from the peer.
67
   */
68
  virtual Buffer::Instance* getReceiveBuffer() PURE;
69

            
70
  /**
71
   * @return true if the receive buffer can accept more data from the peer.
72
   */
73
  virtual bool canReceiveData() const PURE;
74

            
75
  /**
76
   * @return true if the peer is valid and its receive buffer can accept more data. This means that
77
   * write() calls to this handle will not block.
78
   */
79
  virtual bool isWritable() const PURE;
80

            
81
  /**
82
   * Raised by the peer when its receive buffer switches from high watermark to low watermark.
83
   */
84
  virtual void onPeerBufferLowWatermark() PURE;
85

            
86
  /**
87
   * @return true if the receive buffer is not empty or read_end is set. This means that read()
88
   * calls to this handle will not block.
89
   */
90
  virtual bool isReadable() const PURE;
91

            
92
  /**
93
   * @return shared state between peering user space IO handles.
94
   */
95
  virtual PassthroughStateSharedPtr passthroughState() PURE;
96
};
97
} // namespace UserSpace
98
} // namespace IoSocket
99
} // namespace Extensions
100
} // namespace Envoy