1
#pragma once
2

            
3
#include <memory>
4

            
5
#include "envoy/network/connection.h"
6

            
7
#include "source/common/common/logger.h"
8

            
9
namespace Envoy {
10
namespace Quic {
11

            
12
// Read ~32k bytes per connection by default, which is about the same as TCP.
13
static const uint32_t DEFAULT_PACKETS_TO_READ_PER_CONNECTION = 32u;
14

            
15
class QuicWriteEventCallback {
16
public:
17
6152
  virtual ~QuicWriteEventCallback() = default;
18
  // Called when QUIC finishes a write.
19
  virtual void onWriteEventDone() PURE;
20
};
21

            
22
// A base class of both the client and server connections which keeps stats and
23
// connection socket.
24
class QuicNetworkConnection : protected Logger::Loggable<Logger::Id::connection> {
25
public:
26
  QuicNetworkConnection(Network::ConnectionSocketPtr&& connection_socket);
27

            
28
  virtual ~QuicNetworkConnection();
29

            
30
  // Called by EnvoyQuicSession::setConnectionStats().
31
3092
  void setConnectionStats(const Network::Connection::ConnectionStats& stats) {
32
3092
    connection_stats_ = std::make_unique<Network::Connection::ConnectionStats>(stats);
33
3092
  }
34

            
35
  // Called in session Initialize().
36
  void setEnvoyConnection(Network::Connection& connection, QuicWriteEventCallback& write_callback);
37

            
38
63641
  const Network::ConnectionSocketPtr& connectionSocket() const {
39
63641
    return connection_sockets_.back();
40
63641
  }
41

            
42
  // Needed for ENVOY_CONN_LOG.
43
  uint64_t id() const;
44

            
45
protected:
46
  // REQUIRES: hasConnectionStats() == true.
47
108356
  Network::Connection::ConnectionStats& connectionStats() const { return *connection_stats_; }
48

            
49
463520
  bool hasConnectionStats() const { return connection_stats_ != nullptr; }
50

            
51
24
  void setConnectionSocket(Network::ConnectionSocketPtr&& connection_socket) {
52
24
    connection_sockets_.push_back(std::move(connection_socket));
53
24
  }
54

            
55
  void onWriteEventDone();
56

            
57
46
  Network::Connection* networkConnection() { return envoy_connection_; }
58

            
59
private:
60
  // TODO(danzh): populate stats.
61
  std::unique_ptr<Network::Connection::ConnectionStats> connection_stats_;
62
  // Hosts a list of active sockets, while only the last one is used for writing data.
63
  // Hosts a single default socket upon construction. New sockets can be pushed in later as a result
64
  // of QUIC connection migration.
65
  std::vector<Network::ConnectionSocketPtr> connection_sockets_;
66
  // Points to an instance of EnvoyQuicServerSession or EnvoyQuicClientSession.
67
  Network::Connection* envoy_connection_{nullptr};
68
  QuicWriteEventCallback* write_callback_{nullptr};
69
};
70

            
71
} // namespace Quic
72
} // namespace Envoy