blob: 0c414d8aba04429034ed6601aa89ea25f1371d69 [file] [log] [blame]
QUICHE teamc9b2cec2019-01-07 17:54:15 -05001// Copyright (c) 2017 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_QUARTC_QUARTC_SESSION_H_
6#define QUICHE_QUIC_QUARTC_QUARTC_SESSION_H_
7
8#include <memory>
9#include <string>
10
11#include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h"
12#include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream.h"
13#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
14#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
15#include "net/third_party/quiche/src/quic/core/quic_session.h"
16#include "net/third_party/quiche/src/quic/core/quic_types.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
18#include "net/third_party/quiche/src/quic/quartc/quartc_packet_writer.h"
19#include "net/third_party/quiche/src/quic/quartc/quartc_stream.h"
20
21namespace quic {
22
QUICHE teamc9b2cec2019-01-07 17:54:15 -050023// QuartcSession owns and manages a QUIC connection.
24class QUIC_EXPORT_PRIVATE QuartcSession
25 : public QuicSession,
QUICHE team2bfc7542019-02-26 14:36:06 -050026 public QuartcPacketTransport::Delegate {
QUICHE teamc9b2cec2019-01-07 17:54:15 -050027 public:
28 QuartcSession(std::unique_ptr<QuicConnection> connection,
QUICHE team2bfc7542019-02-26 14:36:06 -050029 Visitor* visitor,
QUICHE teamc9b2cec2019-01-07 17:54:15 -050030 const QuicConfig& config,
31 const ParsedQuicVersionVector& supported_versions,
QUICHE team2bfc7542019-02-26 14:36:06 -050032 const QuicClock* clock);
QUICHE teamc9b2cec2019-01-07 17:54:15 -050033 QuartcSession(const QuartcSession&) = delete;
34 QuartcSession& operator=(const QuartcSession&) = delete;
35 ~QuartcSession() override;
36
37 // QuicSession overrides.
QUICHE teamc9b2cec2019-01-07 17:54:15 -050038 QuartcStream* CreateOutgoingBidirectionalStream();
39
40 // Sends short unreliable message using quic message frame (message must fit
41 // in one quic packet). If connection is blocked by congestion control,
42 // message will be queued and resent later after receiving an OnCanWrite
43 // notification.
44 //
45 // Message size must be <= GetLargestMessagePayload().
46 //
47 // Supported in quic version 45 or later.
48 //
49 // Returns false and logs error if message is too long or session does not
50 // support SendMessage API. Other unexpected errors during send will not be
51 // returned, because messages can be sent later if connection is congestion
52 // controlled.
53 bool SendOrQueueMessage(QuicString message);
54
55 // Returns largest message payload acceptable in SendQuartcMessage.
56 QuicPacketLength GetLargestMessagePayload() const {
57 return connection()->GetLargestMessagePayload();
58 }
59
60 // Return true if transport support message frame.
61 bool CanSendMessage() const {
QUICHE team2bfc7542019-02-26 14:36:06 -050062 return connection()->transport_version() > QUIC_VERSION_44;
QUICHE teamc9b2cec2019-01-07 17:54:15 -050063 }
64
65 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) override;
66
67 // QuicConnectionVisitorInterface overrides.
68 void OnCongestionWindowChange(QuicTime now) override;
69
70 void OnCanWrite() override;
71
72 void OnConnectionClosed(QuicErrorCode error,
73 const QuicString& error_details,
74 ConnectionCloseSource source) override;
75
76 // QuartcSession methods.
QUICHE team2bfc7542019-02-26 14:36:06 -050077 virtual void StartCryptoHandshake() = 0;
QUICHE teamc9b2cec2019-01-07 17:54:15 -050078
79 // Closes the connection with the given human-readable error details.
80 // The connection closes with the QUIC_CONNECTION_CANCELLED error code to
81 // indicate the application closed it.
82 //
83 // Informs the peer that the connection has been closed. This prevents the
84 // peer from waiting until the connection times out.
85 //
86 // Cleans up the underlying QuicConnection's state. Closing the connection
87 // makes it safe to delete the QuartcSession.
88 void CloseConnection(const QuicString& details);
89
90 // If the given stream is still open, sends a reset frame to cancel it.
91 // Note: This method cancels a stream by QuicStreamId rather than by pointer
92 // (or by a method on QuartcStream) because QuartcSession (and not
93 // the caller) owns the streams. Streams may finish and be deleted before the
94 // caller tries to cancel them, rendering the caller's pointers invalid.
95 void CancelStream(QuicStreamId stream_id);
96
97 // Callbacks called by the QuartcSession to notify the user of the
98 // QuartcSession of certain events.
99 class Delegate {
100 public:
101 virtual ~Delegate() {}
102
QUICHE teamdc5ce1a2019-01-30 16:01:47 -0500103 // Called when the crypto handshake is complete. Crypto handshake on the
104 // client is only completed _after_ SHLO is received, but we can actually
105 // start sending media data right after CHLO is sent.
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500106 virtual void OnCryptoHandshakeComplete() = 0;
107
QUICHE teamdc5ce1a2019-01-30 16:01:47 -0500108 // Connection can be writable even before crypto handshake is complete.
109 // In particular, on the client, we can start sending data after sending
110 // full CHLO, without waiting for SHLO. This reduces a send delay by 1-rtt.
111 //
112 // This may be called multiple times.
113 virtual void OnConnectionWritable() = 0;
114
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500115 // Called when a new stream is received from the remote endpoint.
116 virtual void OnIncomingStream(QuartcStream* stream) = 0;
117
118 // Called when network parameters change in response to an ack frame.
119 virtual void OnCongestionControlChange(QuicBandwidth bandwidth_estimate,
120 QuicBandwidth pacing_rate,
121 QuicTime::Delta latest_rtt) = 0;
122
123 // Called when the connection is closed. This means all of the streams will
124 // be closed and no new streams can be created.
125 virtual void OnConnectionClosed(QuicErrorCode error_code,
126 const QuicString& error_details,
127 ConnectionCloseSource source) = 0;
128
129 // Called when message (sent as SendMessage) is received.
130 virtual void OnMessageReceived(QuicStringPiece message) = 0;
131
132 // TODO(zhihuang): Add proof verification.
133 };
134
135 // The |delegate| is not owned by QuartcSession.
136 void SetDelegate(Delegate* session_delegate);
137
138 // Called when CanWrite() changes from false to true.
139 void OnTransportCanWrite() override;
140
141 // Called when a packet has been received and should be handled by the
142 // QuicConnection.
143 void OnTransportReceived(const char* data, size_t data_len) override;
144
145 void OnMessageReceived(QuicStringPiece message) override;
146
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500147 // Returns number of queued (not sent) messages submitted by
148 // SendOrQueueMessage. Messages are queued if connection is congestion
149 // controlled.
150 size_t send_message_queue_size() const { return send_message_queue_.size(); }
151
152 protected:
153 // QuicSession override.
154 QuicStream* CreateIncomingStream(QuicStreamId id) override;
155 QuicStream* CreateIncomingStream(PendingStream pending) override;
156
157 std::unique_ptr<QuartcStream> CreateDataStream(QuicStreamId id,
158 spdy::SpdyPriority priority);
159 std::unique_ptr<QuartcStream> CreateDataStream(PendingStream pending,
160 spdy::SpdyPriority priority);
161 // Activates a QuartcStream. The session takes ownership of the stream, but
162 // returns an unowned pointer to the stream for convenience.
163 QuartcStream* ActivateDataStream(std::unique_ptr<QuartcStream> stream);
164
165 void ResetStream(QuicStreamId stream_id, QuicRstStreamErrorCode error);
166
QUICHE team2bfc7542019-02-26 14:36:06 -0500167 const QuicClock* clock() { return clock_; }
168
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500169 private:
170 std::unique_ptr<QuartcStream> InitializeDataStream(
171 std::unique_ptr<QuartcStream> stream,
172 spdy::SpdyPriority priority);
173
174 void ProcessSendMessageQueue();
175
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500176 // Take ownership of the QuicConnection. Note: if |connection_| changes,
177 // the new value of |connection_| must be given to |packet_writer_| before any
178 // packets are written. Otherwise, |packet_writer_| will crash.
179 std::unique_ptr<QuicConnection> connection_;
QUICHE team2bfc7542019-02-26 14:36:06 -0500180
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500181 // For recording packet receipt time
182 const QuicClock* clock_;
QUICHE team2bfc7542019-02-26 14:36:06 -0500183
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500184 // Not owned by QuartcSession.
185 Delegate* session_delegate_ = nullptr;
QUICHE team2bfc7542019-02-26 14:36:06 -0500186
187 // Options passed to the packet writer for each packet.
188 std::unique_ptr<QuartcPerPacketOptions> per_packet_options_;
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500189
190 // Queue of pending messages sent by SendQuartcMessage that were not sent
191 // yet or blocked by congestion control. Messages are queued in the order
192 // of sent by SendOrQueueMessage().
193 QuicDeque<QuicString> send_message_queue_;
194};
195
QUICHE team2bfc7542019-02-26 14:36:06 -0500196class QUIC_EXPORT_PRIVATE QuartcClientSession
197 : public QuartcSession,
198 public QuicCryptoClientStream::ProofHandler {
199 public:
200 QuartcClientSession(
201 std::unique_ptr<QuicConnection> connection,
202 const QuicConfig& config,
203 const ParsedQuicVersionVector& supported_versions,
204 const QuicClock* clock,
205 std::unique_ptr<QuartcPacketWriter> packet_writer,
206 std::unique_ptr<QuicCryptoClientConfig> client_crypto_config,
207 QuicStringPiece server_crypto_config);
208 QuartcClientSession(const QuartcClientSession&) = delete;
209 QuartcClientSession& operator=(const QuartcClientSession&) = delete;
210
211 ~QuartcClientSession() override;
212
213 // Initialize should not be called on a QuartcSession. Instead, call
214 // StartCryptoHandshake().
215 // TODO(mellem): Move creation of the crypto stream into Initialize() and
216 // remove StartCryptoHandshake() to bring QuartcSession in line with other
217 // implementations of QuicSession, which can be started by calling
218 // Initialize().
219 void Initialize() override;
220
221 // Accessors for the client crypto stream.
222 QuicCryptoStream* GetMutableCryptoStream() override;
223 const QuicCryptoStream* GetCryptoStream() const override;
224
225 // Initializes the session and sends a handshake.
226 void StartCryptoHandshake() override;
227
228 // ProofHandler overrides.
229 void OnProofValid(const QuicCryptoClientConfig::CachedState& cached) override;
230
231 // Called by the client crypto handshake when proof verification details
232 // become available, either because proof verification is complete, or when
233 // cached details are used.
234 void OnProofVerifyDetailsAvailable(
235 const ProofVerifyDetails& verify_details) override;
236
237 private:
238 // Packet writer used by |connection_|.
239 std::unique_ptr<QuartcPacketWriter> packet_writer_;
240
241 // Config for QUIC crypto stream.
242 std::unique_ptr<QuicCryptoClientConfig> client_crypto_config_;
243
244 // Client perspective crypto stream.
245 std::unique_ptr<QuicCryptoClientStream> crypto_stream_;
246
247 const QuicString server_config_;
248};
249
250class QUIC_EXPORT_PRIVATE QuartcServerSession : public QuartcSession {
251 public:
252 QuartcServerSession(std::unique_ptr<QuicConnection> connection,
253 Visitor* visitor,
254 const QuicConfig& config,
255 const ParsedQuicVersionVector& supported_versions,
256 const QuicClock* clock,
257 const QuicCryptoServerConfig* server_crypto_config,
258 QuicCompressedCertsCache* const compressed_certs_cache,
259 QuicCryptoServerStream::Helper* const stream_helper);
260 QuartcServerSession(const QuartcServerSession&) = delete;
261 QuartcServerSession& operator=(const QuartcServerSession&) = delete;
262
263 // Accessors for the server crypto stream.
264 QuicCryptoStream* GetMutableCryptoStream() override;
265 const QuicCryptoStream* GetCryptoStream() const override;
266
267 // Initializes the session and prepares to receive a handshake.
268 void StartCryptoHandshake() override;
269
270 private:
271 // Config for QUIC crypto stream.
272 const QuicCryptoServerConfig* server_crypto_config_;
273
274 // Used by QUIC crypto server stream to track most recently compressed certs.
275 QuicCompressedCertsCache* const compressed_certs_cache_;
276
277 // This helper is needed to create QuicCryptoServerStream.
278 QuicCryptoServerStream::Helper* const stream_helper_;
279
280 // Server perspective crypto stream.
281 std::unique_ptr<QuicCryptoServerStream> crypto_stream_;
282};
283
QUICHE teamc9b2cec2019-01-07 17:54:15 -0500284} // namespace quic
285
286#endif // QUICHE_QUIC_QUARTC_QUARTC_SESSION_H_