Delete unused downloads page asset.
[chromium-blink-merge.git] / net / quic / quic_packet_reader.cc
blobc58a51fa7f8e30853ab7fd667e8636575c275496
1 // Copyright (c) 2015 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_packet_reader.h"
7 #include "base/location.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/base/net_errors.h"
13 namespace net {
15 QuicPacketReader::QuicPacketReader(DatagramClientSocket* socket,
16 Visitor* visitor,
17 const BoundNetLog& net_log)
18 : socket_(socket),
19 visitor_(visitor),
20 read_pending_(false),
21 num_packets_read_(0),
22 read_buffer_(new IOBufferWithSize(static_cast<size_t>(kMaxPacketSize))),
23 net_log_(net_log),
24 weak_factory_(this) {
27 QuicPacketReader::~QuicPacketReader() {
30 void QuicPacketReader::StartReading() {
31 if (read_pending_)
32 return;
34 DCHECK(socket_);
35 read_pending_ = true;
36 int rv = socket_->Read(read_buffer_.get(), read_buffer_->size(),
37 base::Bind(&QuicPacketReader::OnReadComplete,
38 weak_factory_.GetWeakPtr()));
39 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.AsyncRead", rv == ERR_IO_PENDING);
40 if (rv == ERR_IO_PENDING) {
41 num_packets_read_ = 0;
42 return;
45 if (++num_packets_read_ > 32) {
46 num_packets_read_ = 0;
47 // Data was read, process it.
48 // Schedule the work through the message loop to 1) prevent infinite
49 // recursion and 2) avoid blocking the thread for too long.
50 base::ThreadTaskRunnerHandle::Get()->PostTask(
51 FROM_HERE, base::Bind(&QuicPacketReader::OnReadComplete,
52 weak_factory_.GetWeakPtr(), rv));
53 } else {
54 OnReadComplete(rv);
58 void QuicPacketReader::OnReadComplete(int result) {
59 read_pending_ = false;
60 if (result == 0)
61 result = ERR_CONNECTION_CLOSED;
63 if (result < 0) {
64 visitor_->OnReadError(result);
65 return;
68 QuicEncryptedPacket packet(read_buffer_->data(), result);
69 IPEndPoint local_address;
70 IPEndPoint peer_address;
71 socket_->GetLocalAddress(&local_address);
72 socket_->GetPeerAddress(&peer_address);
73 if (!visitor_->OnPacket(packet, local_address, peer_address))
74 return;
76 StartReading();
79 } // namespace net