Home | History | Annotate | Download | only in quic
      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.
      6 // Packets are serialized just-in-time.  Control frames are queued.
      7 // Ack and Feedback frames will be requested from the Connection
      8 // just-in-time.  When a packet needs to be sent, the Generator
      9 // will serialize a packet and pass it to QuicConnection::SendOrQueuePacket()
     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:
     17 // * SetShouldSendAck will simply record that an ack is to be sent.
     18 // * AddControlFram will enqueue the control frame.
     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
     46 // mode, we should probably set a timer so that several independent
     47 // operations can be grouped into the same FEC group.
     48 //
     49 // When an FEC packet is generated, it will be send to the Delegate,
     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 
     60 class NET_EXPORT_PRIVATE QuicPacketGenerator {
     61  public:
     62   class NET_EXPORT_PRIVATE DelegateInterface {
     63    public:
     64     virtual ~DelegateInterface() {}
     65     virtual bool CanWrite(Retransmission retransmission,
     66                           HasRetransmittableData retransmittable,
     67                           IsHandshake handshake) = 0;
     68     virtual QuicAckFrame* CreateAckFrame() = 0;
     69     virtual QuicCongestionFeedbackFrame* CreateFeedbackFrame() = 0;
     70     // Takes ownership of |packet.packet| and |packet.retransmittable_frames|.
     71     virtual bool OnSerializedPacket(const SerializedPacket& packet) = 0;
     72   };
     73 
     74   // Interface which gets callbacks from the QuicPacketGenerator at interesting
     75   // points.  Implementations must not mutate the state of the generator
     76   // as a result of these callbacks.
     77   class NET_EXPORT_PRIVATE DebugDelegateInterface {
     78    public:
     79     virtual ~DebugDelegateInterface() {}
     80 
     81     // Called when a frame has been added to the current packet.
     82     virtual void OnFrameAddedToPacket(const QuicFrame& frame) = 0;
     83   };
     84 
     85   QuicPacketGenerator(DelegateInterface* delegate,
     86                       DebugDelegateInterface* debug_delegate,
     87                       QuicPacketCreator* creator);
     88 
     89   virtual ~QuicPacketGenerator();
     90 
     91   void SetShouldSendAck(bool also_send_feedback);
     92   void AddControlFrame(const QuicFrame& frame);
     93   QuicConsumedData ConsumeData(QuicStreamId id,
     94                                base::StringPiece data,
     95                                QuicStreamOffset offset,
     96                                bool fin);
     97 
     98   // Disables flushing.
     99   void StartBatchOperations();
    100   // Enables flushing and flushes queued data.
    101   void FinishBatchOperations();
    102 
    103   bool HasQueuedFrames() const;
    104 
    105   void set_debug_delegate(DebugDelegateInterface* debug_delegate) {
    106     debug_delegate_ = debug_delegate;
    107   }
    108 
    109  private:
    110   void SendQueuedFrames();
    111 
    112   // Test to see if we have pending ack, feedback, or control frames.
    113   bool HasPendingFrames() const;
    114   // Test to see if the addition of a pending frame (which might be
    115   // retransmittable) would still allow the resulting packet to be sent now.
    116   bool CanSendWithNextPendingFrameAddition() const;
    117   // Add exactly one pending frame, preferring ack over feedback over control
    118   // frames.
    119   bool AddNextPendingFrame();
    120 
    121   bool AddFrame(const QuicFrame& frame);
    122   void SerializeAndSendPacket();
    123 
    124   DelegateInterface* delegate_;
    125   DebugDelegateInterface* debug_delegate_;
    126 
    127   QuicPacketCreator* packet_creator_;
    128   QuicFrames queued_control_frames_;
    129   bool should_flush_;
    130   // Flags to indicate the need for just-in-time construction of a frame.
    131   bool should_send_ack_;
    132   bool should_send_feedback_;
    133   // If we put a non-retransmittable frame (namley ack or feedback frame) in
    134   // this packet, then we have to hold a reference to it until we flush (and
    135   // serialize it). Retransmittable frames are referenced elsewhere so that they
    136   // can later be (optionally) retransmitted.
    137   scoped_ptr<QuicAckFrame> pending_ack_frame_;
    138   scoped_ptr<QuicCongestionFeedbackFrame> pending_feedback_frame_;
    139 
    140   DISALLOW_COPY_AND_ASSIGN(QuicPacketGenerator);
    141 };
    142 
    143 }  // namespace net
    144 
    145 #endif  // NET_QUIC_QUIC_PACKET_GENERATOR_H_
    146