1
#pragma once
2

            
3
#include "envoy/event/dispatcher.h"
4

            
5
#include "source/common/common/logger.h"
6
#include "source/common/network/filter_manager_impl.h"
7

            
8
namespace Envoy {
9
namespace Network {
10

            
11
class ConnectionImplBase : public FilterManagerConnection,
12
                           protected Logger::Loggable<Logger::Id::connection> {
13
public:
14
  /**
15
   * Add a connection id to a hash key
16
   * @param hash_key the current hash key -- the function will only append to this vector
17
   * @param connection_id the 64-bit connection id
18
   */
19
  static void addIdToHashKey(std::vector<uint8_t>& hash_key, uint64_t connection_id);
20

            
21
  ConnectionImplBase(Event::Dispatcher& dispatcher, uint64_t id);
22

            
23
  // Network::Connection
24
  void addConnectionCallbacks(ConnectionCallbacks& cb) override;
25
  void removeConnectionCallbacks(ConnectionCallbacks& cb) override;
26
8307392
  Event::Dispatcher& dispatcher() const override { return dispatcher_; }
27
140481
  uint64_t id() const override { return id_; }
28
  void hashKey(std::vector<uint8_t>& hash) const override;
29
  void setConnectionStats(const ConnectionStats& stats) override;
30
  void setDelayedCloseTimeout(std::chrono::milliseconds timeout) override;
31

            
32
  // ScopeTrackedObject
33
  OptRef<const StreamInfo::StreamInfo> trackedStream() const override;
34

            
35
protected:
36
  void initializeDelayedCloseTimer();
37

            
38
8248502
  bool inDelayedClose() const { return delayed_close_state_ != DelayedCloseState::None; }
39

            
40
  void raiseConnectionEvent(ConnectionEvent event);
41

            
42
  virtual void closeConnectionImmediately() PURE;
43

            
44
1965
  void closeConnectionImmediatelyWithDetails(absl::string_view local_close_details) {
45
1965
    setLocalCloseReason(local_close_details);
46
1965
    closeConnectionImmediately();
47
1965
  }
48

            
49
7883
  absl::string_view localCloseReason() const override { return local_close_reason_; }
50
11937
  virtual void setLocalCloseReason(absl::string_view local_close_reason) {
51
11937
    local_close_reason_ = std::string(local_close_reason);
52
11937
  }
53

            
54
  // States associated with delayed closing of the connection (i.e., when the underlying socket is
55
  // not immediately close()d as a result of a ConnectionImpl::close()).
56
  enum class DelayedCloseState {
57
    None,
58
    // The socket will be closed immediately after the buffer is flushed _or_ if a period of
59
    // inactivity after the last write event greater than or equal to delayed_close_timeout_ has
60
    // elapsed.
61
    CloseAfterFlush,
62
    // The socket will be closed after a grace period of delayed_close_timeout_ has elapsed after
63
    // the socket is flushed _or_ if a period of inactivity after the last write event greater than
64
    // or equal to delayed_close_timeout_ has elapsed.
65
    CloseAfterFlushAndWait
66
  };
67
  DelayedCloseState delayed_close_state_{DelayedCloseState::None};
68

            
69
  Event::TimerPtr delayed_close_timer_;
70
  std::chrono::milliseconds delayed_close_timeout_{0};
71
  // Should be set with setLocalCloseReason.
72
  std::string local_close_reason_;
73
  Event::Dispatcher& dispatcher_;
74
  const uint64_t id_;
75
  std::list<ConnectionCallbacks*> callbacks_;
76
  std::unique_ptr<ConnectionStats> connection_stats_;
77

            
78
private:
79
  // Callback issued when a delayed close timeout triggers.
80
  void onDelayedCloseTimeout();
81
};
82

            
83
} // namespace Network
84
} // namespace Envoy