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_ |
(...skipping 11 matching lines...) Expand all Loading... | |
22 namespace net { | 22 namespace net { |
23 namespace test { | 23 namespace test { |
24 class QuicPacketCreatorPeer; | 24 class QuicPacketCreatorPeer; |
25 } | 25 } |
26 | 26 |
27 class QuicRandom; | 27 class QuicRandom; |
28 class QuicRandomBoolSource; | 28 class QuicRandomBoolSource; |
29 | 29 |
30 class NET_EXPORT_PRIVATE QuicPacketCreator { | 30 class NET_EXPORT_PRIVATE QuicPacketCreator { |
31 public: | 31 public: |
32 // A delegate interface for further processing serialized packet. | |
33 class DelegateInterface { | |
Ryan Hamilton
2015/11/24 04:02:48
Looks like you need NET_EXPORT_PRIVATE here.
| |
34 public: | |
35 virtual ~DelegateInterface() {} | |
36 // Called when a packet is serialized. Delegate does not take the ownership | |
37 // of |serialized_packet|. | |
38 virtual void OnSerializedPacket(SerializedPacket* serialized_packet) = 0; | |
39 }; | |
40 | |
32 // QuicRandom* required for packet entropy. | 41 // QuicRandom* required for packet entropy. |
33 QuicPacketCreator(QuicConnectionId connection_id, | 42 QuicPacketCreator(QuicConnectionId connection_id, |
34 QuicFramer* framer, | 43 QuicFramer* framer, |
35 QuicRandom* random_generator); | 44 QuicRandom* random_generator, |
45 DelegateInterface* delegate); | |
36 | 46 |
37 ~QuicPacketCreator(); | 47 ~QuicPacketCreator(); |
38 | 48 |
39 // Turn on FEC protection for subsequently created packets. FEC should be | 49 // Turn on FEC protection for subsequently created packets. FEC should be |
40 // enabled first (max_packets_per_fec_group should be non-zero) for FEC | 50 // enabled first (max_packets_per_fec_group should be non-zero) for FEC |
41 // protection to start. | 51 // protection to start. |
42 void StartFecProtectingPackets(); | 52 void StartFecProtectingPackets(); |
43 | 53 |
44 // Turn off FEC protection for subsequently created packets. If the creator | 54 // Turn off FEC protection for subsequently created packets. If the creator |
45 // has any open FEC group, call will fail. It is the caller's responsibility | 55 // has any open FEC group, call will fail. It is the caller's responsibility |
(...skipping 21 matching lines...) Expand all Loading... | |
67 QuicPacketCount max_packets_in_flight); | 77 QuicPacketCount max_packets_in_flight); |
68 | 78 |
69 // The overhead the framing will add for a packet with one frame. | 79 // The overhead the framing will add for a packet with one frame. |
70 static size_t StreamFramePacketOverhead( | 80 static size_t StreamFramePacketOverhead( |
71 QuicConnectionIdLength connection_id_length, | 81 QuicConnectionIdLength connection_id_length, |
72 bool include_version, | 82 bool include_version, |
73 QuicPacketNumberLength packet_number_length, | 83 QuicPacketNumberLength packet_number_length, |
74 QuicStreamOffset offset, | 84 QuicStreamOffset offset, |
75 InFecGroup is_in_fec_group); | 85 InFecGroup is_in_fec_group); |
76 | 86 |
87 // Returns false and flushes all pending frames if current open packet is | |
88 // full. | |
89 // If current packet is not full, converts a raw payload into a stream frame | |
90 // that fits into the open packet and adds it to the packet. | |
91 // The payload begins at |iov_offset| into the |iov|. | |
92 bool ConsumeData(QuicStreamId id, | |
93 QuicIOVector iov, | |
94 size_t iov_offset, | |
95 QuicStreamOffset offset, | |
96 bool fin, | |
97 bool needs_padding, | |
98 QuicFrame* frame); | |
99 | |
100 // Returns true if current open packet can accommodate more stream frames of | |
101 // stream |id| at |offset|, false otherwise. | |
77 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const; | 102 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const; |
78 | 103 |
79 // Converts a raw payload to a frame which fits into the currently open | |
80 // packet. The payload begins at |iov_offset| into the |iov|. | |
81 // Returns the number of bytes consumed from data. | |
82 // If data is empty and fin is true, the expected behavior is to consume the | |
83 // fin but return 0. If any data is consumed, it will be copied into a | |
84 // new buffer that |frame| will point to and will be stored in |buffer|. | |
85 size_t CreateStreamFrame(QuicStreamId id, | |
86 QuicIOVector iov, | |
87 size_t iov_offset, | |
88 QuicStreamOffset offset, | |
89 bool fin, | |
90 QuicFrame* frame, | |
91 UniqueStreamBuffer* buffer); | |
92 | |
93 // Serializes all frames into a single packet. All frames must fit into a | 104 // Serializes all frames into a single packet. All frames must fit into a |
94 // single packet. Also, sets the entropy hash of the serialized packet to a | 105 // single packet. Also, sets the entropy hash of the serialized packet to a |
95 // random bool and returns that value as a member of SerializedPacket. | 106 // random bool and returns that value as a member of SerializedPacket. |
96 // Never returns a RetransmittableFrames in SerializedPacket. | 107 // Never returns a RetransmittableFrames in SerializedPacket. |
97 SerializedPacket SerializeAllFrames(const QuicFrames& frames, | 108 SerializedPacket SerializeAllFrames(const QuicFrames& frames, |
98 char* buffer, | 109 char* buffer, |
99 size_t buffer_len); | 110 size_t buffer_len); |
100 | 111 |
101 // Re-serializes frames with the original packet's packet number length. | 112 // Re-serializes frames with the original packet's packet number length. |
102 // Used for retransmitting packets to ensure they aren't too long. | 113 // Used for retransmitting packets to ensure they aren't too long. |
103 // Caller must ensure that any open FEC group is closed before calling this | 114 // Caller must ensure that any open FEC group is closed before calling this |
104 // method. | 115 // method. |
105 SerializedPacket ReserializeAllFrames(const RetransmittableFrames& frames, | 116 SerializedPacket ReserializeAllFrames(const RetransmittableFrames& frames, |
106 QuicPacketNumberLength original_length, | 117 QuicPacketNumberLength original_length, |
107 char* buffer, | 118 char* buffer, |
108 size_t buffer_len); | 119 size_t buffer_len); |
109 | 120 |
121 // Serializes all added frames into a single packet and invokes the delegate_ | |
122 // to further process the SerializedPacket. | |
123 void Flush(); | |
124 | |
110 // Returns true if there are frames pending to be serialized. | 125 // Returns true if there are frames pending to be serialized. |
111 bool HasPendingFrames() const; | 126 bool HasPendingFrames() const; |
112 | 127 |
113 // Returns true if there are retransmittable frames pending to be serialized. | 128 // Returns true if there are retransmittable frames pending to be serialized. |
114 bool HasPendingRetransmittableFrames() const; | 129 bool HasPendingRetransmittableFrames() const; |
115 | 130 |
116 // TODO(jri): Remove this method. | 131 // TODO(jri): Remove this method. |
117 // Returns whether FEC protection is currently enabled. Note: Enabled does not | 132 // Returns whether FEC protection is currently enabled. Note: Enabled does not |
118 // mean that an FEC group is currently active; i.e., IsFecProtected() may | 133 // mean that an FEC group is currently active; i.e., IsFecProtected() may |
119 // still return false. | 134 // still return false. |
(...skipping 19 matching lines...) Expand all Loading... | |
139 | 154 |
140 // Returns the number of bytes in the current packet, including the header, | 155 // Returns the number of bytes in the current packet, including the header, |
141 // if serialized with the current frames. Adding a frame to the packet | 156 // if serialized with the current frames. Adding a frame to the packet |
142 // may change the serialized length of existing frames, as per the comment | 157 // may change the serialized length of existing frames, as per the comment |
143 // in BytesFree. | 158 // in BytesFree. |
144 size_t PacketSize() const; | 159 size_t PacketSize() const; |
145 | 160 |
146 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame | 161 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame |
147 // if it is a stream frame, not other types of frames. Fix this API; | 162 // if it is a stream frame, not other types of frames. Fix this API; |
148 // add a AddNonSavedFrame method. | 163 // add a AddNonSavedFrame method. |
149 // Adds |frame| to the packet creator's list of frames to be serialized. | 164 // Tries to add |frame| to the packet creator's std::list of frames to be |
150 // Returns false if the frame doesn't fit into the current packet. | 165 // serialized. If the frame does not fit into the current packet, flushes the |
166 // packet and returns false. | |
151 bool AddSavedFrame(const QuicFrame& frame); | 167 bool AddSavedFrame(const QuicFrame& frame); |
152 | 168 |
153 // Identical to AddSavedFrame, but takes ownership of the buffer. | 169 // Identical to AddSavedFrame, but takes ownership of the buffer. |
154 bool AddSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer); | 170 bool AddSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer); |
155 | 171 |
156 // Identical to AddSavedFrame, but takes ownership of the buffer, and allows | 172 // Identical to AddSavedFrame, but takes ownership of the buffer, and allows |
157 // to cause the packet to be padded. | 173 // to cause the packet to be padded. |
158 bool AddPaddedSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer); | 174 bool AddPaddedSavedFrame(const QuicFrame& frame, UniqueStreamBuffer buffer); |
159 | 175 |
160 // Serializes all frames which have been added and adds any which should be | |
161 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames | |
162 // must fit into a single packet. Sets the entropy hash of the serialized | |
163 // packet to a random bool and returns that value as a member of | |
164 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to | |
165 // the corresponding RetransmittableFrames if any frames are to be | |
166 // retransmitted. | |
167 // Fails if |buffer_len| isn't long enough for the encrypted packet. | |
168 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len); | |
169 | |
170 // Packetize FEC data. All frames must fit into a single packet. Also, sets | 176 // Packetize FEC data. All frames must fit into a single packet. Also, sets |
171 // the entropy hash of the serialized packet to a random bool and returns | 177 // the entropy hash of the serialized packet to a random bool and returns |
172 // that value as a member of SerializedPacket. | 178 // that value as a member of SerializedPacket. |
173 // Fails if |buffer_len| isn't long enough for the encrypted packet. | 179 // Fails if |buffer_len| isn't long enough for the encrypted packet. |
174 SerializedPacket SerializeFec(char* buffer, size_t buffer_len); | 180 SerializedPacket SerializeFec(char* buffer, size_t buffer_len); |
175 | 181 |
176 // Creates a version negotiation packet which supports |supported_versions|. | 182 // Creates a version negotiation packet which supports |supported_versions|. |
177 // Caller owns the created packet. Also, sets the entropy hash of the | 183 // Caller owns the created packet. Also, sets the entropy hash of the |
178 // serialized packet to a random bool and returns that value as a member of | 184 // serialized packet to a random bool and returns that value as a member of |
179 // SerializedPacket. | 185 // SerializedPacket. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 size_t max_packets_per_fec_group() const { | 225 size_t max_packets_per_fec_group() const { |
220 return max_packets_per_fec_group_; | 226 return max_packets_per_fec_group_; |
221 } | 227 } |
222 | 228 |
223 // Sets creator's max number of packets covered by an FEC group. | 229 // Sets creator's max number of packets covered by an FEC group. |
224 // Note: While there are no constraints on |max_packets_per_fec_group|, | 230 // Note: While there are no constraints on |max_packets_per_fec_group|, |
225 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup. | 231 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup. |
226 // To turn off FEC protection, use StopFecProtectingPackets(). | 232 // To turn off FEC protection, use StopFecProtectingPackets(). |
227 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group); | 233 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group); |
228 | 234 |
229 // Returns the currently open FEC group's number. Returns 0 when FEC is | 235 // Returns the current open FEC group's number. Returns 0 when FEC is |
230 // disabled or no FEC group is open. | 236 // disabled or no FEC group is open. |
231 QuicFecGroupNumber fec_group_number(); | 237 QuicFecGroupNumber fec_group_number(); |
232 | 238 |
233 private: | 239 private: |
234 friend class test::QuicPacketCreatorPeer; | 240 friend class test::QuicPacketCreatorPeer; |
235 | 241 |
236 static bool ShouldRetransmit(const QuicFrame& frame); | 242 static bool ShouldRetransmit(const QuicFrame& frame); |
237 | 243 |
244 // Converts a raw payload to a frame which fits into the current open | |
245 // packet. The payload begins at |iov_offset| into the |iov|. | |
246 // Returns the number of bytes consumed from data. | |
247 // If data is empty and fin is true, the expected behavior is to consume the | |
248 // fin but return 0. If any data is consumed, it will be copied into a | |
249 // new buffer that |frame| will point to and will be stored in |buffer|. | |
250 size_t CreateStreamFrame(QuicStreamId id, | |
251 QuicIOVector iov, | |
252 size_t iov_offset, | |
253 QuicStreamOffset offset, | |
254 bool fin, | |
255 QuicFrame* frame, | |
256 UniqueStreamBuffer* buffer); | |
257 | |
238 // Copies |length| bytes from iov starting at offset |iov_offset| into buffer. | 258 // Copies |length| bytes from iov starting at offset |iov_offset| into buffer. |
239 // |iov| must be at least iov_offset+length total length and buffer must be | 259 // |iov| must be at least iov_offset+length total length and buffer must be |
240 // at least |length| long. | 260 // at least |length| long. |
241 static void CopyToBuffer(QuicIOVector iov, | 261 static void CopyToBuffer(QuicIOVector iov, |
242 size_t iov_offset, | 262 size_t iov_offset, |
243 size_t length, | 263 size_t length, |
244 char* buffer); | 264 char* buffer); |
245 | 265 |
246 // Updates lengths and also starts an FEC group if FEC protection is on and | 266 // Updates lengths and also starts an FEC group if FEC protection is on and |
247 // there is not already an FEC group open. | 267 // there is not already an FEC group open. |
248 InFecGroup MaybeUpdateLengthsAndStartFec(); | 268 InFecGroup MaybeUpdateLengthsAndStartFec(); |
249 | 269 |
250 // Called when a data packet is constructed that is part of an FEC group. | 270 // Called when a data packet is constructed that is part of an FEC group. |
251 // |payload| is the non-encrypted FEC protected payload of the packet. | 271 // |payload| is the non-encrypted FEC protected payload of the packet. |
252 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header, | 272 void OnBuiltFecProtectedPayload(const QuicPacketHeader& header, |
253 base::StringPiece payload); | 273 base::StringPiece payload); |
254 | 274 |
255 void FillPacketHeader(QuicFecGroupNumber fec_group, | 275 void FillPacketHeader(QuicFecGroupNumber fec_group, |
256 bool fec_flag, | 276 bool fec_flag, |
257 QuicPacketHeader* header); | 277 QuicPacketHeader* header); |
258 | 278 |
259 // Allows a frame to be added without creating retransmittable frames. | 279 // Allows a frame to be added without creating retransmittable frames. |
260 // Particularly useful for retransmits using SerializeAllFrames(). | 280 // Particularly useful for retransmits using SerializeAllFrames(). |
281 // If current open packet cannot accommodate |frame|, returns false and | |
282 // flushes all pending frames. | |
261 bool AddFrame(const QuicFrame& frame, | 283 bool AddFrame(const QuicFrame& frame, |
262 bool save_retransmittable_frames, | 284 bool save_retransmittable_frames, |
263 bool needs_padding, | 285 bool needs_padding, |
264 UniqueStreamBuffer buffer); | 286 UniqueStreamBuffer buffer); |
265 | 287 |
266 // Adds a padding frame to the current packet only if the current packet | 288 // Adds a padding frame to the current packet only if the current packet |
267 // contains a handshake message, and there is sufficient room to fit a | 289 // contains a handshake message, and there is sufficient room to fit a |
268 // padding frame. | 290 // padding frame. |
269 void MaybeAddPadding(); | 291 void MaybeAddPadding(); |
270 | 292 |
293 // Serializes all frames which have been added and adds any which should be | |
294 // retransmitted to queued_retransmittable_frames_ if it's not nullptr. All | |
295 // frames must fit into a single packet. Sets the entropy hash of the | |
296 // serialized packet to a random bool and returns that value as a member of | |
297 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to | |
298 // the corresponding RetransmittableFrames if any frames are to be | |
299 // retransmitted. | |
300 // Fails if |buffer_len| isn't long enough for the encrypted packet. | |
301 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len); | |
302 | |
303 // Does not own this delegate. | |
304 DelegateInterface* delegate_; | |
271 QuicConnectionId connection_id_; | 305 QuicConnectionId connection_id_; |
272 EncryptionLevel encryption_level_; | 306 EncryptionLevel encryption_level_; |
273 QuicFramer* framer_; | 307 QuicFramer* framer_; |
274 scoped_ptr<QuicRandomBoolSource> random_bool_source_; | 308 scoped_ptr<QuicRandomBoolSource> random_bool_source_; |
275 QuicPacketNumber packet_number_; | 309 QuicPacketNumber packet_number_; |
276 // If true, any created packets will be FEC protected. | 310 // If true, any created packets will be FEC protected. |
277 bool should_fec_protect_; | 311 bool should_fec_protect_; |
278 scoped_ptr<QuicFecGroup> fec_group_; | 312 scoped_ptr<QuicFecGroup> fec_group_; |
279 // Controls whether protocol version should be included while serializing the | 313 // Controls whether protocol version should be included while serializing the |
280 // packet. | 314 // packet. |
(...skipping 21 matching lines...) Expand all Loading... | |
302 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_; | 336 scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_; |
303 // If true, the packet will be padded up to |max_packet_length_|. | 337 // If true, the packet will be padded up to |max_packet_length_|. |
304 bool needs_padding_; | 338 bool needs_padding_; |
305 | 339 |
306 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); | 340 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); |
307 }; | 341 }; |
308 | 342 |
309 } // namespace net | 343 } // namespace net |
310 | 344 |
311 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ | 345 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ |
OLD | NEW |