QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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. |
| 4 | |
| 5 | // An implementation of the QuicClientBase::NetworkHelper |
| 6 | // that is based off the epoll server. |
| 7 | |
| 8 | #ifndef QUICHE_QUIC_TOOLS_QUIC_CLIENT_EPOLL_NETWORK_HELPER_H_ |
| 9 | #define QUICHE_QUIC_TOOLS_QUIC_CLIENT_EPOLL_NETWORK_HELPER_H_ |
| 10 | |
| 11 | #include <cstdint> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | |
| 15 | #include "base/macros.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/http/quic_client_push_promise_index.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_config.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_packet_reader.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/quic_process_packet_interface.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_containers.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_epoll.h" |
| 22 | #include "net/third_party/quiche/src/quic/tools/quic_client_base.h" |
| 23 | |
| 24 | namespace quic { |
| 25 | |
| 26 | namespace test { |
| 27 | class QuicClientPeer; |
| 28 | } // namespace test |
| 29 | |
| 30 | // An implementation of the QuicClientBase::NetworkHelper based off |
| 31 | // the epoll server. |
| 32 | class QuicClientEpollNetworkHelper : public QuicClientBase::NetworkHelper, |
| 33 | public QuicEpollCallbackInterface, |
| 34 | public ProcessPacketInterface { |
| 35 | public: |
| 36 | // Create a quic client, which will have events managed by an externally owned |
| 37 | // EpollServer. |
| 38 | QuicClientEpollNetworkHelper(QuicEpollServer* epoll_server, |
| 39 | QuicClientBase* client); |
| 40 | QuicClientEpollNetworkHelper(const QuicClientEpollNetworkHelper&) = delete; |
| 41 | QuicClientEpollNetworkHelper& operator=(const QuicClientEpollNetworkHelper&) = |
| 42 | delete; |
| 43 | |
| 44 | ~QuicClientEpollNetworkHelper() override; |
| 45 | |
| 46 | // Return a name describing the class for use in debug/error reporting. |
| 47 | QuicString Name() const override; |
| 48 | |
| 49 | // From EpollCallbackInterface |
| 50 | void OnRegistration(QuicEpollServer* eps, int fd, int event_mask) override; |
| 51 | void OnModification(int fd, int event_mask) override; |
| 52 | void OnEvent(int fd, QuicEpollEvent* event) override; |
| 53 | // |fd_| can be unregistered without the client being disconnected. This |
| 54 | // happens in b3m QuicProber where we unregister |fd_| to feed in events to |
| 55 | // the client from the SelectServer. |
| 56 | void OnUnregistration(int fd, bool replaced) override; |
| 57 | void OnShutdown(QuicEpollServer* eps, int fd) override; |
| 58 | |
| 59 | // From ProcessPacketInterface. This will be called for each received |
| 60 | // packet. |
| 61 | void ProcessPacket(const QuicSocketAddress& self_address, |
| 62 | const QuicSocketAddress& peer_address, |
| 63 | const QuicReceivedPacket& packet) override; |
| 64 | |
| 65 | // From NetworkHelper. |
| 66 | void RunEventLoop() override; |
| 67 | bool CreateUDPSocketAndBind(QuicSocketAddress server_address, |
| 68 | QuicIpAddress bind_to_address, |
| 69 | int bind_to_port) override; |
| 70 | void CleanUpAllUDPSockets() override; |
| 71 | QuicSocketAddress GetLatestClientAddress() const override; |
| 72 | QuicPacketWriter* CreateQuicPacketWriter() override; |
| 73 | |
| 74 | // Accessors provided for convenience, not part of any interface. |
| 75 | |
| 76 | QuicEpollServer* epoll_server() { return epoll_server_; } |
| 77 | |
| 78 | const QuicLinkedHashMap<int, QuicSocketAddress>& fd_address_map() const { |
| 79 | return fd_address_map_; |
| 80 | } |
| 81 | |
| 82 | // If the client has at least one UDP socket, return the latest created one. |
| 83 | // Otherwise, return -1. |
| 84 | int GetLatestFD() const; |
| 85 | |
| 86 | // Create socket for connection to |server_address| with default socket |
| 87 | // options. |
| 88 | // Return fd index. |
| 89 | virtual int CreateUDPSocket(QuicSocketAddress server_address, |
| 90 | bool* overflow_supported); |
| 91 | |
| 92 | QuicClientBase* client() { return client_; } |
| 93 | |
| 94 | void set_max_reads_per_epoll_loop(int num_reads) { |
| 95 | max_reads_per_epoll_loop_ = num_reads; |
| 96 | } |
| 97 | // If |fd| is an open UDP socket, unregister and close it. Otherwise, do |
| 98 | // nothing. |
| 99 | void CleanUpUDPSocket(int fd); |
| 100 | |
| 101 | private: |
| 102 | friend class test::QuicClientPeer; |
| 103 | |
| 104 | // Used for testing. |
| 105 | void SetClientPort(int port); |
| 106 | |
| 107 | // Actually clean up |fd|. |
| 108 | void CleanUpUDPSocketImpl(int fd); |
| 109 | |
| 110 | // Listens for events on the client socket. |
| 111 | QuicEpollServer* epoll_server_; |
| 112 | |
| 113 | // Map mapping created UDP sockets to their addresses. By using linked hash |
| 114 | // map, the order of socket creation can be recorded. |
| 115 | QuicLinkedHashMap<int, QuicSocketAddress> fd_address_map_; |
| 116 | |
| 117 | // If overflow_supported_ is true, this will be the number of packets dropped |
| 118 | // during the lifetime of the server. |
| 119 | QuicPacketCount packets_dropped_; |
| 120 | |
| 121 | // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped |
| 122 | // because the socket would otherwise overflow. |
| 123 | bool overflow_supported_; |
| 124 | |
| 125 | // Point to a QuicPacketReader object on the heap. The reader allocates more |
| 126 | // space than allowed on the stack. |
| 127 | std::unique_ptr<QuicPacketReader> packet_reader_; |
| 128 | |
| 129 | QuicClientBase* client_; |
| 130 | |
| 131 | int max_reads_per_epoll_loop_; |
| 132 | }; |
| 133 | |
| 134 | } // namespace quic |
| 135 | |
| 136 | #endif // QUICHE_QUIC_TOOLS_QUIC_CLIENT_EPOLL_NETWORK_HELPER_H_ |