Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / quic / quic_session.h
blobd23b684d4f20d4bf89c8c17d39db5a5fd1d12a40
1 // Copyright (c) 2012 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 // A QuicSession, which demuxes a single connection to individual streams.
7 #ifndef NET_QUIC_QUIC_SESSION_H_
8 #define NET_QUIC_QUIC_SESSION_H_
10 #include <vector>
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_connection.h"
17 #include "net/quic/quic_crypto_stream.h"
18 #include "net/quic/quic_data_stream.h"
19 #include "net/quic/quic_headers_stream.h"
20 #include "net/quic/quic_packet_creator.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_write_blocked_list.h"
23 #include "net/quic/reliable_quic_stream.h"
25 namespace net {
27 class QuicCryptoStream;
28 class QuicFlowController;
29 class ReliableQuicStream;
30 class SSLInfo;
31 class VisitorShim;
33 namespace test {
34 class QuicSessionPeer;
35 } // namespace test
37 class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
38 public:
39 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
40 enum CryptoHandshakeEvent {
41 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
42 // sent by a client and that subsequent packets will be encrypted. (Client
43 // only.)
44 ENCRYPTION_FIRST_ESTABLISHED,
45 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
46 // the server and thus the encryption key has been updated. Therefore the
47 // connection should resend any packets that were sent under
48 // ENCRYPTION_INITIAL. (Client only.)
49 ENCRYPTION_REESTABLISHED,
50 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
51 // our handshake. In a server it indicates that a full, valid client hello
52 // has been received. (Client and server.)
53 HANDSHAKE_CONFIRMED,
56 QuicSession(QuicConnection* connection,
57 uint32 max_flow_control_receive_window_bytes,
58 const QuicConfig& config);
60 virtual ~QuicSession();
62 // QuicConnectionVisitorInterface methods:
63 virtual void OnStreamFrames(
64 const std::vector<QuicStreamFrame>& frames) OVERRIDE;
65 virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
66 virtual void OnGoAway(const QuicGoAwayFrame& frame) OVERRIDE;
67 virtual void OnWindowUpdateFrames(
68 const std::vector<QuicWindowUpdateFrame>& frames) OVERRIDE;
69 virtual void OnBlockedFrames(
70 const std::vector<QuicBlockedFrame>& frames) OVERRIDE;
71 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
72 virtual void OnWriteBlocked() OVERRIDE {}
73 virtual void OnSuccessfulVersionNegotiation(
74 const QuicVersion& version) OVERRIDE;
75 virtual void OnCanWrite() OVERRIDE;
76 virtual bool WillingAndAbleToWrite() const OVERRIDE;
77 virtual bool HasPendingHandshake() const OVERRIDE;
78 virtual bool HasOpenDataStreams() const OVERRIDE;
80 // Called by the headers stream when headers have been received for a stream.
81 virtual void OnStreamHeaders(QuicStreamId stream_id,
82 base::StringPiece headers_data);
83 // Called by the headers stream when headers with a priority have been
84 // received for this stream. This method will only be called for server
85 // streams.
86 virtual void OnStreamHeadersPriority(QuicStreamId stream_id,
87 QuicPriority priority);
88 // Called by the headers stream when headers have been completely received
89 // for a stream. |fin| will be true if the fin flag was set in the headers
90 // frame.
91 virtual void OnStreamHeadersComplete(QuicStreamId stream_id,
92 bool fin,
93 size_t frame_len);
95 // Called by streams when they want to write data to the peer.
96 // Returns a pair with the number of bytes consumed from data, and a boolean
97 // indicating if the fin bit was consumed. This does not indicate the data
98 // has been sent on the wire: it may have been turned into a packet and queued
99 // if the socket was unexpectedly blocked.
100 // If provided, |ack_notifier_delegate| will be registered to be notified when
101 // we have seen ACKs for all packets resulting from this call.
102 virtual QuicConsumedData WritevData(
103 QuicStreamId id,
104 const IOVector& data,
105 QuicStreamOffset offset,
106 bool fin,
107 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
109 // Writes |headers| for the stream |id| to the dedicated headers stream.
110 // If |fin| is true, then no more data will be sent for the stream |id|.
111 // If provided, |ack_notifier_delegate| will be registered to be notified when
112 // we have seen ACKs for all packets resulting from this call.
113 size_t WriteHeaders(
114 QuicStreamId id,
115 const SpdyHeaderBlock& headers,
116 bool fin,
117 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
119 // Called by streams when they want to close the stream in both directions.
120 virtual void SendRstStream(QuicStreamId id,
121 QuicRstStreamErrorCode error,
122 QuicStreamOffset bytes_written);
124 // Called when the session wants to go away and not accept any new streams.
125 void SendGoAway(QuicErrorCode error_code, const std::string& reason);
127 // Removes the stream associated with 'stream_id' from the active stream map.
128 virtual void CloseStream(QuicStreamId stream_id);
130 // Returns true if outgoing packets will be encrypted, even if the server
131 // hasn't confirmed the handshake yet.
132 virtual bool IsEncryptionEstablished();
134 // For a client, returns true if the server has confirmed our handshake. For
135 // a server, returns true if a full, valid client hello has been received.
136 virtual bool IsCryptoHandshakeConfirmed();
138 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
139 virtual void OnConfigNegotiated();
141 // Called by the QuicCryptoStream when the handshake enters a new state.
143 // Clients will call this function in the order:
144 // ENCRYPTION_FIRST_ESTABLISHED
145 // zero or more ENCRYPTION_REESTABLISHED
146 // HANDSHAKE_CONFIRMED
148 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
149 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
151 // Called by the QuicCryptoStream when a handshake message is sent.
152 virtual void OnCryptoHandshakeMessageSent(
153 const CryptoHandshakeMessage& message);
155 // Called by the QuicCryptoStream when a handshake message is received.
156 virtual void OnCryptoHandshakeMessageReceived(
157 const CryptoHandshakeMessage& message);
159 // Returns mutable config for this session. Returned config is owned
160 // by QuicSession.
161 QuicConfig* config();
163 // Returns true if the stream existed previously and has been closed.
164 // Returns false if the stream is still active or if the stream has
165 // not yet been created.
166 bool IsClosedStream(QuicStreamId id);
168 QuicConnection* connection() { return connection_.get(); }
169 const QuicConnection* connection() const { return connection_.get(); }
170 size_t num_active_requests() const { return stream_map_.size(); }
171 const IPEndPoint& peer_address() const {
172 return connection_->peer_address();
174 QuicConnectionId connection_id() const {
175 return connection_->connection_id();
178 QuicPacketCreator::Options* options() { return connection()->options(); }
180 // Returns the number of currently open streams, including those which have
181 // been implicitly created, but excluding the reserved headers and crypto
182 // streams.
183 virtual size_t GetNumOpenStreams() const;
185 void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
187 // Returns true if the session has data to be sent, either queued in the
188 // connection, or in a write-blocked stream.
189 bool HasDataToWrite() const;
191 bool goaway_received() const {
192 return goaway_received_;
195 bool goaway_sent() const {
196 return goaway_sent_;
199 // Gets the SSL connection information.
200 virtual bool GetSSLInfo(SSLInfo* ssl_info) const;
202 QuicErrorCode error() const { return error_; }
204 bool is_server() const { return connection_->is_server(); }
206 uint32 max_flow_control_receive_window_bytes() {
207 return max_flow_control_receive_window_bytes_;
210 QuicFlowController* flow_controller() { return flow_controller_.get(); }
212 protected:
213 typedef base::hash_map<QuicStreamId, QuicDataStream*> DataStreamMap;
215 // Creates a new stream, owned by the caller, to handle a peer-initiated
216 // stream. Returns NULL and does error handling if the stream can not be
217 // created.
218 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) = 0;
220 // Create a new stream, owned by the caller, to handle a locally-initiated
221 // stream. Returns NULL if max streams have already been opened.
222 virtual QuicDataStream* CreateOutgoingDataStream() = 0;
224 // Return the reserved crypto stream.
225 virtual QuicCryptoStream* GetCryptoStream() = 0;
227 // Adds 'stream' to the active stream map.
228 virtual void ActivateStream(QuicDataStream* stream);
230 // Returns the stream id for a new stream.
231 QuicStreamId GetNextStreamId();
233 QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id);
235 QuicDataStream* GetDataStream(const QuicStreamId stream_id);
237 ReliableQuicStream* GetStream(const QuicStreamId stream_id);
239 // This is called after every call other than OnConnectionClose from the
240 // QuicConnectionVisitor to allow post-processing once the work has been done.
241 // In this case, it deletes streams given that it's safe to do so (no other
242 // operations are being done on the streams at this time)
243 virtual void PostProcessAfterData();
245 base::hash_map<QuicStreamId, QuicDataStream*>* streams() {
246 return &stream_map_;
249 const base::hash_map<QuicStreamId, QuicDataStream*>* streams() const {
250 return &stream_map_;
253 std::vector<QuicDataStream*>* closed_streams() { return &closed_streams_; }
255 size_t get_max_open_streams() const {
256 return max_open_streams_;
259 private:
260 friend class test::QuicSessionPeer;
261 friend class VisitorShim;
263 // Performs the work required to close |stream_id|. If |locally_reset|
264 // then the stream has been reset by this endpoint, not by the peer.
265 void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
267 scoped_ptr<QuicConnection> connection_;
269 scoped_ptr<QuicHeadersStream> headers_stream_;
271 // A shim to stand between the connection and the session, to handle stream
272 // deletions.
273 scoped_ptr<VisitorShim> visitor_shim_;
275 std::vector<QuicDataStream*> closed_streams_;
277 QuicConfig config_;
279 // Returns the maximum number of streams this connection can open.
280 size_t max_open_streams_;
282 // Map from StreamId to pointers to streams that are owned by the caller.
283 DataStreamMap stream_map_;
284 QuicStreamId next_stream_id_;
286 // Set of stream ids that have been "implicitly created" by receipt
287 // of a stream id larger than the next expected stream id.
288 base::hash_set<QuicStreamId> implicitly_created_streams_;
290 // A list of streams which need to write more data.
291 QuicWriteBlockedList write_blocked_streams_;
293 QuicStreamId largest_peer_created_stream_id_;
295 // The latched error with which the connection was closed.
296 QuicErrorCode error_;
298 // Whether a GoAway has been received.
299 bool goaway_received_;
300 // Whether a GoAway has been sent.
301 bool goaway_sent_;
303 // Indicate if there is pending data for the crypto stream.
304 bool has_pending_handshake_;
306 // Used for session level flow control.
307 scoped_ptr<QuicFlowController> flow_controller_;
309 // Initial flow control receive window size for new streams.
310 uint32 max_flow_control_receive_window_bytes_;
312 DISALLOW_COPY_AND_ASSIGN(QuicSession);
315 } // namespace net
317 #endif // NET_QUIC_QUIC_SESSION_H_