Complete SyncMessageFilter initialization after SyncChannel initialization
[chromium-blink-merge.git] / net / quic / quic_session.h
blob606fabb986e9b911cf738eb5ec9e15f5c997e8f6
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 <map>
11 #include <string>
12 #include <vector>
14 #include "build/build_config.h"
16 // TODO(rtenneti): Temporary while investigating crbug.com/473893.
17 // Note base::Debug::StackTrace() is not supported in NACL
18 // builds so conditionally disabled it there.
19 #ifndef OS_NACL
20 #define TEMP_INSTRUMENTATION_473893
21 #endif
23 #include "base/compiler_specific.h"
24 #include "base/containers/hash_tables.h"
25 #ifdef TEMP_INSTRUMENTATION_473893
26 #include "base/debug/stack_trace.h"
27 #endif
28 #include "base/memory/scoped_ptr.h"
29 #include "base/strings/string_piece.h"
30 #include "net/base/ip_endpoint.h"
31 #include "net/quic/quic_connection.h"
32 #include "net/quic/quic_crypto_stream.h"
33 #include "net/quic/quic_packet_creator.h"
34 #include "net/quic/quic_protocol.h"
35 #include "net/quic/quic_write_blocked_list.h"
36 #include "net/quic/reliable_quic_stream.h"
38 namespace net {
40 class QuicCryptoStream;
41 class QuicFlowController;
42 class ReliableQuicStream;
43 class VisitorShim;
45 namespace test {
46 class QuicSessionPeer;
47 } // namespace test
49 class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
50 public:
51 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
52 enum CryptoHandshakeEvent {
53 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
54 // sent by a client and that subsequent packets will be encrypted. (Client
55 // only.)
56 ENCRYPTION_FIRST_ESTABLISHED,
57 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
58 // the server and thus the encryption key has been updated. Therefore the
59 // connection should resend any packets that were sent under
60 // ENCRYPTION_INITIAL. (Client only.)
61 ENCRYPTION_REESTABLISHED,
62 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
63 // our handshake. In a server it indicates that a full, valid client hello
64 // has been received. (Client and server.)
65 HANDSHAKE_CONFIRMED,
68 QuicSession(QuicConnection* connection, const QuicConfig& config);
70 ~QuicSession() override;
72 virtual void Initialize();
74 // QuicConnectionVisitorInterface methods:
75 void OnStreamFrame(const QuicStreamFrame& frame) override;
76 void OnRstStream(const QuicRstStreamFrame& frame) override;
77 void OnGoAway(const QuicGoAwayFrame& frame) override;
78 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
79 void OnBlockedFrame(const QuicBlockedFrame& frame) override;
80 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override;
81 void OnWriteBlocked() override {}
82 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override;
83 void OnCanWrite() override;
84 void OnCongestionWindowChange(QuicTime now) override {}
85 bool WillingAndAbleToWrite() const override;
86 bool HasPendingHandshake() const override;
87 bool HasOpenDynamicStreams() const override;
89 // Called by streams when they want to write data to the peer.
90 // Returns a pair with the number of bytes consumed from data, and a boolean
91 // indicating if the fin bit was consumed. This does not indicate the data
92 // has been sent on the wire: it may have been turned into a packet and queued
93 // if the socket was unexpectedly blocked. |fec_protection| indicates if
94 // data is to be FEC protected. Note that data that is sent immediately
95 // following MUST_FEC_PROTECT data may get protected by falling within the
96 // same FEC group.
97 // If provided, |ack_notifier_delegate| will be registered to be notified when
98 // we have seen ACKs for all packets resulting from this call.
99 virtual QuicConsumedData WritevData(
100 QuicStreamId id,
101 const QuicIOVector& iov,
102 QuicStreamOffset offset,
103 bool fin,
104 FecProtection fec_protection,
105 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
107 // Called by streams when they want to close the stream in both directions.
108 virtual void SendRstStream(QuicStreamId id,
109 QuicRstStreamErrorCode error,
110 QuicStreamOffset bytes_written);
112 // Called when the session wants to go away and not accept any new streams.
113 void SendGoAway(QuicErrorCode error_code, const std::string& reason);
115 // Removes the stream associated with 'stream_id' from the active stream map.
116 virtual void CloseStream(QuicStreamId stream_id);
118 // Returns true if outgoing packets will be encrypted, even if the server
119 // hasn't confirmed the handshake yet.
120 virtual bool IsEncryptionEstablished();
122 // For a client, returns true if the server has confirmed our handshake. For
123 // a server, returns true if a full, valid client hello has been received.
124 virtual bool IsCryptoHandshakeConfirmed();
126 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
127 virtual void OnConfigNegotiated();
129 // Called by the QuicCryptoStream when the handshake enters a new state.
131 // Clients will call this function in the order:
132 // ENCRYPTION_FIRST_ESTABLISHED
133 // zero or more ENCRYPTION_REESTABLISHED
134 // HANDSHAKE_CONFIRMED
136 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
137 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
139 // Called by the QuicCryptoStream when a handshake message is sent.
140 virtual void OnCryptoHandshakeMessageSent(
141 const CryptoHandshakeMessage& message);
143 // Called by the QuicCryptoStream when a handshake message is received.
144 virtual void OnCryptoHandshakeMessageReceived(
145 const CryptoHandshakeMessage& message);
147 // Returns mutable config for this session. Returned config is owned
148 // by QuicSession.
149 QuicConfig* config();
151 // Returns true if the stream existed previously and has been closed.
152 // Returns false if the stream is still active or if the stream has
153 // not yet been created.
154 bool IsClosedStream(QuicStreamId id);
156 QuicConnection* connection() {
157 // TODO(rtenneti): Temporary while investigating crbug.com/473893
158 CrashIfInvalid();
159 return connection_.get();
161 const QuicConnection* connection() const {
162 // TODO(rtenneti): Temporary while investigating crbug.com/473893
163 CrashIfInvalid();
164 return connection_.get();
166 size_t num_active_requests() const { return dynamic_stream_map_.size(); }
167 const IPEndPoint& peer_address() const {
168 return connection_->peer_address();
170 QuicConnectionId connection_id() const {
171 return connection_->connection_id();
174 // Returns the number of currently open streams, including those which have
175 // been implicitly created, but excluding the reserved headers and crypto
176 // streams.
177 virtual size_t GetNumOpenStreams() const;
179 // Add the stream to the session's write-blocked list because it is blocked by
180 // connection-level flow control but not by its own stream-level flow control.
181 // The stream will be given a chance to write when a connection-level
182 // WINDOW_UPDATE arrives.
183 void MarkConnectionLevelWriteBlocked(QuicStreamId id, QuicPriority priority);
185 // Returns true if the session has data to be sent, either queued in the
186 // connection, or in a write-blocked stream.
187 bool HasDataToWrite() const;
189 bool goaway_received() const { return goaway_received_; }
191 bool goaway_sent() const { return goaway_sent_; }
193 QuicErrorCode error() const { return error_; }
195 Perspective perspective() const { return connection_->perspective(); }
197 QuicFlowController* flow_controller() { return &flow_controller_; }
199 // Returns true if connection is flow controller blocked.
200 bool IsConnectionFlowControlBlocked() const;
202 // Returns true if any stream is flow controller blocked.
203 bool IsStreamFlowControlBlocked();
205 // Returns true if this is a secure QUIC session.
206 bool IsSecure() const { return connection()->is_secure(); }
208 size_t get_max_open_streams() const { return max_open_streams_; }
210 ReliableQuicStream* GetStream(const QuicStreamId stream_id);
212 // Mark a stream as draining.
213 void StreamDraining(QuicStreamId id);
215 protected:
216 typedef base::hash_map<QuicStreamId, ReliableQuicStream*> StreamMap;
218 // Creates a new stream, owned by the caller, to handle a peer-initiated
219 // stream. Returns nullptr and does error handling if the stream can not be
220 // created.
221 virtual ReliableQuicStream* CreateIncomingDynamicStream(QuicStreamId id) = 0;
223 // Create a new stream, owned by the caller, to handle a locally-initiated
224 // stream. Returns nullptr if max streams have already been opened.
225 virtual ReliableQuicStream* CreateOutgoingDynamicStream() = 0;
227 // Return the reserved crypto stream.
228 virtual QuicCryptoStream* GetCryptoStream() = 0;
230 // Adds 'stream' to the active stream map.
231 virtual void ActivateStream(ReliableQuicStream* stream);
233 // Returns the stream id for a new stream.
234 QuicStreamId GetNextStreamId();
236 ReliableQuicStream* GetIncomingDynamicStream(QuicStreamId stream_id);
238 ReliableQuicStream* GetDynamicStream(const QuicStreamId stream_id);
240 // This is called after every call other than OnConnectionClose from the
241 // QuicConnectionVisitor to allow post-processing once the work has been done.
242 // In this case, it deletes streams given that it's safe to do so (no other
243 // operations are being done on the streams at this time)
244 virtual void PostProcessAfterData();
246 StreamMap& static_streams() { return static_stream_map_; }
247 const StreamMap& static_streams() const { return static_stream_map_; }
249 StreamMap& dynamic_streams() { return dynamic_stream_map_; }
250 const StreamMap& dynamic_streams() const { return dynamic_stream_map_; }
252 std::vector<ReliableQuicStream*>* closed_streams() {
253 return &closed_streams_;
256 void set_max_open_streams(size_t max_open_streams);
258 void set_largest_peer_created_stream_id(
259 QuicStreamId largest_peer_created_stream_id) {
260 largest_peer_created_stream_id_ = largest_peer_created_stream_id;
263 private:
264 friend class test::QuicSessionPeer;
265 friend class VisitorShim;
267 #ifdef TEMP_INSTRUMENTATION_473893
268 // TODO(rtenneti): Temporary while investigating crbug.com/473893
269 enum Liveness {
270 ALIVE = 0xCA11AB13,
271 DEAD = 0xDEADBEEF,
273 #endif
275 // Performs the work required to close |stream_id|. If |locally_reset|
276 // then the stream has been reset by this endpoint, not by the peer.
277 void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
279 // When a stream is closed locally, it may not yet know how many bytes the
280 // peer sent on that stream.
281 // When this data arrives (via stream frame w. FIN, or RST) this method
282 // is called, and correctly updates the connection level flow controller.
283 void UpdateFlowControlOnFinalReceivedByteOffset(
284 QuicStreamId id, QuicStreamOffset final_byte_offset);
286 // Called in OnConfigNegotiated when we receive a new stream level flow
287 // control window in a negotiated config. Closes the connection if invalid.
288 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window);
290 // Called in OnConfigNegotiated when we receive a new connection level flow
291 // control window in a negotiated config. Closes the connection if invalid.
292 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window);
294 // Called in OnConfigNegotiated when auto-tuning is enabled for flow
295 // control receive windows.
296 void EnableAutoTuneReceiveWindow();
298 // TODO(rtenneti): Temporary while investigating crbug.com/473893
299 void CrashIfInvalid() const;
301 // Keep track of highest received byte offset of locally closed streams, while
302 // waiting for a definitive final highest offset from the peer.
303 std::map<QuicStreamId, QuicStreamOffset>
304 locally_closed_streams_highest_offset_;
306 scoped_ptr<QuicConnection> connection_;
308 // A shim to stand between the connection and the session, to handle stream
309 // deletions.
310 scoped_ptr<VisitorShim> visitor_shim_;
312 std::vector<ReliableQuicStream*> closed_streams_;
314 QuicConfig config_;
316 // Returns the maximum number of streams this connection can open.
317 size_t max_open_streams_;
319 // Static streams, such as crypto and header streams. Owned by child classes
320 // that create these streams.
321 StreamMap static_stream_map_;
323 // Map from StreamId to pointers to streams that are owned by the caller.
324 StreamMap dynamic_stream_map_;
325 QuicStreamId next_stream_id_;
327 // Set of stream ids that have been "implicitly created" by receipt
328 // of a stream id larger than the next expected stream id.
329 base::hash_set<QuicStreamId> implicitly_created_streams_;
331 // Set of stream ids that are "draining" -- a FIN has been sent and received,
332 // but the stream object still exists because not all the received data has
333 // been consumed.
334 base::hash_set<QuicStreamId> draining_streams_;
336 // A list of streams which need to write more data.
337 QuicWriteBlockedList write_blocked_streams_;
339 QuicStreamId largest_peer_created_stream_id_;
341 // The latched error with which the connection was closed.
342 QuicErrorCode error_;
344 // Used for connection-level flow control.
345 QuicFlowController flow_controller_;
347 // Whether a GoAway has been received.
348 bool goaway_received_;
349 // Whether a GoAway has been sent.
350 bool goaway_sent_;
352 // Indicate if there is pending data for the crypto stream.
353 bool has_pending_handshake_;
355 #ifdef TEMP_INSTRUMENTATION_473893
356 // TODO(rtenneti): Temporary while investigating crbug.com/473893
357 Liveness liveness_ = ALIVE;
358 base::debug::StackTrace stack_trace_;
359 #endif
361 DISALLOW_COPY_AND_ASSIGN(QuicSession);
364 } // namespace net
366 #endif // NET_QUIC_QUIC_SESSION_H_