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"
15 QuicPacketReader::QuicPacketReader(DatagramClientSocket
* socket
,
17 const BoundNetLog
& net_log
)
22 read_buffer_(new IOBufferWithSize(static_cast<size_t>(kMaxPacketSize
))),
27 QuicPacketReader::~QuicPacketReader() {
30 void QuicPacketReader::StartReading() {
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;
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
));
58 void QuicPacketReader::OnReadComplete(int result
) {
59 read_pending_
= false;
61 result
= ERR_CONNECTION_CLOSED
;
64 visitor_
->OnReadError(result
);
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
))