xref: /aosp_15_r20/external/cronet/net/tools/quic/quic_client_message_loop_network_helper.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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 // A toy client, which connects to a specified port and sends QUIC
6 // request to that endpoint.
7 
8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_MESSAGE_LOOP_NETWORK_HELPER_H_
9 #define NET_TOOLS_QUIC_QUIC_CLIENT_MESSAGE_LOOP_NETWORK_HELPER_H_
10 
11 #include <stddef.h>
12 
13 #include <memory>
14 
15 #include "base/command_line.h"
16 #include "net/base/ip_address.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/quic/platform/impl/quic_chromium_clock.h"
20 #include "net/quic/quic_chromium_packet_reader.h"
21 #include "net/third_party/quiche/src/quiche/quic/core/http/quic_spdy_stream.h"
22 #include "net/third_party/quiche/src/quiche/quic/core/quic_config.h"
23 #include "net/third_party/quiche/src/quiche/quic/tools/quic_spdy_client_base.h"
24 
25 namespace net {
26 
27 class UDPClientSocket;
28 
29 // An implementation of the quic::QuicClientBase::NetworkHelper based off
30 // the chromium epoll server.
31 class QuicClientMessageLooplNetworkHelper
32     : public quic::QuicClientBase::NetworkHelper,
33       public QuicChromiumPacketReader::Visitor {
34  public:
35   // Create a quic client, which will have events managed by an externally owned
36   // EpollServer.
37   QuicClientMessageLooplNetworkHelper(quic::QuicChromiumClock* clock,
38                                       quic::QuicClientBase* client);
39 
40   QuicClientMessageLooplNetworkHelper(
41       const QuicClientMessageLooplNetworkHelper&) = delete;
42   QuicClientMessageLooplNetworkHelper& operator=(
43       const QuicClientMessageLooplNetworkHelper&) = delete;
44 
45   ~QuicClientMessageLooplNetworkHelper() override;
46 
47   // QuicChromiumPacketReader::Visitor
48   bool OnReadError(int result, const DatagramClientSocket* socket) override;
49   bool OnPacket(const quic::QuicReceivedPacket& packet,
50                 const quic::QuicSocketAddress& local_address,
51                 const quic::QuicSocketAddress& peer_address) override;
52 
53   // From NetworkHelper.
54   void RunEventLoop() override;
55   bool CreateUDPSocketAndBind(quic::QuicSocketAddress server_address,
56                               quic::QuicIpAddress bind_to_address,
57                               int bind_to_port) override;
58   void CleanUpAllUDPSockets() override;
59   quic::QuicSocketAddress GetLatestClientAddress() const override;
60   quic::QuicPacketWriter* CreateQuicPacketWriter() override;
61 
62  private:
63   void StartPacketReaderIfNotStarted();
64 
65   // Address of the client if the client is connected to the server.
66   quic::QuicSocketAddress client_address_;
67 
68   // UDP socket connected to the server.
69   std::unique_ptr<UDPClientSocket> socket_;
70 
71   std::unique_ptr<QuicChromiumPacketReader> packet_reader_;
72 
73   bool packet_reader_started_ = false;
74 
75   quic::QuicChromiumClock* clock_;
76   quic::QuicClientBase* client_;
77 };
78 
79 }  // namespace net
80 
81 #endif  // NET_TOOLS_QUIC_QUIC_CLIENT_MESSAGE_LOOP_NETWORK_HELPER_H_
82