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/metrics/histogram.h"
8 #include "net/base/net_errors.h"
12 QuicPacketReader::QuicPacketReader(DatagramClientSocket
* socket
,
14 const BoundNetLog
& net_log
)
19 read_buffer_(new IOBufferWithSize(kMaxPacketSize
)),
24 QuicPacketReader::~QuicPacketReader() {
27 void QuicPacketReader::StartReading() {
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;
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
));
55 void QuicPacketReader::OnReadComplete(int result
) {
56 read_pending_
= false;
58 result
= ERR_CONNECTION_CLOSED
;
61 visitor_
->OnReadError(result
);
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
))