1 // Copyright (c) 2019 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 #ifndef QUICHE_QUIC_QBONE_QBONE_PACKET_EXCHANGER_H_ 6 #define QUICHE_QUIC_QBONE_QBONE_PACKET_EXCHANGER_H_ 7 8 #include "quiche/quic/core/quic_packets.h" 9 #include "quiche/quic/qbone/qbone_client_interface.h" 10 #include "quiche/quic/qbone/qbone_packet_writer.h" 11 12 namespace quic { 13 14 // Handles reading and writing on the local network and exchange packets between 15 // the local network with a QBONE connection. 16 class QbonePacketExchanger : public QbonePacketWriter { 17 public: 18 // The owner might want to receive notifications when read or write fails. 19 class Visitor { 20 public: ~Visitor()21 virtual ~Visitor() {} OnReadError(const std::string & error)22 virtual void OnReadError(const std::string& error) {} OnWriteError(const std::string & error)23 virtual void OnWriteError(const std::string& error) {} 24 }; 25 // Does not take ownership of visitor. QbonePacketExchanger(Visitor * visitor,size_t max_pending_packets)26 QbonePacketExchanger(Visitor* visitor, size_t max_pending_packets) 27 : visitor_(visitor), max_pending_packets_(max_pending_packets) {} 28 29 QbonePacketExchanger(const QbonePacketExchanger&) = delete; 30 QbonePacketExchanger& operator=(const QbonePacketExchanger&) = delete; 31 32 QbonePacketExchanger(QbonePacketExchanger&&) = delete; 33 QbonePacketExchanger& operator=(QbonePacketExchanger&&) = delete; 34 35 ~QbonePacketExchanger() = default; 36 37 // Returns true if there may be more packets to read. 38 // Implementations handles the actual raw read and delivers the packet to 39 // qbone_client. 40 bool ReadAndDeliverPacket(QboneClientInterface* qbone_client); 41 42 // From QbonePacketWriter. 43 // Writes a packet to the local network. If the write would be blocked, the 44 // packet will be queued if the queue is smaller than max_pending_packets_. 45 void WritePacketToNetwork(const char* packet, size_t size) override; 46 47 // The caller signifies that the local network is no longer blocked. 48 void SetWritable(); 49 50 private: 51 // The actual implementation that reads a packet from the local network. 52 // Returns the packet if one is successfully read. This might nullptr when a) 53 // there is no packet to read, b) the read failed. In the former case, blocked 54 // is set to true. error contains the error message. 55 virtual std::unique_ptr<QuicData> ReadPacket(bool* blocked, 56 std::string* error) = 0; 57 58 // The actual implementation that writes a packet to the local network. 59 // Returns true if the write succeeds. blocked will be set to true if the 60 // write failure is caused by the local network being blocked. error contains 61 // the error message. 62 virtual bool WritePacket(const char* packet, size_t size, bool* blocked, 63 std::string* error) = 0; 64 65 std::list<std::unique_ptr<QuicData>> packet_queue_; 66 67 Visitor* visitor_; 68 69 // The maximum number of packets that could be queued up when writing to local 70 // network is blocked. 71 size_t max_pending_packets_; 72 73 bool write_blocked_ = false; 74 }; 75 76 } // namespace quic 77 78 #endif // QUICHE_QUIC_QBONE_QBONE_PACKET_EXCHANGER_H_ 79