Line data Source code
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 27001 : Event::Dispatcher& dispatcher() const override { return dispatcher_; } 27 2370 : 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 : protected: 33 : void initializeDelayedCloseTimer(); 34 : 35 36820 : bool inDelayedClose() const { return delayed_close_state_ != DelayedCloseState::None; } 36 : 37 : void raiseConnectionEvent(ConnectionEvent event); 38 : 39 : virtual void closeConnectionImmediately() PURE; 40 : 41 28 : void closeConnectionImmediatelyWithDetails(absl::string_view local_close_details) { 42 28 : setLocalCloseReason(local_close_details); 43 28 : closeConnectionImmediately(); 44 28 : } 45 : 46 28 : absl::string_view localCloseReason() const override { return local_close_reason_; } 47 1083 : void setLocalCloseReason(absl::string_view local_close_reason) { 48 1083 : local_close_reason_ = std::string(local_close_reason); 49 1083 : } 50 : 51 : // States associated with delayed closing of the connection (i.e., when the underlying socket is 52 : // not immediately close()d as a result of a ConnectionImpl::close()). 53 : enum class DelayedCloseState { 54 : None, 55 : // The socket will be closed immediately after the buffer is flushed _or_ if a period of 56 : // inactivity after the last write event greater than or equal to delayed_close_timeout_ has 57 : // elapsed. 58 : CloseAfterFlush, 59 : // The socket will be closed after a grace period of delayed_close_timeout_ has elapsed after 60 : // the socket is flushed _or_ if a period of inactivity after the last write event greater than 61 : // or equal to delayed_close_timeout_ has elapsed. 62 : CloseAfterFlushAndWait 63 : }; 64 : DelayedCloseState delayed_close_state_{DelayedCloseState::None}; 65 : 66 : Event::TimerPtr delayed_close_timer_; 67 : std::chrono::milliseconds delayed_close_timeout_{0}; 68 : // Should be set with setLocalCloseReason. 69 : std::string local_close_reason_; 70 : Event::Dispatcher& dispatcher_; 71 : const uint64_t id_; 72 : std::list<ConnectionCallbacks*> callbacks_; 73 : std::unique_ptr<ConnectionStats> connection_stats_; 74 : 75 : private: 76 : // Callback issued when a delayed close timeout triggers. 77 : void onDelayedCloseTimeout(); 78 : }; 79 : 80 : } // namespace Network 81 : } // namespace Envoy