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