| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Accumulates frames for the next packet until more frames no longer fit or | 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 | 6 // it's time to create a packet from them. Also provides packet creation of |
| 7 // FEC packets based on previously created packets. | 7 // FEC packets based on previously created packets. |
| 8 | 8 |
| 9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ | 9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ |
| 10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_ | 10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_ |
| 11 | 11 |
| 12 #include <utility> | 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 #include "net/quic/quic_fec_group.h" | 17 #include "net/quic/quic_fec_group.h" |
| 18 #include "net/quic/quic_framer.h" | 18 #include "net/quic/quic_framer.h" |
| 19 #include "net/quic/quic_protocol.h" | 19 #include "net/quic/quic_protocol.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 class QuicRandom; |
| 24 |
| 23 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface { | 25 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface { |
| 24 public: | 26 public: |
| 25 typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair; | |
| 26 | |
| 27 // Options for controlling how packets are created. | 27 // Options for controlling how packets are created. |
| 28 struct Options { | 28 struct Options { |
| 29 Options() | 29 Options() |
| 30 : max_packet_length(kMaxPacketSize), | 30 : max_packet_length(kMaxPacketSize), |
| 31 random_reorder(false), | 31 random_reorder(false), |
| 32 max_packets_per_fec_group(0) { | 32 max_packets_per_fec_group(0) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 size_t max_packet_length; | 35 size_t max_packet_length; |
| 36 bool random_reorder; // Inefficient: rewrite if used at scale. | 36 bool random_reorder; // Inefficient: rewrite if used at scale. |
| 37 // 0 indicates fec is disabled. | 37 // 0 indicates fec is disabled. |
| 38 size_t max_packets_per_fec_group; | 38 size_t max_packets_per_fec_group; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 QuicPacketCreator(QuicGuid guid, QuicFramer* framer); | 41 // QuicRandom* required for packet entropy. |
| 42 QuicPacketCreator(QuicGuid guid, |
| 43 QuicFramer* framer, |
| 44 QuicRandom* random_generator); |
| 42 | 45 |
| 43 virtual ~QuicPacketCreator(); | 46 virtual ~QuicPacketCreator(); |
| 44 | 47 |
| 45 // QuicFecBuilderInterface | 48 // QuicFecBuilderInterface |
| 46 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header, | 49 virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header, |
| 47 base::StringPiece payload) OVERRIDE; | 50 base::StringPiece payload) OVERRIDE; |
| 48 | 51 |
| 49 // Checks if it's time to send an FEC packet. |force_close| forces this to | 52 // Checks if it's time to send an FEC packet. |force_close| forces this to |
| 50 // return true if an fec group is open. | 53 // return true if an fec group is open. |
| 51 bool ShouldSendFec(bool force_close) const; | 54 bool ShouldSendFec(bool force_close) const; |
| 52 | 55 |
| 53 // Starts a new FEC group with the next serialized packet, if FEC is enabled | 56 // Starts a new FEC group with the next serialized packet, if FEC is enabled |
| 54 // and there is not already an FEC group open. | 57 // and there is not already an FEC group open. |
| 55 void MaybeStartFEC(); | 58 void MaybeStartFEC(); |
| 56 | 59 |
| 60 bool HasRoomForStreamFrame() const; |
| 61 |
| 57 // Converts a raw payload to a frame which fits into the currently open | 62 // Converts a raw payload to a frame which fits into the currently open |
| 58 // packet if there is one. Returns the number of bytes consumed from data. | 63 // packet if there is one. Returns the number of bytes consumed from data. |
| 59 // If data is empty and fin is true, the expected behavior is to consume the | 64 // If data is empty and fin is true, the expected behavior is to consume the |
| 60 // fin but return 0. | 65 // fin but return 0. |
| 61 size_t CreateStreamFrame(QuicStreamId id, | 66 size_t CreateStreamFrame(QuicStreamId id, |
| 62 base::StringPiece data, | 67 base::StringPiece data, |
| 63 QuicStreamOffset offset, | 68 QuicStreamOffset offset, |
| 64 bool fin, | 69 bool fin, |
| 65 QuicFrame* frame); | 70 QuicFrame* frame); |
| 66 | 71 |
| 67 // Serializes all frames into a single packet. All frames must fit into a | 72 // Serializes all frames into a single packet. All frames must fit into a |
| 68 // single packet. | 73 // single packet. Also, sets the entropy hash of the serialized packet to a |
| 69 PacketPair SerializeAllFrames(const QuicFrames& frames); | 74 // random bool and returns that value as a member of SerializedPacket. |
| 75 // Never returns an RetransmittableFrames in SerializedPacket |
| 76 SerializedPacket SerializeAllFrames(const QuicFrames& frames); |
| 70 | 77 |
| 71 // Returns true if there are frames pending to be serialized. | 78 // Returns true if there are frames pending to be serialized. |
| 72 bool HasPendingFrames(); | 79 bool HasPendingFrames(); |
| 73 | 80 |
| 74 // Returns the number of bytes which are free to frames in the current packet. | 81 // Returns the number of bytes which are free to frames in the current packet. |
| 75 size_t BytesFree(); | 82 size_t BytesFree() const; |
| 76 | 83 |
| 77 // Adds |frame| to the packet creator's list of frames to be serialized. | 84 // Adds |frame| to the packet creator's list of frames to be serialized. |
| 78 // Returns false if the frame doesn't fit into the current packet. | 85 // Returns false if the frame doesn't fit into the current packet. |
| 79 bool AddFrame(const QuicFrame& frame); | 86 bool AddSavedFrame(const QuicFrame& frame); |
| 80 | 87 |
| 81 // Serializes all frames which have been added and adds any which should be | 88 // Serializes all frames which have been added and adds any which should be |
| 82 // retransmitted to |retransmittable_frames| if it's not NULL. | 89 // retransmitted to |retransmittable_frames| if it's not NULL. All frames must |
| 83 PacketPair SerializePacket(QuicFrames* retransmittable_frames); | 90 // fit into a single packet. Sets the entropy hash of the serialized |
| 91 // packet to a random bool and returns that value as a member of |
| 92 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket |
| 93 // to the corresponding RetransmittableFrames if any frames are to be |
| 94 // retransmitted. |
| 95 SerializedPacket SerializePacket(); |
| 84 | 96 |
| 85 // Packetize FEC data. | 97 // Packetize FEC data. All frames must fit into a single packet. Also, sets |
| 86 PacketPair SerializeFec(); | 98 // the entropy hash of the serialized packet to a random bool and returns |
| 99 // that value as a member of SerializedPacket. |
| 100 SerializedPacket SerializeFec(); |
| 87 | 101 |
| 88 // Creates a packet with connection close frame. Caller owns the created | 102 // Creates a packet with connection close frame. Caller owns the created |
| 89 // packet. | 103 // packet. Also, sets the entropy hash of the serialized packet to a random |
| 90 PacketPair CloseConnection(QuicConnectionCloseFrame* close_frame); | 104 // bool and returns that value as a member of SerializedPacket. |
| 105 SerializedPacket SerializeConnectionClose( |
| 106 QuicConnectionCloseFrame* close_frame); |
| 91 | 107 |
| 92 QuicPacketSequenceNumber sequence_number() const { | 108 QuicPacketSequenceNumber sequence_number() const { |
| 93 return sequence_number_; | 109 return sequence_number_; |
| 94 } | 110 } |
| 95 | 111 |
| 96 void set_sequence_number(QuicPacketSequenceNumber s) { | 112 void set_sequence_number(QuicPacketSequenceNumber s) { |
| 97 sequence_number_ = s; | 113 sequence_number_ = s; |
| 98 } | 114 } |
| 99 | 115 |
| 100 Options* options() { | 116 Options* options() { |
| 101 return &options_; | 117 return &options_; |
| 102 } | 118 } |
| 103 | 119 |
| 104 private: | 120 private: |
| 105 static bool ShouldRetransmit(const QuicFrame& frame); | 121 static bool ShouldRetransmit(const QuicFrame& frame); |
| 106 | 122 |
| 107 void FillPacketHeader(QuicFecGroupNumber fec_group, | 123 void FillPacketHeader(QuicFecGroupNumber fec_group, |
| 108 QuicPacketPrivateFlags flags, | 124 bool fec_flag, |
| 125 bool fec_entropy_flag, |
| 109 QuicPacketHeader* header); | 126 QuicPacketHeader* header); |
| 110 | 127 |
| 128 // Allows a frame to be added without creating retransmittable frames. |
| 129 // Particularly useful for retransmits using SerializeAllFrames(). |
| 130 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); |
| 131 |
| 111 Options options_; | 132 Options options_; |
| 112 QuicGuid guid_; | 133 QuicGuid guid_; |
| 113 QuicFramer* framer_; | 134 QuicFramer* framer_; |
| 135 QuicRandom* random_generator_; |
| 114 QuicPacketSequenceNumber sequence_number_; | 136 QuicPacketSequenceNumber sequence_number_; |
| 115 QuicFecGroupNumber fec_group_number_; | 137 QuicFecGroupNumber fec_group_number_; |
| 116 scoped_ptr<QuicFecGroup> fec_group_; | 138 scoped_ptr<QuicFecGroup> fec_group_; |
| 117 size_t packet_size_; | 139 size_t packet_size_; |
| 118 QuicFrames queued_frames_; | 140 QuicFrames queued_frames_; |
| 141 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_; |
| 119 }; | 142 }; |
| 120 | 143 |
| 121 } // namespace net | 144 } // namespace net |
| 122 | 145 |
| 123 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ | 146 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ |
| OLD | NEW |