Initialize UserMetricsRecorder on Windows Ash and Ozone
[chromium-blink-merge.git] / net / tools / quic / quic_server_session.cc
blob6e64da50c9057e0144af2b90b5ce77e86a00c394
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.
5 #include "net/tools/quic/quic_server_session.h"
7 #include "base/logging.h"
8 #include "net/quic/crypto/cached_network_parameters.h"
9 #include "net/quic/quic_connection.h"
10 #include "net/quic/quic_flags.h"
11 #include "net/quic/reliable_quic_stream.h"
12 #include "net/tools/quic/quic_spdy_server_stream.h"
14 namespace net {
15 namespace tools {
17 QuicServerSession::QuicServerSession(const QuicConfig& config,
18 QuicConnection* connection,
19 QuicServerSessionVisitor* visitor)
20 : QuicSession(connection, config),
21 visitor_(visitor),
22 bandwidth_estimate_sent_to_client_(QuicBandwidth::Zero()),
23 last_scup_time_(QuicTime::Zero()),
24 last_scup_sequence_number_(0) {}
26 QuicServerSession::~QuicServerSession() {}
28 void QuicServerSession::InitializeSession(
29 const QuicCryptoServerConfig& crypto_config) {
30 QuicSession::InitializeSession();
31 crypto_stream_.reset(CreateQuicCryptoServerStream(crypto_config));
34 QuicCryptoServerStream* QuicServerSession::CreateQuicCryptoServerStream(
35 const QuicCryptoServerConfig& crypto_config) {
36 return new QuicCryptoServerStream(crypto_config, this);
39 void QuicServerSession::OnConfigNegotiated() {
40 QuicSession::OnConfigNegotiated();
42 if (!config()->HasReceivedConnectionOptions()) {
43 return;
46 // If the client has provided a bandwidth estimate from the same serving
47 // region, then pass it to the sent packet manager in preparation for possible
48 // bandwidth resumption.
49 const CachedNetworkParameters* cached_network_params =
50 crypto_stream_->previous_cached_network_params();
51 if (FLAGS_quic_enable_bandwidth_resumption_experiment &&
52 cached_network_params != nullptr &&
53 ContainsQuicTag(config()->ReceivedConnectionOptions(), kBWRE) &&
54 cached_network_params->serving_region() == serving_region_) {
55 connection()->ResumeConnectionState(*cached_network_params);
58 if (FLAGS_enable_quic_fec &&
59 ContainsQuicTag(config()->ReceivedConnectionOptions(), kFHDR)) {
60 // kFHDR config maps to FEC protection always for headers stream.
61 // TODO(jri): Add crypto stream in addition to headers for kHDR.
62 headers_stream_->set_fec_policy(FEC_PROTECT_ALWAYS);
66 void QuicServerSession::OnConnectionClosed(QuicErrorCode error,
67 bool from_peer) {
68 QuicSession::OnConnectionClosed(error, from_peer);
69 // In the unlikely event we get a connection close while doing an asynchronous
70 // crypto event, make sure we cancel the callback.
71 if (crypto_stream_.get() != nullptr) {
72 crypto_stream_->CancelOutstandingCallbacks();
74 visitor_->OnConnectionClosed(connection()->connection_id(), error);
77 void QuicServerSession::OnWriteBlocked() {
78 QuicSession::OnWriteBlocked();
79 visitor_->OnWriteBlocked(connection());
82 void QuicServerSession::OnCongestionWindowChange(QuicTime now) {
83 if (connection()->version() <= QUIC_VERSION_21) {
84 return;
87 // Only send updates when the application has no data to write.
88 if (HasDataToWrite()) {
89 return;
92 // If not enough time has passed since the last time we sent an update to the
93 // client, or not enough packets have been sent, then return early.
94 const QuicSentPacketManager& sent_packet_manager =
95 connection()->sent_packet_manager();
96 int64 srtt_ms =
97 sent_packet_manager.GetRttStats()->smoothed_rtt().ToMilliseconds();
98 int64 now_ms = now.Subtract(last_scup_time_).ToMilliseconds();
99 int64 packets_since_last_scup =
100 connection()->sequence_number_of_last_sent_packet() -
101 last_scup_sequence_number_;
102 if (now_ms < (kMinIntervalBetweenServerConfigUpdatesRTTs * srtt_ms) ||
103 now_ms < kMinIntervalBetweenServerConfigUpdatesMs ||
104 packets_since_last_scup < kMinPacketsBetweenServerConfigUpdates) {
105 return;
108 // If the bandwidth recorder does not have a valid estimate, return early.
109 const QuicSustainedBandwidthRecorder& bandwidth_recorder =
110 sent_packet_manager.SustainedBandwidthRecorder();
111 if (!bandwidth_recorder.HasEstimate()) {
112 return;
115 // The bandwidth recorder has recorded at least one sustained bandwidth
116 // estimate. Check that it's substantially different from the last one that
117 // we sent to the client, and if so, send the new one.
118 QuicBandwidth new_bandwidth_estimate = bandwidth_recorder.BandwidthEstimate();
120 int64 bandwidth_delta =
121 std::abs(new_bandwidth_estimate.ToBitsPerSecond() -
122 bandwidth_estimate_sent_to_client_.ToBitsPerSecond());
124 // Define "substantial" difference as a 50% increase or decrease from the
125 // last estimate.
126 bool substantial_difference =
127 bandwidth_delta >
128 0.5 * bandwidth_estimate_sent_to_client_.ToBitsPerSecond();
129 if (!substantial_difference) {
130 return;
133 bandwidth_estimate_sent_to_client_ = new_bandwidth_estimate;
134 DVLOG(1) << "Server: sending new bandwidth estimate (KBytes/s): "
135 << bandwidth_estimate_sent_to_client_.ToKBytesPerSecond();
137 // Include max bandwidth in the update.
138 QuicBandwidth max_bandwidth_estimate =
139 bandwidth_recorder.MaxBandwidthEstimate();
140 int32 max_bandwidth_timestamp = bandwidth_recorder.MaxBandwidthTimestamp();
142 // Fill the proto before passing it to the crypto stream to send.
143 CachedNetworkParameters cached_network_params;
144 cached_network_params.set_bandwidth_estimate_bytes_per_second(
145 bandwidth_estimate_sent_to_client_.ToBytesPerSecond());
146 cached_network_params.set_max_bandwidth_estimate_bytes_per_second(
147 max_bandwidth_estimate.ToBytesPerSecond());
148 cached_network_params.set_max_bandwidth_timestamp_seconds(
149 max_bandwidth_timestamp);
150 cached_network_params.set_min_rtt_ms(
151 sent_packet_manager.GetRttStats()->min_rtt().ToMilliseconds());
152 cached_network_params.set_previous_connection_state(
153 bandwidth_recorder.EstimateRecordedDuringSlowStart()
154 ? CachedNetworkParameters::SLOW_START
155 : CachedNetworkParameters::CONGESTION_AVOIDANCE);
156 cached_network_params.set_timestamp(
157 connection()->clock()->WallNow().ToUNIXSeconds());
158 if (!serving_region_.empty()) {
159 cached_network_params.set_serving_region(serving_region_);
162 crypto_stream_->SendServerConfigUpdate(&cached_network_params);
163 last_scup_time_ = now;
164 last_scup_sequence_number_ =
165 connection()->sequence_number_of_last_sent_packet();
168 bool QuicServerSession::ShouldCreateIncomingDataStream(QuicStreamId id) {
169 if (id % 2 == 0) {
170 DVLOG(1) << "Invalid incoming even stream_id:" << id;
171 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
172 return false;
174 if (GetNumOpenStreams() >= get_max_open_streams()) {
175 DVLOG(1) << "Failed to create a new incoming stream with id:" << id
176 << " Already " << GetNumOpenStreams() << " streams open (max "
177 << get_max_open_streams() << ").";
178 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS);
179 return false;
181 return true;
184 QuicDataStream* QuicServerSession::CreateIncomingDataStream(
185 QuicStreamId id) {
186 if (!ShouldCreateIncomingDataStream(id)) {
187 return nullptr;
190 return new QuicSpdyServerStream(id, this);
193 QuicDataStream* QuicServerSession::CreateOutgoingDataStream() {
194 DLOG(ERROR) << "Server push not yet supported";
195 return nullptr;
198 QuicCryptoServerStream* QuicServerSession::GetCryptoStream() {
199 return crypto_stream_.get();
202 } // namespace tools
203 } // namespace net