xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/legacy_quic_stream_id_manager.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2018 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 #ifndef QUICHE_QUIC_CORE_LEGACY_QUIC_STREAM_ID_MANAGER_H_
5 #define QUICHE_QUIC_CORE_LEGACY_QUIC_STREAM_ID_MANAGER_H_
6 
7 #include "absl/container/flat_hash_set.h"
8 #include "quiche/quic/core/quic_stream_id_manager.h"
9 #include "quiche/quic/core/quic_types.h"
10 #include "quiche/quic/core/quic_versions.h"
11 
12 namespace quic {
13 
14 namespace test {
15 class QuicSessionPeer;
16 }  // namespace test
17 
18 class QuicSession;
19 
20 // Manages Google QUIC stream IDs. This manager is responsible for two
21 // questions: 1) can next outgoing stream ID be allocated (if yes, what is the
22 // next outgoing stream ID) and 2) can a new incoming stream be opened.
23 class QUICHE_EXPORT LegacyQuicStreamIdManager {
24  public:
25   LegacyQuicStreamIdManager(Perspective perspective,
26                             QuicTransportVersion transport_version,
27                             size_t max_open_outgoing_streams,
28                             size_t max_open_incoming_streams);
29 
30   ~LegacyQuicStreamIdManager();
31 
32   // Returns true if the next outgoing stream ID can be allocated.
33   bool CanOpenNextOutgoingStream() const;
34 
35   // Returns true if a new incoming stream can be opened.
36   bool CanOpenIncomingStream() const;
37 
38   // Returns false when increasing the largest created stream id to |id| would
39   // violate the limit, so the connection should be closed.
40   bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId id);
41 
42   // Returns true if |id| is still available.
43   bool IsAvailableStream(QuicStreamId id) const;
44 
45   // Returns the stream ID for a new outgoing stream, and increments the
46   // underlying counter.
47   QuicStreamId GetNextOutgoingStreamId();
48 
49   // Called when a new stream is open.
50   void ActivateStream(bool is_incoming);
51 
52   // Called when a stream ID is closed.
53   void OnStreamClosed(bool is_incoming);
54 
55   // Return true if |id| is peer initiated.
56   bool IsIncomingStream(QuicStreamId id) const;
57 
58   size_t MaxAvailableStreams() const;
59 
set_max_open_incoming_streams(size_t max_open_incoming_streams)60   void set_max_open_incoming_streams(size_t max_open_incoming_streams) {
61     max_open_incoming_streams_ = max_open_incoming_streams;
62   }
63 
set_max_open_outgoing_streams(size_t max_open_outgoing_streams)64   void set_max_open_outgoing_streams(size_t max_open_outgoing_streams) {
65     max_open_outgoing_streams_ = max_open_outgoing_streams;
66   }
67 
set_largest_peer_created_stream_id(QuicStreamId largest_peer_created_stream_id)68   void set_largest_peer_created_stream_id(
69       QuicStreamId largest_peer_created_stream_id) {
70     largest_peer_created_stream_id_ = largest_peer_created_stream_id;
71   }
72 
max_open_incoming_streams()73   size_t max_open_incoming_streams() const {
74     return max_open_incoming_streams_;
75   }
76 
max_open_outgoing_streams()77   size_t max_open_outgoing_streams() const {
78     return max_open_outgoing_streams_;
79   }
80 
next_outgoing_stream_id()81   QuicStreamId next_outgoing_stream_id() const {
82     return next_outgoing_stream_id_;
83   }
84 
largest_peer_created_stream_id()85   QuicStreamId largest_peer_created_stream_id() const {
86     return largest_peer_created_stream_id_;
87   }
88 
89   size_t GetNumAvailableStreams() const;
90 
num_open_incoming_streams()91   size_t num_open_incoming_streams() const {
92     return num_open_incoming_streams_;
93   }
num_open_outgoing_streams()94   size_t num_open_outgoing_streams() const {
95     return num_open_outgoing_streams_;
96   }
97 
98  private:
99   friend class test::QuicSessionPeer;
100 
101   const Perspective perspective_;
102   const QuicTransportVersion transport_version_;
103 
104   // The maximum number of outgoing streams this connection can open.
105   size_t max_open_outgoing_streams_;
106 
107   // The maximum number of incoming streams this connection will allow.
108   size_t max_open_incoming_streams_;
109 
110   // The ID to use for the next outgoing stream.
111   QuicStreamId next_outgoing_stream_id_;
112 
113   // Set of stream ids that are less than the largest stream id that has been
114   // received, but are nonetheless available to be created.
115   absl::flat_hash_set<QuicStreamId> available_streams_;
116 
117   QuicStreamId largest_peer_created_stream_id_;
118 
119   // A counter for peer initiated open streams.
120   size_t num_open_incoming_streams_;
121 
122   // A counter for self initiated open streams.
123   size_t num_open_outgoing_streams_;
124 };
125 
126 }  // namespace quic
127 
128 #endif  // QUICHE_QUIC_CORE_LEGACY_QUIC_STREAM_ID_MANAGER_H_
129