• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 
6 #ifndef NET_QUIC_QUIC_CHROMIUM_PACKET_READER_H_
7 #define NET_QUIC_QUIC_CHROMIUM_PACKET_READER_H_
8 
9 #include "base/memory/raw_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_export.h"
13 #include "net/log/net_log_with_source.h"
14 #include "net/socket/datagram_client_socket.h"
15 #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
16 #include "net/third_party/quiche/src/quiche/quic/core/quic_time.h"
17 
18 namespace quic {
19 class QuicClock;
20 }  // namespace quic
21 namespace net {
22 
23 // If more than this many packets have been read or more than that many
24 // milliseconds have passed, QuicChromiumPacketReader::StartReading() yields by
25 // doing a QuicChromiumPacketReader::PostTask().
26 const int kQuicYieldAfterPacketsRead = 32;
27 const int kQuicYieldAfterDurationMilliseconds = 2;
28 
29 class NET_EXPORT_PRIVATE QuicChromiumPacketReader {
30  public:
31   class NET_EXPORT_PRIVATE Visitor {
32    public:
33     virtual ~Visitor() = default;
34     // Called when the read operation failed. The visitor returns
35     // whether the reader should keep reading.
36     virtual bool OnReadError(int result,
37                              const DatagramClientSocket* socket) = 0;
38     virtual bool OnPacket(const quic::QuicReceivedPacket& packet,
39                           const quic::QuicSocketAddress& local_address,
40                           const quic::QuicSocketAddress& peer_address) = 0;
41   };
42 
43   // If |report_ecn| is true, then the reader will call GetLastTos() on the
44   // socket after each read and report the ECN codepoint in the
45   // QuicReceivedPacket.
46   // TODO(crbug.com/332924003): When the relevant config flags are deprecated,
47   // this argument can be removed.
48   QuicChromiumPacketReader(std::unique_ptr<DatagramClientSocket> socket,
49                            const quic::QuicClock* clock,
50                            Visitor* visitor,
51                            int yield_after_packets,
52                            quic::QuicTime::Delta yield_after_duration,
53                            bool report_ecn,
54                            const NetLogWithSource& net_log);
55 
56   QuicChromiumPacketReader(const QuicChromiumPacketReader&) = delete;
57   QuicChromiumPacketReader& operator=(const QuicChromiumPacketReader&) = delete;
58 
59   virtual ~QuicChromiumPacketReader();
60 
61   // Causes the QuicConnectionHelper to start reading from the socket
62   // and passing the data along to the quic::QuicConnection.
63   void StartReading();
64 
socket()65   DatagramClientSocket* socket() { return socket_.get(); }
66 
67   void CloseSocket();
68 
69  private:
70   // A completion callback invoked when a read completes.
71   void OnReadComplete(int result);
72   // Return true if reading should continue.
73   bool ProcessReadResult(int result);
74 
75   std::unique_ptr<DatagramClientSocket> socket_;
76 
77   raw_ptr<Visitor> visitor_;
78   bool read_pending_ = false;
79   int num_packets_read_ = 0;
80   raw_ptr<const quic::QuicClock> clock_;  // Not owned.
81   int yield_after_packets_;
82   quic::QuicTime::Delta yield_after_duration_;
83   quic::QuicTime yield_after_;
84   scoped_refptr<IOBufferWithSize> read_buffer_;
85   NetLogWithSource net_log_;
86   // Stores whether receiving ECN is in the feature list to avoid accessing
87   // the feature list for every packet.
88   bool report_ecn_;
89 
90   base::WeakPtrFactory<QuicChromiumPacketReader> weak_factory_{this};
91 };
92 
93 }  // namespace net
94 
95 #endif  // NET_QUIC_QUIC_CHROMIUM_PACKET_READER_H_
96