| Index: net/tools/quic/quic_simple_client.cc
|
| diff --git a/net/tools/quic/quic_simple_client.cc b/net/tools/quic/quic_simple_client.cc
|
| index 261086b6dcf692223962c22a6a7391f46399741d..d3d811aa935bed172ead5f86c817a533ef8736ad 100644
|
| --- a/net/tools/quic/quic_simple_client.cc
|
| +++ b/net/tools/quic/quic_simple_client.cc
|
| @@ -13,7 +13,6 @@
|
| #include "net/quic/crypto/quic_random.h"
|
| #include "net/quic/quic_connection.h"
|
| #include "net/quic/quic_connection_helper.h"
|
| -#include "net/quic/quic_crypto_client_stream.h"
|
| #include "net/quic/quic_default_packet_writer.h"
|
| #include "net/quic/quic_flags.h"
|
| #include "net/quic/quic_protocol.h"
|
| @@ -37,12 +36,11 @@ void QuicSimpleClient::ClientQuicDataToResend::Resend() {
|
| QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address,
|
| const QuicServerId& server_id,
|
| const QuicVersionVector& supported_versions)
|
| - : server_address_(server_address),
|
| - server_id_(server_id),
|
| + : QuicClientBase(server_id, supported_versions, QuicConfig()),
|
| + server_address_(server_address),
|
| local_port_(0),
|
| helper_(CreateQuicConnectionHelper()),
|
| initialized_(false),
|
| - supported_versions_(supported_versions),
|
| packet_reader_started_(false),
|
| weak_factory_(this) {}
|
|
|
| @@ -50,18 +48,11 @@ QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address,
|
| const QuicServerId& server_id,
|
| const QuicVersionVector& supported_versions,
|
| const QuicConfig& config)
|
| - : server_address_(server_address),
|
| - server_id_(server_id),
|
| - config_(config),
|
| + : QuicClientBase(server_id, supported_versions, config),
|
| + server_address_(server_address),
|
| local_port_(0),
|
| helper_(CreateQuicConnectionHelper()),
|
| initialized_(false),
|
| - supported_versions_(supported_versions),
|
| - initial_max_packet_length_(0),
|
| - num_stateless_rejects_received_(0),
|
| - num_sent_client_hellos_(0),
|
| - connection_error_(QUIC_NO_ERROR),
|
| - connected_or_attempting_connect_(false),
|
| packet_reader_started_(false),
|
| weak_factory_(this) {}
|
|
|
| @@ -77,10 +68,7 @@ QuicSimpleClient::~QuicSimpleClient() {
|
| bool QuicSimpleClient::Initialize() {
|
| DCHECK(!initialized_);
|
|
|
| - num_sent_client_hellos_ = 0;
|
| - num_stateless_rejects_received_ = 0;
|
| - connection_error_ = QUIC_NO_ERROR;
|
| - connected_or_attempting_connect_ = false;
|
| + QuicClientBase::Initialize();
|
|
|
| if (!CreateUDPSocket()) {
|
| return false;
|
| @@ -90,17 +78,6 @@ bool QuicSimpleClient::Initialize() {
|
| return true;
|
| }
|
|
|
| -QuicSimpleClient::DummyPacketWriterFactory::DummyPacketWriterFactory(
|
| - QuicPacketWriter* writer)
|
| - : writer_(writer) {}
|
| -
|
| -QuicSimpleClient::DummyPacketWriterFactory::~DummyPacketWriterFactory() {}
|
| -
|
| -QuicPacketWriter* QuicSimpleClient::DummyPacketWriterFactory::Create(
|
| - QuicConnection* /*connection*/) const {
|
| - return writer_;
|
| -}
|
| -
|
| QuicSimpleClient::QuicDataToResend::QuicDataToResend(HttpRequestInfo* headers,
|
| StringPiece body,
|
| bool fin)
|
| @@ -193,8 +170,8 @@ bool QuicSimpleClient::Connect() {
|
| }
|
| STLDeleteElements(&data_to_resend_on_connect_);
|
| }
|
| - if (session_.get() != nullptr &&
|
| - session_->error() != QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| + if (session() != nullptr &&
|
| + session()->error() != QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| // We've successfully created a session but we're not connected, and there
|
| // is no stateless reject to recover from. Give up trying.
|
| break;
|
| @@ -202,63 +179,44 @@ bool QuicSimpleClient::Connect() {
|
| }
|
| if (!connected() &&
|
| GetNumSentClientHellos() > QuicCryptoClientStream::kMaxClientHellos &&
|
| - session_ != nullptr &&
|
| - session_->error() == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| + session() != nullptr &&
|
| + session()->error() == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| // The overall connection failed due too many stateless rejects.
|
| - connection_error_ = QUIC_CRYPTO_TOO_MANY_REJECTS;
|
| + set_connection_error(QUIC_CRYPTO_TOO_MANY_REJECTS);
|
| }
|
| - return session_->connection()->connected();
|
| -}
|
| -
|
| -QuicClientSession* QuicSimpleClient::CreateQuicClientSession(
|
| - const QuicConfig& config,
|
| - QuicConnection* connection,
|
| - const QuicServerId& server_id,
|
| - QuicCryptoClientConfig* crypto_config) {
|
| - return new QuicClientSession(config, connection, server_id_, &crypto_config_);
|
| + return session()->connection()->connected();
|
| }
|
|
|
| void QuicSimpleClient::StartConnect() {
|
| DCHECK(initialized_);
|
| DCHECK(!connected());
|
|
|
| - writer_.reset(CreateQuicPacketWriter());
|
| + set_writer(CreateQuicPacketWriter());
|
|
|
| - DummyPacketWriterFactory factory(writer_.get());
|
| + DummyPacketWriterFactory factory(writer());
|
|
|
| - if (connected_or_attempting_connect_) {
|
| + if (connected_or_attempting_connect()) {
|
| // Before we destroy the last session and create a new one, gather its stats
|
| // and update the stats for the overall connection.
|
| - num_sent_client_hellos_ += session_->GetNumSentClientHellos();
|
| - if (session_->error() == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| + UpdateStats();
|
| + if (session()->error() == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| // If the last error was due to a stateless reject, queue up the data to
|
| // be resent on the next successful connection.
|
| // TODO(jokulik): I'm a little bit concerned about ordering here. Maybe
|
| // we should just maintain one queue?
|
| - ++num_stateless_rejects_received_;
|
| DCHECK(data_to_resend_on_connect_.empty());
|
| data_to_resend_on_connect_.swap(data_sent_before_handshake_);
|
| }
|
| }
|
|
|
| - session_.reset(CreateQuicClientSession(
|
| - config_,
|
| - new QuicConnection(GetNextConnectionId(), server_address_, helper_.get(),
|
| - factory,
|
| - /* owns_writer= */ false, Perspective::IS_CLIENT,
|
| - server_id_.is_https(), supported_versions_),
|
| - server_id_, &crypto_config_));
|
| - if (initial_max_packet_length_ != 0) {
|
| - session_->connection()->set_max_packet_length(initial_max_packet_length_);
|
| - }
|
| - session_->Initialize();
|
| - session_->CryptoConnect();
|
| - connected_or_attempting_connect_ = true;
|
| -}
|
| + CreateQuicClientSession(new QuicConnection(
|
| + GetNextConnectionId(), server_address_, helper_.get(), factory,
|
| + /* owns_writer= */ false, Perspective::IS_CLIENT, server_id().is_https(),
|
| + supported_versions()));
|
|
|
| -bool QuicSimpleClient::EncryptionBeingEstablished() {
|
| - return !session_->IsEncryptionEstablished() &&
|
| - session_->connection()->connected();
|
| + session()->Initialize();
|
| + session()->CryptoConnect();
|
| + set_connected_or_attempting_connect(true);
|
| }
|
|
|
| void QuicSimpleClient::Disconnect() {
|
| @@ -270,14 +228,14 @@ void QuicSimpleClient::Disconnect() {
|
| STLDeleteElements(&data_to_resend_on_connect_);
|
| STLDeleteElements(&data_sent_before_handshake_);
|
|
|
| - writer_.reset();
|
| + reset_writer();
|
| packet_reader_.reset();
|
|
|
| initialized_ = false;
|
| }
|
|
|
| void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers,
|
| - base::StringPiece body,
|
| + StringPiece body,
|
| bool fin) {
|
| QuicSpdyClientStream* stream = CreateReliableClientStream();
|
| if (stream == nullptr) {
|
| @@ -304,7 +262,7 @@ void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers,
|
| void QuicSimpleClient::MaybeAddQuicDataToResend(
|
| QuicDataToResend* data_to_resend) {
|
| DCHECK(FLAGS_enable_quic_stateless_reject_support);
|
| - if (session_->IsCryptoHandshakeConfirmed()) {
|
| + if (session()->IsCryptoHandshakeConfirmed()) {
|
| // The handshake is confirmed. No need to continue saving requests to
|
| // resend.
|
| STLDeleteElements(&data_sent_before_handshake_);
|
| @@ -319,7 +277,7 @@ void QuicSimpleClient::MaybeAddQuicDataToResend(
|
|
|
| void QuicSimpleClient::SendRequestAndWaitForResponse(
|
| const HttpRequestInfo& request,
|
| - base::StringPiece body,
|
| + StringPiece body,
|
| bool fin) {
|
| SendRequest(request, body, fin);
|
| while (WaitForEvents()) {}
|
| @@ -337,45 +295,21 @@ void QuicSimpleClient::SendRequestsAndWaitForResponse(
|
| while (WaitForEvents()) {}
|
| }
|
|
|
| -QuicSpdyClientStream* QuicSimpleClient::CreateReliableClientStream() {
|
| - if (!connected()) {
|
| - return nullptr;
|
| - }
|
| -
|
| - return session_->CreateOutgoingDynamicStream();
|
| -}
|
| -
|
| -void QuicSimpleClient::WaitForStreamToClose(QuicStreamId id) {
|
| - DCHECK(connected());
|
| -
|
| - while (connected() && !session_->IsClosedStream(id)) {
|
| - WaitForEvents();
|
| - }
|
| -}
|
| -
|
| -void QuicSimpleClient::WaitForCryptoHandshakeConfirmed() {
|
| - DCHECK(connected());
|
| -
|
| - while (connected() && !session_->IsCryptoHandshakeConfirmed()) {
|
| - WaitForEvents();
|
| - }
|
| -}
|
| -
|
| bool QuicSimpleClient::WaitForEvents() {
|
| DCHECK(connected());
|
|
|
| base::RunLoop().RunUntilIdle();
|
|
|
| - DCHECK(session_ != nullptr);
|
| + DCHECK(session() != nullptr);
|
| if (!connected() &&
|
| - session_->error() == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| + session()->error() == QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT) {
|
| DCHECK(FLAGS_enable_quic_stateless_reject_support);
|
| DVLOG(1) << "Detected stateless reject while waiting for events. "
|
| << "Attempting to reconnect.";
|
| Connect();
|
| }
|
|
|
| - return session_->num_active_requests() != 0;
|
| + return session()->num_active_requests() != 0;
|
| }
|
|
|
| bool QuicSimpleClient::MigrateSocket(const IPAddressNumber& new_host) {
|
| @@ -388,14 +322,12 @@ bool QuicSimpleClient::MigrateSocket(const IPAddressNumber& new_host) {
|
| return false;
|
| }
|
|
|
| - session_->connection()->SetSelfAddress(client_address_);
|
| + session()->connection()->SetSelfAddress(client_address_);
|
|
|
| QuicPacketWriter* writer = CreateQuicPacketWriter();
|
| DummyPacketWriterFactory factory(writer);
|
| - if (writer_.get() != writer) {
|
| - writer_.reset(writer);
|
| - }
|
| - session_->connection()->SetQuicPacketWriter(writer, false);
|
| + set_writer(writer);
|
| + session()->connection()->SetQuicPacketWriter(writer, false);
|
|
|
| return true;
|
| }
|
| @@ -421,15 +353,6 @@ void QuicSimpleClient::OnClose(QuicDataStream* stream) {
|
| }
|
| }
|
|
|
| -bool QuicSimpleClient::connected() const {
|
| - return session_.get() && session_->connection() &&
|
| - session_->connection()->connected();
|
| -}
|
| -
|
| -bool QuicSimpleClient::goaway_received() const {
|
| - return session_ != nullptr && session_->goaway_received();
|
| -}
|
| -
|
| size_t QuicSimpleClient::latest_response_code() const {
|
| LOG_IF(DFATAL, !store_response_) << "Response not stored!";
|
| return latest_response_code_;
|
| @@ -445,45 +368,6 @@ const string& QuicSimpleClient::latest_response_body() const {
|
| return latest_response_body_;
|
| }
|
|
|
| -int QuicSimpleClient::GetNumSentClientHellos() {
|
| - // If we are not actively attempting to connect, the session object
|
| - // corresponds to the previous connection and should not be used.
|
| - const int current_session_hellos = !connected_or_attempting_connect_
|
| - ? 0
|
| - : session_->GetNumSentClientHellos();
|
| - return num_sent_client_hellos_ + current_session_hellos;
|
| -}
|
| -
|
| -QuicErrorCode QuicSimpleClient::connection_error() const {
|
| - // Return the high-level error if there was one. Otherwise, return the
|
| - // connection error from the last session.
|
| - if (connection_error_ != QUIC_NO_ERROR) {
|
| - return connection_error_;
|
| - }
|
| - if (session_.get() == nullptr) {
|
| - return QUIC_NO_ERROR;
|
| - }
|
| - return session_->error();
|
| -}
|
| -
|
| -QuicConnectionId QuicSimpleClient::GetNextConnectionId() {
|
| - QuicConnectionId server_designated_id = GetNextServerDesignatedConnectionId();
|
| - return server_designated_id ? server_designated_id
|
| - : GenerateNewConnectionId();
|
| -}
|
| -
|
| -QuicConnectionId QuicSimpleClient::GetNextServerDesignatedConnectionId() {
|
| - QuicCryptoClientConfig::CachedState* cached =
|
| - crypto_config_.LookupOrCreate(server_id_);
|
| - // If the cached state indicates that we should use a server-designated
|
| - // connection ID, then return that connection ID.
|
| - CHECK(cached != nullptr) << "QuicClientCryptoConfig::LookupOrCreate returned "
|
| - << "unexpected nullptr.";
|
| - return cached->has_server_designated_connection_id()
|
| - ? cached->GetNextServerDesignatedConnectionId()
|
| - : 0;
|
| -}
|
| -
|
| QuicConnectionId QuicSimpleClient::GenerateNewConnectionId() {
|
| return helper_->GetRandomGenerator()->RandUint64();
|
| }
|
| @@ -505,8 +389,9 @@ void QuicSimpleClient::OnReadError(int result) {
|
| bool QuicSimpleClient::OnPacket(const QuicEncryptedPacket& packet,
|
| IPEndPoint local_address,
|
| IPEndPoint peer_address) {
|
| - session_->connection()->ProcessUdpPacket(local_address, peer_address, packet);
|
| - if (!session_->connection()->connected()) {
|
| + session()->connection()->ProcessUdpPacket(local_address, peer_address,
|
| + packet);
|
| + if (!session()->connection()->connected()) {
|
| return false;
|
| }
|
|
|
|
|