diff options
author | rch <rch@chromium.org> | 2015-03-19 22:51:00 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-20 05:51:48 +0000 |
commit | 31ca50f123ae4580d4d681856aec2379aaa39896 (patch) | |
tree | 7fff52160befa0aec779d57c93eeb4c865043810 /net/tools | |
parent | 64461c4d9dab2a7f33b2611447d259a94976aca5 (diff) | |
download | chromium_src-31ca50f123ae4580d4d681856aec2379aaa39896.zip chromium_src-31ca50f123ae4580d4d681856aec2379aaa39896.tar.gz chromium_src-31ca50f123ae4580d4d681856aec2379aaa39896.tar.bz2 |
Factor out the QUIC socket reading code into a stand alone QuicPacketReader
and share it with both the QuicClientSession and QuicSimpleClient.
BUG=
Review URL: https://codereview.chromium.org/1009803003
Cr-Commit-Position: refs/heads/master@{#321522}
Diffstat (limited to 'net/tools')
-rw-r--r-- | net/tools/quic/quic_simple_client.cc | 76 | ||||
-rw-r--r-- | net/tools/quic/quic_simple_client.h | 26 |
2 files changed, 22 insertions, 80 deletions
diff --git a/net/tools/quic/quic_simple_client.cc b/net/tools/quic/quic_simple_client.cc index 74fa541..f1bfda1 100644 --- a/net/tools/quic/quic_simple_client.cc +++ b/net/tools/quic/quic_simple_client.cc @@ -21,13 +21,6 @@ using std::vector; namespace net { namespace tools { -namespace { - -// Allocate some extra space so we can send an error if the server goes over -// the limit. -const int kReadBufferSize = 2 * kMaxPacketSize; - -} // namespace QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, const QuicServerId& server_id, @@ -38,9 +31,6 @@ QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, helper_(CreateQuicConnectionHelper()), initialized_(false), supported_versions_(supported_versions), - read_pending_(false), - synchronous_read_count_(0), - read_buffer_(new IOBufferWithSize(kReadBufferSize)), weak_factory_(this) { } @@ -55,9 +45,6 @@ QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, helper_(CreateQuicConnectionHelper()), initialized_(false), supported_versions_(supported_versions), - read_pending_(false), - synchronous_read_count_(0), - read_buffer_(new IOBufferWithSize(kReadBufferSize)), weak_factory_(this) { } @@ -135,8 +122,8 @@ bool QuicSimpleClient::CreateUDPSocket() { } socket_.swap(socket); - - read_pending_ = false; + packet_reader_.reset(new QuicPacketReader(socket_.get(), this, + BoundNetLog())); if (socket != nullptr) { socket->Close(); @@ -147,7 +134,7 @@ bool QuicSimpleClient::CreateUDPSocket() { bool QuicSimpleClient::Connect() { StartConnect(); - StartReading(); + packet_reader_->StartReading(); while (EncryptionBeingEstablished()) { WaitForEvents(); } @@ -185,8 +172,7 @@ void QuicSimpleClient::Disconnect() { } writer_.reset(); - - read_pending_ = false; + packet_reader_.reset(); initialized_ = false; } @@ -309,58 +295,20 @@ QuicPacketWriter* QuicSimpleClient::CreateQuicPacketWriter() { return new QuicDefaultPacketWriter(socket_.get()); } -void QuicSimpleClient::StartReading() { - if (read_pending_) { - return; - } - read_pending_ = true; - - int result = socket_->Read( - read_buffer_.get(), - read_buffer_->size(), - base::Bind(&QuicSimpleClient::OnReadComplete, - weak_factory_.GetWeakPtr())); - - if (result == ERR_IO_PENDING) { - synchronous_read_count_ = 0; - return; - } - - if (++synchronous_read_count_ > 32) { - synchronous_read_count_ = 0; - // Schedule the processing through the message loop to 1) prevent infinite - // recursion and 2) avoid blocking the thread for too long. - base::MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(&QuicSimpleClient::OnReadComplete, - weak_factory_.GetWeakPtr(), result)); - } else { - OnReadComplete(result); - } +void QuicSimpleClient::OnReadError(int result) { + LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToString(result); + Disconnect(); } -void QuicSimpleClient::OnReadComplete(int result) { - read_pending_ = false; - if (result == 0) - result = ERR_CONNECTION_CLOSED; - - if (result < 0) { - LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToString(result); - Disconnect(); - return; - } - - QuicEncryptedPacket packet(read_buffer_->data(), result); - IPEndPoint local_address; - IPEndPoint peer_address; - socket_->GetLocalAddress(&local_address); - socket_->GetPeerAddress(&peer_address); +bool QuicSimpleClient::OnPacket(const QuicEncryptedPacket& packet, + IPEndPoint local_address, + IPEndPoint peer_address) { session_->connection()->ProcessUdpPacket(local_address, peer_address, packet); if (!session_->connection()->connected()) { - return; + return false; } - StartReading(); + return true; } } // namespace tools diff --git a/net/tools/quic/quic_simple_client.h b/net/tools/quic/quic_simple_client.h index 0bab4e4..b44e693 100644 --- a/net/tools/quic/quic_simple_client.h +++ b/net/tools/quic/quic_simple_client.h @@ -22,6 +22,7 @@ #include "net/quic/quic_config.h" #include "net/quic/quic_framer.h" #include "net/quic/quic_packet_creator.h" +#include "net/quic/quic_packet_reader.h" #include "net/tools/quic/quic_simple_client_session.h" #include "net/tools/quic/quic_simple_client_stream.h" @@ -39,7 +40,8 @@ namespace test { class QuicClientPeer; } // namespace test -class QuicSimpleClient : public QuicDataStream::Visitor { +class QuicSimpleClient : public QuicDataStream::Visitor, + public QuicPacketReader::Visitor { public: class ResponseListener { public: @@ -113,12 +115,11 @@ class QuicSimpleClient : public QuicDataStream::Visitor { // Returns true if there are any outstanding requests. bool WaitForEvents(); - // Start the read loop on the socket. - void StartReading(); - - // Called on reads that complete asynchronously. Dispatches the packet and - // calls StartReading() again. - void OnReadComplete(int result); + // QuicPacketReader::Visitor + void OnReadError(int result) override; + bool OnPacket(const QuicEncryptedPacket& packet, + IPEndPoint local_address, + IPEndPoint peer_address) override; // QuicDataStream::Visitor void OnClose(QuicDataStream* stream) override; @@ -278,18 +279,11 @@ class QuicSimpleClient : public QuicDataStream::Visitor { // Body of most recent response. std::string latest_response_body_; - bool read_pending_; - - // The number of iterations of the read loop that have completed synchronously - // and without posting a new task to the message loop. - int synchronous_read_count_; - - // The target buffer of the current read. - scoped_refptr<IOBufferWithSize> read_buffer_; - // The log used for the sockets. NetLog net_log_; + scoped_ptr<QuicPacketReader> packet_reader_; + base::WeakPtrFactory<QuicSimpleClient> weak_factory_; DISALLOW_COPY_AND_ASSIGN(QuicSimpleClient); |