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 // Accumulates frames for the next packet until more frames no longer fit or
      6 // it's time to create a packet from them.  Also provides packet creation of
      7 // FEC packets based on previously created packets.
      8 
      9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_
     10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_
     11 
     12 #include <utility>
     13 #include <vector>
     14 
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/strings/string_piece.h"
     17 #include "net/quic/quic_fec_group.h"
     18 #include "net/quic/quic_framer.h"
     19 #include "net/quic/quic_protocol.h"
     20 
     21 namespace net {
     22 namespace test {
     23 class QuicPacketCreatorPeer;
     24 }
     25 
     26 class QuicRandom;
     27 
     28 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
     29  public:
     30   // Options for controlling how packets are created.
     31   struct Options {
     32     Options()
     33         : max_packet_length(kMaxPacketSize),
     34           random_reorder(false),
     35           max_packets_per_fec_group(0),
     36           send_guid_length(PACKET_8BYTE_GUID),
     37           send_sequence_number_length(PACKET_6BYTE_SEQUENCE_NUMBER) {
     38     }
     39 
     40     size_t max_packet_length;
     41     bool random_reorder;   // Inefficient: rewrite if used at scale.
     42     // 0 indicates fec is disabled.
     43     size_t max_packets_per_fec_group;
     44     // Length of guid to send over the wire.
     45     QuicGuidLength send_guid_length;
     46     QuicSequenceNumberLength send_sequence_number_length;
     47   };
     48 
     49   // QuicRandom* required for packet entropy.
     50   QuicPacketCreator(QuicGuid guid,
     51                     QuicFramer* framer,
     52                     QuicRandom* random_generator,
     53                     bool is_server);
     54 
     55   virtual ~QuicPacketCreator();
     56 
     57   // QuicFecBuilderInterface
     58   virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
     59                                           base::StringPiece payload) OVERRIDE;
     60 
     61   // Checks if it's time to send an FEC packet.  |force_close| forces this to
     62   // return true if an fec group is open.
     63   bool ShouldSendFec(bool force_close) const;
     64 
     65   // Starts a new FEC group with the next serialized packet, if FEC is enabled
     66   // and there is not already an FEC group open.
     67   void MaybeStartFEC();
     68 
     69   // Makes the framer not serialize the protocol version in sent packets.
     70   void StopSendingVersion();
     71 
     72   // The overhead the framing will add for a packet with one frame.
     73   static size_t StreamFramePacketOverhead(
     74       QuicVersion version,
     75       QuicGuidLength guid_length,
     76       bool include_version,
     77       QuicSequenceNumberLength sequence_number_length,
     78       InFecGroup is_in_fec_group);
     79 
     80   bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
     81 
     82   // Converts a raw payload to a frame which fits into the currently open
     83   // packet if there is one.  Returns the number of bytes consumed from data.
     84   // If data is empty and fin is true, the expected behavior is to consume the
     85   // fin but return 0.
     86   size_t CreateStreamFrame(QuicStreamId id,
     87                            base::StringPiece data,
     88                            QuicStreamOffset offset,
     89                            bool fin,
     90                            QuicFrame* frame);
     91 
     92   // Serializes all frames into a single packet. All frames must fit into a
     93   // single packet. Also, sets the entropy hash of the serialized packet to a
     94   // random bool and returns that value as a member of SerializedPacket.
     95   // Never returns a RetransmittableFrames in SerializedPacket.
     96   SerializedPacket SerializeAllFrames(const QuicFrames& frames);
     97 
     98   // Returns true if there are frames pending to be serialized.
     99   bool HasPendingFrames();
    100 
    101   // Returns the number of bytes which are free to frames in the current packet.
    102   size_t BytesFree() const;
    103 
    104   // Adds |frame| to the packet creator's list of frames to be serialized.
    105   // Returns false if the frame doesn't fit into the current packet.
    106   bool AddSavedFrame(const QuicFrame& frame);
    107 
    108   // Serializes all frames which have been added and adds any which should be
    109   // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
    110   // fit into a single packet. Sets the entropy hash of the serialized
    111   // packet to a random bool and returns that value as a member of
    112   // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
    113   // to the corresponding RetransmittableFrames if any frames are to be
    114   // retransmitted.
    115   SerializedPacket SerializePacket();
    116 
    117   // Packetize FEC data. All frames must fit into a single packet. Also, sets
    118   // the entropy hash of the serialized packet to a random bool and returns
    119   // that value as a member of SerializedPacket.
    120   SerializedPacket SerializeFec();
    121 
    122   // Creates a packet with connection close frame. Caller owns the created
    123   // packet. Also, sets the entropy hash of the serialized packet to a random
    124   // bool and returns that value as a member of SerializedPacket.
    125   SerializedPacket SerializeConnectionClose(
    126       QuicConnectionCloseFrame* close_frame);
    127 
    128   // Creates a version negotiation packet which supports |supported_versions|.
    129   // Caller owns the created  packet. Also, sets the entropy hash of the
    130   // serialized packet to a random bool and returns that value as a member of
    131   // SerializedPacket.
    132   QuicEncryptedPacket* SerializeVersionNegotiationPacket(
    133       const QuicVersionVector& supported_versions);
    134 
    135   QuicPacketSequenceNumber sequence_number() const {
    136     return sequence_number_;
    137   }
    138 
    139   void set_sequence_number(QuicPacketSequenceNumber s) {
    140     sequence_number_ = s;
    141   }
    142 
    143   Options* options() {
    144     return &options_;
    145   }
    146 
    147  private:
    148   friend class test::QuicPacketCreatorPeer;
    149 
    150   static bool ShouldRetransmit(const QuicFrame& frame);
    151 
    152   void FillPacketHeader(QuicFecGroupNumber fec_group,
    153                         bool fec_flag,
    154                         bool fec_entropy_flag,
    155                         QuicPacketHeader* header);
    156 
    157   // Allows a frame to be added without creating retransmittable frames.
    158   // Particularly useful for retransmits using SerializeAllFrames().
    159   bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
    160 
    161   Options options_;
    162   QuicGuid guid_;
    163   QuicFramer* framer_;
    164   QuicRandom* random_generator_;
    165   QuicPacketSequenceNumber sequence_number_;
    166   QuicFecGroupNumber fec_group_number_;
    167   scoped_ptr<QuicFecGroup> fec_group_;
    168   // bool to keep track if this packet creator is being used the server.
    169   bool is_server_;
    170   // Controls whether protocol version should be included while serializing the
    171   // packet.
    172   bool send_version_in_packet_;
    173   size_t packet_size_;
    174   QuicFrames queued_frames_;
    175   scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
    176 
    177   DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
    178 };
    179 
    180 }  // namespace net
    181 
    182 #endif  // NET_QUIC_QUIC_PACKET_CREATOR_H_
    183