- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / quic_packet_creator.h
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 NET_EXPORT_PRIVATE extern bool FLAGS_pad_quic_handshake_packets;
22
23 namespace net {
24 namespace test {
25 class QuicPacketCreatorPeer;
26 }
27
28 class QuicAckNotifier;
29 class QuicRandom;
30
31 class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
32  public:
33   // Options for controlling how packets are created.
34   struct Options {
35     Options()
36         : max_packet_length(kDefaultMaxPacketSize),
37           max_packets_per_fec_group(0),
38           send_guid_length(PACKET_8BYTE_GUID),
39           send_sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER) {}
40
41     size_t max_packet_length;
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   // Makes the framer not serialize the protocol version in sent packets.
66   void StopSendingVersion();
67
68   // Update the sequence number length to use in future packets as soon as it
69   // can be safely changed.
70   void UpdateSequenceNumberLength(
71       QuicPacketSequenceNumber least_packet_awaited_by_peer,
72       QuicByteCount bytes_per_second);
73
74   // The overhead the framing will add for a packet with one frame.
75   static size_t StreamFramePacketOverhead(
76       QuicVersion version,
77       QuicGuidLength guid_length,
78       bool include_version,
79       QuicSequenceNumberLength sequence_number_length,
80       InFecGroup is_in_fec_group);
81
82   bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
83
84   // Converts a raw payload to a frame which fits into the currently open
85   // packet if there is one.  Returns the number of bytes consumed from data.
86   // If data is empty and fin is true, the expected behavior is to consume the
87   // fin but return 0.
88   size_t CreateStreamFrame(QuicStreamId id,
89                            base::StringPiece data,
90                            QuicStreamOffset offset,
91                            bool fin,
92                            QuicFrame* frame);
93
94   // As above, but keeps track of an QuicAckNotifier that should be called when
95   // the packet that contains this stream frame is ACKed.
96   // The |notifier| is not owned by the QuicPacketGenerator and must outlive the
97   // generated packet.
98   size_t CreateStreamFrameWithNotifier(QuicStreamId id,
99                                        base::StringPiece data,
100                                        QuicStreamOffset offset,
101                                        bool fin,
102                                        QuicAckNotifier* notifier,
103                                        QuicFrame* frame);
104
105   // Serializes all frames into a single packet. All frames must fit into a
106   // single packet. Also, sets the entropy hash of the serialized packet to a
107   // random bool and returns that value as a member of SerializedPacket.
108   // Never returns a RetransmittableFrames in SerializedPacket.
109   SerializedPacket SerializeAllFrames(const QuicFrames& frames);
110
111   // Re-serializes frames with the original packet's sequence number length.
112   // Used for retransmitting packets to ensure they aren't too long.
113   SerializedPacket ReserializeAllFrames(
114       const QuicFrames& frames, QuicSequenceNumberLength original_length);
115
116   // Returns true if there are frames pending to be serialized.
117   bool HasPendingFrames();
118
119   // Returns the number of bytes which are available to be used by additional
120   // frames in the packet.  Since stream frames are slightly smaller when they
121   // are the last frame in a packet, this method will return a different
122   // value than max_packet_size - PacketSize(), in this case.
123   size_t BytesFree() const;
124
125   // Returns the number of bytes in the current packet, including the header,
126   // if serialized with the current frames.  Adding a frame to the packet
127   // may change the serialized length of existing frames, as per the comment
128   // in BytesFree.
129   size_t PacketSize() const;
130
131   // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
132   // if it is a stream frame, not other types of frames. Fix this API;
133   // add a AddNonSavedFrame method.
134   // Adds |frame| to the packet creator's list of frames to be serialized.
135   // Returns false if the frame doesn't fit into the current packet.
136   bool AddSavedFrame(const QuicFrame& frame);
137
138   // Serializes all frames which have been added and adds any which should be
139   // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
140   // fit into a single packet. Sets the entropy hash of the serialized
141   // packet to a random bool and returns that value as a member of
142   // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
143   // to the corresponding RetransmittableFrames if any frames are to be
144   // retransmitted.
145   SerializedPacket SerializePacket();
146
147   // Packetize FEC data. All frames must fit into a single packet. Also, sets
148   // the entropy hash of the serialized packet to a random bool and returns
149   // that value as a member of SerializedPacket.
150   SerializedPacket SerializeFec();
151
152   // Creates a packet with connection close frame. Caller owns the created
153   // packet. Also, sets the entropy hash of the serialized packet to a random
154   // bool and returns that value as a member of SerializedPacket.
155   SerializedPacket SerializeConnectionClose(
156       QuicConnectionCloseFrame* close_frame);
157
158   // Creates a version negotiation packet which supports |supported_versions|.
159   // Caller owns the created  packet. Also, sets the entropy hash of the
160   // serialized packet to a random bool and returns that value as a member of
161   // SerializedPacket.
162   QuicEncryptedPacket* SerializeVersionNegotiationPacket(
163       const QuicVersionVector& supported_versions);
164
165   // Sequence number of the last created packet, or 0 if no packets have been
166   // created.
167   QuicPacketSequenceNumber sequence_number() const {
168     return sequence_number_;
169   }
170
171   void set_sequence_number(QuicPacketSequenceNumber s) {
172     sequence_number_ = s;
173   }
174
175   Options* options() {
176     return &options_;
177   }
178
179  private:
180   friend class test::QuicPacketCreatorPeer;
181
182   static bool ShouldRetransmit(const QuicFrame& frame);
183
184   // Starts a new FEC group with the next serialized packet, if FEC is enabled
185   // and there is not already an FEC group open.
186   void MaybeStartFEC();
187
188   void FillPacketHeader(QuicFecGroupNumber fec_group,
189                         bool fec_flag,
190                         bool fec_entropy_flag,
191                         QuicPacketHeader* header);
192
193   // Allows a frame to be added without creating retransmittable frames.
194   // Particularly useful for retransmits using SerializeAllFrames().
195   bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
196
197   // Adds a padding frame to the current packet only if the current packet
198   // contains a handshake message, and there is sufficient room to fit a
199   // padding frame.
200   void MaybeAddPadding();
201
202   Options options_;
203   QuicGuid guid_;
204   QuicFramer* framer_;
205   QuicRandom* random_generator_;
206   QuicPacketSequenceNumber sequence_number_;
207   QuicFecGroupNumber fec_group_number_;
208   scoped_ptr<QuicFecGroup> fec_group_;
209   // bool to keep track if this packet creator is being used the server.
210   bool is_server_;
211   // Controls whether protocol version should be included while serializing the
212   // packet.
213   bool send_version_in_packet_;
214   // The sequence number length for the current packet and the current FEC group
215   // if FEC is enabled.
216   // Mutable so PacketSize() can adjust it when the packet is empty.
217   mutable QuicSequenceNumberLength sequence_number_length_;
218   // packet_size_ is mutable because it's just a cache of the current size.
219   // packet_size should never be read directly, use PacketSize() instead.
220   mutable size_t packet_size_;
221   QuicFrames queued_frames_;
222   scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
223
224   DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
225 };
226
227 }  // namespace net
228
229 #endif  // NET_QUIC_QUIC_PACKET_CREATOR_H_