xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/qbone/qbone_session_base.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_QUIC_QBONE_QBONE_SESSION_BASE_H_
6 #define QUICHE_QUIC_QBONE_QBONE_SESSION_BASE_H_
7 
8 #include "absl/strings/string_view.h"
9 #include "quiche/quic/core/quic_crypto_server_stream_base.h"
10 #include "quiche/quic/core/quic_crypto_stream.h"
11 #include "quiche/quic/core/quic_error_codes.h"
12 #include "quiche/quic/core/quic_session.h"
13 #include "quiche/quic/platform/api/quic_export.h"
14 #include "quiche/quic/qbone/qbone_packet_writer.h"
15 #include "quiche/quic/qbone/qbone_stream.h"
16 
17 namespace quic {
18 
19 class QUIC_EXPORT_PRIVATE QboneSessionBase : public QuicSession {
20  public:
21   QboneSessionBase(QuicConnection* connection, Visitor* owner,
22                    const QuicConfig& config,
23                    const ParsedQuicVersionVector& supported_versions,
24                    QbonePacketWriter* writer);
25   QboneSessionBase(const QboneSessionBase&) = delete;
26   QboneSessionBase& operator=(const QboneSessionBase&) = delete;
27   ~QboneSessionBase() override;
28 
29   // Overrides from QuicSession.
30   // This will ensure that the crypto session is created.
31   void Initialize() override;
32   // This will check if the packet is wholly contained.
33   void OnStreamFrame(const QuicStreamFrame& frame) override;
34   // Called whenever a MESSAGE frame is received.
35   void OnMessageReceived(absl::string_view message) override;
36 
37   virtual void ProcessPacketFromNetwork(absl::string_view packet) = 0;
38   virtual void ProcessPacketFromPeer(absl::string_view packet) = 0;
39 
40   // Returns the number of QBONE network packets that were received
41   // that fit into a single QuicStreamFrame and elided the creation of
42   // a QboneReadOnlyStream.
43   uint64_t GetNumEphemeralPackets() const;
44 
45   // Returns the number of QBONE network packets that were via
46   // multiple packets, requiring the creation of a QboneReadOnlyStream.
47   uint64_t GetNumStreamedPackets() const;
48 
49   // Returns the number of QBONE network packets that were received using QUIC
50   // MESSAGE frame.
51   uint64_t GetNumMessagePackets() const;
52 
53   // Returns the number of times sending a MESSAGE frame failed, and the session
54   // used an ephemeral stream instead.
55   uint64_t GetNumFallbackToStream() const;
56 
57   void set_writer(QbonePacketWriter* writer);
set_send_packets_as_messages(bool send_packets_as_messages)58   void set_send_packets_as_messages(bool send_packets_as_messages) {
59     send_packets_as_messages_ = send_packets_as_messages;
60   }
61 
62  protected:
63   virtual std::unique_ptr<QuicCryptoStream> CreateCryptoStream() = 0;
64 
65   // QuicSession interface implementation.
66   QuicCryptoStream* GetMutableCryptoStream() override;
67   const QuicCryptoStream* GetCryptoStream() const override;
68   QuicStream* CreateIncomingStream(QuicStreamId id) override;
69   QuicStream* CreateIncomingStream(PendingStream* pending) override;
70   bool ShouldKeepConnectionAlive() const override;
71 
MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id)72   bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id) override {
73     return true;
74   }
75 
76   QuicStream* CreateOutgoingStream();
77   std::unique_ptr<QuicStream> CreateDataStream(QuicStreamId id);
78   // Activates a QuicStream.  The session takes ownership of the stream, but
79   // returns an unowned pointer to the stream for convenience.
80   QuicStream* ActivateDataStream(std::unique_ptr<QuicStream> stream);
81 
82   // Accepts a given packet from the network and writes it out
83   // to the QUIC stream. This will create an ephemeral stream per
84   // packet. This function will return true if a stream was created
85   // and the packet sent. It will return false if the stream could not
86   // be created.
87   void SendPacketToPeer(absl::string_view packet);
88 
89   QbonePacketWriter* writer_;
90 
91   // If true, send QUIC DATAGRAM (aka MESSAGE) frames instead of ephemeral
92   // streams. Note that receiving DATAGRAM frames is always supported.
93   bool send_packets_as_messages_ = true;
94 
95  private:
96   // Used for the crypto handshake.
97   std::unique_ptr<QuicCryptoStream> crypto_stream_;
98 
99   // Statistics for the packets received by the session.
100   uint64_t num_ephemeral_packets_ = 0;
101   uint64_t num_message_packets_ = 0;
102   uint64_t num_streamed_packets_ = 0;
103 
104   // Number of times the connection has failed to send packets as MESSAGE frame
105   // and used streams as a fallback.
106   uint64_t num_fallback_to_stream_ = 0;
107 };
108 
109 }  // namespace quic
110 
111 #endif  // QUICHE_QUIC_QBONE_QBONE_SESSION_BASE_H_
112