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