| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "net/quic/quic_packet_reader.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "net/base/net_errors.h" |
| 9 |
| 10 namespace net { |
| 11 |
| 12 QuicPacketReader::QuicPacketReader(DatagramClientSocket* socket, |
| 13 Visitor* visitor, |
| 14 const BoundNetLog& net_log) |
| 15 : socket_(socket), |
| 16 visitor_(visitor), |
| 17 read_pending_(false), |
| 18 num_packets_read_(0), |
| 19 read_buffer_(new IOBufferWithSize(kMaxPacketSize)), |
| 20 net_log_(net_log), |
| 21 weak_factory_(this) { |
| 22 } |
| 23 |
| 24 QuicPacketReader::~QuicPacketReader() { |
| 25 } |
| 26 |
| 27 void QuicPacketReader::StartReading() { |
| 28 if (read_pending_) |
| 29 return; |
| 30 |
| 31 DCHECK(socket_); |
| 32 read_pending_ = true; |
| 33 int rv = socket_->Read(read_buffer_.get(), read_buffer_->size(), |
| 34 base::Bind(&QuicPacketReader::OnReadComplete, |
| 35 weak_factory_.GetWeakPtr())); |
| 36 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.AsyncRead", rv == ERR_IO_PENDING); |
| 37 if (rv == ERR_IO_PENDING) { |
| 38 num_packets_read_ = 0; |
| 39 return; |
| 40 } |
| 41 |
| 42 if (++num_packets_read_ > 32) { |
| 43 num_packets_read_ = 0; |
| 44 // Data was read, process it. |
| 45 // Schedule the work through the message loop to 1) prevent infinite |
| 46 // recursion and 2) avoid blocking the thread for too long. |
| 47 base::MessageLoop::current()->PostTask( |
| 48 FROM_HERE, base::Bind(&QuicPacketReader::OnReadComplete, |
| 49 weak_factory_.GetWeakPtr(), rv)); |
| 50 } else { |
| 51 OnReadComplete(rv); |
| 52 } |
| 53 } |
| 54 |
| 55 void QuicPacketReader::OnReadComplete(int result) { |
| 56 read_pending_ = false; |
| 57 if (result == 0) |
| 58 result = ERR_CONNECTION_CLOSED; |
| 59 |
| 60 if (result < 0) { |
| 61 visitor_->OnReadError(result); |
| 62 return; |
| 63 } |
| 64 |
| 65 QuicEncryptedPacket packet(read_buffer_->data(), result); |
| 66 IPEndPoint local_address; |
| 67 IPEndPoint peer_address; |
| 68 socket_->GetLocalAddress(&local_address); |
| 69 socket_->GetPeerAddress(&peer_address); |
| 70 if (!visitor_->OnPacket(packet, local_address, peer_address)) |
| 71 return; |
| 72 |
| 73 StartReading(); |
| 74 } |
| 75 |
| 76 } // namespace net |
| OLD | NEW |