[email protected] | 9db44391 | 2013-02-25 05:27:03 | [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 | // Responsible for generating packets on behalf of a QuicConnection. |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 6 | // Packets are serialized just-in-time. Control frames are queued. |
| 7 | // Ack and Feedback frames will be requested from the Connection |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 8 | // just-in-time. When a packet needs to be sent, the Generator |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 9 | // will serialize a packet and pass it to QuicConnection::SendOrQueuePacket() |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 10 | // |
| 11 | // The Generator's mode of operation is controlled by two conditions: |
| 12 | // |
| 13 | // 1) Is the Delegate writable? |
| 14 | // |
| 15 | // If the Delegate is not writable, then no operations will cause |
| 16 | // a packet to be serialized. In particular: |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 17 | // * SetShouldSendAck will simply record that an ack is to be sent. |
[email protected] | 4d1789c3 | 2013-09-18 23:54:36 | [diff] [blame] | 18 | // * AddControlFrame will enqueue the control frame. |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 19 | // * ConsumeData will do nothing. |
| 20 | // |
| 21 | // If the Delegate is writable, then the behavior depends on the second |
| 22 | // condition: |
| 23 | // |
| 24 | // 2) Is the Generator in batch mode? |
| 25 | // |
| 26 | // If the Generator is NOT in batch mode, then each call to a write |
| 27 | // operation will serialize one or more packets. The contents will |
| 28 | // include any previous queued frames. If an ack should be sent |
| 29 | // but has not been sent, then the Delegate will be asked to create |
| 30 | // an Ack frame which will then be included in the packet. When |
| 31 | // the write call completes, the current packet will be serialized |
| 32 | // and sent to the Delegate, even if it is not full. |
| 33 | // |
| 34 | // If the Generator is in batch mode, then each write operation will |
| 35 | // add data to the "current" packet. When the current packet becomes |
| 36 | // full, it will be serialized and sent to the packet. When batch |
| 37 | // mode is ended via |FinishBatchOperations|, the current packet |
| 38 | // will be serialzied, even if it is not full. |
| 39 | // |
| 40 | // FEC behavior also depends on batch mode. In batch mode, FEC packets |
| 41 | // will be sent after |max_packets_per_group| have been sent, as well |
| 42 | // as after batch operations are complete. When not in batch mode, |
| 43 | // an FEC packet will be sent after each write call completes. |
| 44 | // |
| 45 | // TODO(rch): This behavior should probably be tuned. When not in batch |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 46 | // mode, we should probably set a timer so that several independent |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 47 | // operations can be grouped into the same FEC group. |
| 48 | // |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 49 | // When an FEC packet is generated, it will be send to the Delegate, |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 50 | // even if the Delegate has become unwritable after handling the |
| 51 | // data packet immediately proceeding the FEC packet. |
| 52 | |
| 53 | #ifndef NET_QUIC_QUIC_PACKET_GENERATOR_H_ |
| 54 | #define NET_QUIC_QUIC_PACKET_GENERATOR_H_ |
| 55 | |
| 56 | #include "net/quic/quic_packet_creator.h" |
| 57 | |
| 58 | namespace net { |
| 59 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 60 | class QuicAckNotifier; |
| 61 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 62 | class NET_EXPORT_PRIVATE QuicPacketGenerator { |
| 63 | public: |
| 64 | class NET_EXPORT_PRIVATE DelegateInterface { |
| 65 | public: |
| 66 | virtual ~DelegateInterface() {} |
[email protected] | c67a82cb | 2013-09-24 02:53:21 | [diff] [blame] | 67 | virtual bool CanWrite(TransmissionType transmission_type, |
[email protected] | 575cce6 | 2013-08-03 02:06:43 | [diff] [blame] | 68 | HasRetransmittableData retransmittable, |
| 69 | IsHandshake handshake) = 0; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 70 | virtual QuicAckFrame* CreateAckFrame() = 0; |
| 71 | virtual QuicCongestionFeedbackFrame* CreateFeedbackFrame() = 0; |
| 72 | // Takes ownership of |packet.packet| and |packet.retransmittable_frames|. |
| 73 | virtual bool OnSerializedPacket(const SerializedPacket& packet) = 0; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 74 | virtual void CloseConnection(QuicErrorCode error, bool from_peer) = 0; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 75 | }; |
| 76 | |
[email protected] | e1c2acb | 2013-06-22 22:34:13 | [diff] [blame] | 77 | // Interface which gets callbacks from the QuicPacketGenerator at interesting |
| 78 | // points. Implementations must not mutate the state of the generator |
| 79 | // as a result of these callbacks. |
| 80 | class NET_EXPORT_PRIVATE DebugDelegateInterface { |
| 81 | public: |
| 82 | virtual ~DebugDelegateInterface() {} |
| 83 | |
| 84 | // Called when a frame has been added to the current packet. |
| 85 | virtual void OnFrameAddedToPacket(const QuicFrame& frame) = 0; |
| 86 | }; |
| 87 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 88 | QuicPacketGenerator(DelegateInterface* delegate, |
[email protected] | e1c2acb | 2013-06-22 22:34:13 | [diff] [blame] | 89 | DebugDelegateInterface* debug_delegate, |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 90 | QuicPacketCreator* creator); |
| 91 | |
| 92 | virtual ~QuicPacketGenerator(); |
| 93 | |
[email protected] | 4d1789c3 | 2013-09-18 23:54:36 | [diff] [blame] | 94 | // Indicates that an ACK frame should be sent. If |also_send_feedback| is |
| 95 | // true, then it also indicates a CONGESTION_FEEDBACK frame should be sent. |
| 96 | // The contents of the frame(s) will be generated via a call to the delegates |
| 97 | // CreateAckFrame() and CreateFeedbackFrame() when the packet is serialized. |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 98 | void SetShouldSendAck(bool also_send_feedback); |
| 99 | void AddControlFrame(const QuicFrame& frame); |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 100 | |
[email protected] | 4d1789c3 | 2013-09-18 23:54:36 | [diff] [blame] | 101 | // Given some data, may consume part or all of it and pass it to the |
| 102 | // packet creator to be serialized into packets. If not in batch |
| 103 | // mode, these packets will also be sent during this call. Also |
| 104 | // attaches a QuicAckNotifier to any created stream frames, which |
| 105 | // will be called once the frame is ACKed by the peer. The |
| 106 | // QuicAckNotifier is owned by the QuicConnection. |notifier| may |
| 107 | // be NULL. |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 108 | QuicConsumedData ConsumeData(QuicStreamId id, |
| 109 | base::StringPiece data, |
| 110 | QuicStreamOffset offset, |
| 111 | bool fin, |
| 112 | QuicAckNotifier* notifier); |
| 113 | |
[email protected] | e4696aa | 2013-08-20 16:03:55 | [diff] [blame] | 114 | // Indicates whether batch mode is currently enabled. |
| 115 | bool InBatchMode(); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 116 | // Disables flushing. |
| 117 | void StartBatchOperations(); |
| 118 | // Enables flushing and flushes queued data. |
| 119 | void FinishBatchOperations(); |
| 120 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 121 | bool HasQueuedFrames() const; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 122 | |
[email protected] | e1c2acb | 2013-06-22 22:34:13 | [diff] [blame] | 123 | void set_debug_delegate(DebugDelegateInterface* debug_delegate) { |
| 124 | debug_delegate_ = debug_delegate; |
| 125 | } |
| 126 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 127 | private: |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 128 | void SendQueuedFrames(); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 129 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 130 | // Test to see if we have pending ack, feedback, or control frames. |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 131 | bool HasPendingFrames() const; |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 132 | // Test to see if the addition of a pending frame (which might be |
| 133 | // retransmittable) would still allow the resulting packet to be sent now. |
| 134 | bool CanSendWithNextPendingFrameAddition() const; |
| 135 | // Add exactly one pending frame, preferring ack over feedback over control |
| 136 | // frames. |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 137 | bool AddNextPendingFrame(); |
[email protected] | e1c2acb | 2013-06-22 22:34:13 | [diff] [blame] | 138 | |
| 139 | bool AddFrame(const QuicFrame& frame); |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 140 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 141 | void SerializeAndSendPacket(); |
| 142 | |
| 143 | DelegateInterface* delegate_; |
[email protected] | e1c2acb | 2013-06-22 22:34:13 | [diff] [blame] | 144 | DebugDelegateInterface* debug_delegate_; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 145 | |
| 146 | QuicPacketCreator* packet_creator_; |
| 147 | QuicFrames queued_control_frames_; |
[email protected] | e4696aa | 2013-08-20 16:03:55 | [diff] [blame] | 148 | |
| 149 | // True if batch mode is currently enabled. |
| 150 | bool batch_mode_; |
| 151 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 152 | // Flags to indicate the need for just-in-time construction of a frame. |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 153 | bool should_send_ack_; |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 154 | bool should_send_feedback_; |
| 155 | // If we put a non-retransmittable frame (namley ack or feedback frame) in |
| 156 | // this packet, then we have to hold a reference to it until we flush (and |
| 157 | // serialize it). Retransmittable frames are referenced elsewhere so that they |
| 158 | // can later be (optionally) retransmitted. |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 159 | scoped_ptr<QuicAckFrame> pending_ack_frame_; |
| 160 | scoped_ptr<QuicCongestionFeedbackFrame> pending_feedback_frame_; |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 161 | |
| 162 | DISALLOW_COPY_AND_ASSIGN(QuicPacketGenerator); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // namespace net |
| 166 | |
| 167 | #endif // NET_QUIC_QUIC_PACKET_GENERATOR_H_ |