• 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   QuicChromiumPacketReader(DatagramClientSocket* socket,
44                            const quic::QuicClock* clock,
45                            Visitor* visitor,
46                            int yield_after_packets,
47                            quic::QuicTime::Delta yield_after_duration,
48                            const NetLogWithSource& net_log);
49 
50   QuicChromiumPacketReader(const QuicChromiumPacketReader&) = delete;
51   QuicChromiumPacketReader& operator=(const QuicChromiumPacketReader&) = delete;
52 
53   virtual ~QuicChromiumPacketReader();
54 
55   // Causes the QuicConnectionHelper to start reading from the socket
56   // and passing the data along to the quic::QuicConnection.
57   void StartReading();
58 
59  private:
60   // A completion callback invoked when a read completes.
61   void OnReadComplete(int result);
62   // Return true if reading should continue.
63   bool ProcessReadResult(int result);
64 
65   raw_ptr<DatagramClientSocket, DanglingUntriaged> socket_;
66 
67   raw_ptr<Visitor> visitor_;
68   bool read_pending_ = false;
69   int num_packets_read_ = 0;
70   raw_ptr<const quic::QuicClock> clock_;  // Not owned.
71   int yield_after_packets_;
72   quic::QuicTime::Delta yield_after_duration_;
73   quic::QuicTime yield_after_;
74   scoped_refptr<IOBufferWithSize> read_buffer_;
75   NetLogWithSource net_log_;
76 
77   base::WeakPtrFactory<QuicChromiumPacketReader> weak_factory_{this};
78 };
79 
80 }  // namespace net
81 
82 #endif  // NET_QUIC_QUIC_CHROMIUM_PACKET_READER_H_
83