blob: 8620a492fdbefb02cf294abf481d3c73ca94a0c3 [file] [log] [blame]
wubf975eac2019-08-19 19:41:01 -07001// 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 "net/third_party/quiche/src/quic/core/quic_packets.h"
9#include "net/third_party/quiche/src/quic/qbone/qbone_client_interface.h"
10#include "net/third_party/quiche/src/quic/qbone/qbone_packet_writer.h"
11
12namespace quic {
13
14// Handles reading and writing on the local network and exchange packets between
15// the local network with a Qbone connection.
16class QbonePacketExchanger : public QbonePacketWriter {
17 public:
18 // The owner might want to receive notifications when read or write fails.
19 class Visitor {
20 public:
21 virtual ~Visitor() {}
22 virtual void OnReadError(const string& error) {}
23 virtual void OnWriteError(const string& error) {}
24 };
25 // Does not take ownership of visitor.
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 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,
63 size_t size,
64 bool* blocked,
65 string* error) = 0;
66
67 std::list<std::unique_ptr<QuicData>> packet_queue_;
68
69 Visitor* visitor_;
70
71 // The maximum number of packets that could be queued up when writing to local
72 // network is blocked.
73 size_t max_pending_packets_;
74
75 bool write_blocked_ = false;
76};
77
78} // namespace quic
79
80#endif // QUICHE_QUIC_QBONE_QBONE_PACKET_EXCHANGER_H_