QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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_QUARTC_QUARTC_PACKET_WRITER_H_ |
| 6 | #define QUICHE_QUIC_QUARTC_QUARTC_PACKET_WRITER_H_ |
| 7 | |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 9 | #include "net/third_party/quiche/src/quic/core/quic_packet_writer.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | // Send and receive packets, like a virtual UDP socket. For example, this |
| 15 | // could be implemented by WebRTC's IceTransport. |
| 16 | class QuartcPacketTransport { |
| 17 | public: |
| 18 | // Additional metadata provided for each packet written. |
| 19 | struct PacketInfo { |
| 20 | QuicPacketNumber packet_number; |
| 21 | }; |
| 22 | |
| 23 | // Delegate for packet transport callbacks. Note that the delegate is not |
| 24 | // thread-safe. Packet transport implementations must ensure that callbacks |
| 25 | // are synchronized with all other work done by QUIC. |
| 26 | class Delegate { |
| 27 | public: |
| 28 | virtual ~Delegate() = default; |
| 29 | |
| 30 | // Called whenever the transport can write. |
| 31 | virtual void OnTransportCanWrite() = 0; |
| 32 | |
| 33 | // Called when the transport receives a packet. |
| 34 | virtual void OnTransportReceived(const char* data, size_t data_len) = 0; |
| 35 | }; |
| 36 | |
| 37 | virtual ~QuartcPacketTransport() {} |
| 38 | |
| 39 | // Called by the QuartcPacketWriter when writing packets to the network. |
| 40 | // Return the number of written bytes. Return 0 if the write is blocked. |
| 41 | virtual int Write(const char* buffer, |
| 42 | size_t buf_len, |
| 43 | const PacketInfo& info) = 0; |
| 44 | |
| 45 | // Sets the delegate which must be called when the transport can write or |
| 46 | // a packet is received. QUIC sets |delegate| to a nonnull pointer when it |
| 47 | // is ready to process incoming packets and sets |delegate| to nullptr before |
| 48 | // QUIC is deleted. Implementations may assume |delegate| remains valid until |
| 49 | // it is set to nullptr. |
| 50 | virtual void SetDelegate(Delegate* delegate) = 0; |
| 51 | }; |
| 52 | |
| 53 | struct QuartcPerPacketOptions : public PerPacketOptions { |
| 54 | std::unique_ptr<PerPacketOptions> Clone() const override; |
| 55 | |
| 56 | // The connection which is sending this packet. |
| 57 | QuicConnection* connection = nullptr; |
| 58 | }; |
| 59 | |
| 60 | // Implements a QuicPacketWriter using a QuartcPacketTransport, which allows a |
| 61 | // QuicConnection to use (for example), a WebRTC IceTransport. |
| 62 | class QuartcPacketWriter : public QuicPacketWriter { |
| 63 | public: |
| 64 | QuartcPacketWriter(QuartcPacketTransport* packet_transport, |
| 65 | QuicByteCount max_packet_size); |
| 66 | ~QuartcPacketWriter() override {} |
| 67 | |
| 68 | // The QuicConnection calls WritePacket and the QuicPacketWriter writes them |
| 69 | // to the QuartcSession::PacketTransport. |
| 70 | WriteResult WritePacket(const char* buffer, |
| 71 | size_t buf_len, |
| 72 | const QuicIpAddress& self_address, |
| 73 | const QuicSocketAddress& peer_address, |
| 74 | PerPacketOptions* options) override; |
| 75 | |
| 76 | // Whether the underneath |transport_| is blocked. If this returns true, |
| 77 | // outgoing QUIC packets are queued by QuicConnection until SetWritable() is |
| 78 | // called. |
| 79 | bool IsWriteBlocked() const override; |
| 80 | |
| 81 | // Maximum size of the QUIC packet which can be written. Users such as WebRTC |
| 82 | // can set the value through the QuartcFactoryConfig without updating the QUIC |
| 83 | // code. |
| 84 | QuicByteCount GetMaxPacketSize( |
| 85 | const QuicSocketAddress& peer_address) const override; |
| 86 | |
| 87 | // Sets the packet writer to a writable (non-blocked) state. |
| 88 | void SetWritable() override; |
| 89 | |
| 90 | bool SupportsReleaseTime() const override; |
| 91 | |
| 92 | bool IsBatchMode() const override; |
| 93 | |
| 94 | char* GetNextWriteLocation(const QuicIpAddress& self_address, |
| 95 | const QuicSocketAddress& peer_address) override; |
| 96 | |
| 97 | WriteResult Flush() override; |
| 98 | |
| 99 | void SetPacketTransportDelegate(QuartcPacketTransport::Delegate* delegate); |
| 100 | |
| 101 | private: |
| 102 | // QuartcPacketWriter will not own the transport. |
| 103 | QuartcPacketTransport* packet_transport_; |
| 104 | // The maximum size of the packet can be written by this writer. |
| 105 | QuicByteCount max_packet_size_; |
| 106 | |
| 107 | // Whether packets can be written. |
| 108 | bool writable_ = false; |
| 109 | }; |
| 110 | |
| 111 | } // namespace quic |
| 112 | |
| 113 | #endif // QUICHE_QUIC_QUARTC_QUARTC_PACKET_WRITER_H_ |