OLD | NEW |
| (Empty) |
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 // Responsible for generating packets on behalf of a QuicConnection. | |
6 // Packets are serialized just-in-time. Control frames are queued. | |
7 // Ack and Feedback frames will be requested from the Connection | |
8 // just-in-time. When a packet needs to be sent, the Generator | |
9 // will serialize a packet and pass it to QuicConnection::SendOrQueuePacket() | |
10 // | |
11 // The Generator's mode of operation is controlled by two conditions: | |
12 // | |
13 // 1) Is the Delegate writable? | |
14 // | |
15 // If the Delegate is not writable, then no operations will cause | |
16 // a packet to be serialized. In particular: | |
17 // * SetShouldSendAck will simply record that an ack is to be sent. | |
18 // * AddControlFrame will enqueue the control frame. | |
19 // * ConsumeData will do nothing. | |
20 // | |
21 // If the Delegate is writable, then the behavior depends on the second | |
22 // condition: | |
23 // | |
24 // 2) Is the Generator in batch mode? | |
25 // | |
26 // If the Generator is NOT in batch mode, then each call to a write | |
27 // operation will serialize one or more packets. The contents will | |
28 // include any previous queued frames. If an ack should be sent | |
29 // but has not been sent, then the Delegate will be asked to create | |
30 // an Ack frame which will then be included in the packet. When | |
31 // the write call completes, the current packet will be serialized | |
32 // and sent to the Delegate, even if it is not full. | |
33 // | |
34 // If the Generator is in batch mode, then each write operation will | |
35 // add data to the "current" packet. When the current packet becomes | |
36 // full, it will be serialized and sent to the packet. When batch | |
37 // mode is ended via |FinishBatchOperations|, the current packet | |
38 // will be serialzied, even if it is not full. | |
39 | |
40 #ifndef NET_QUIC_QUIC_PACKET_GENERATOR_H_ | |
41 #define NET_QUIC_QUIC_PACKET_GENERATOR_H_ | |
42 | |
43 #include <stddef.h> | |
44 #include <stdint.h> | |
45 | |
46 #include <list> | |
47 | |
48 #include "base/macros.h" | |
49 #include "net/quic/quic_packet_creator.h" | |
50 #include "net/quic/quic_sent_packet_manager.h" | |
51 #include "net/quic/quic_types.h" | |
52 | |
53 namespace net { | |
54 | |
55 namespace test { | |
56 class QuicPacketGeneratorPeer; | |
57 } // namespace test | |
58 | |
59 class NET_EXPORT_PRIVATE QuicPacketGenerator { | |
60 public: | |
61 class NET_EXPORT_PRIVATE DelegateInterface | |
62 : public QuicPacketCreator::DelegateInterface { | |
63 public: | |
64 ~DelegateInterface() override {} | |
65 // Consults delegate whether a packet should be generated. | |
66 virtual bool ShouldGeneratePacket(HasRetransmittableData retransmittable, | |
67 IsHandshake handshake) = 0; | |
68 virtual const QuicFrame GetUpdatedAckFrame() = 0; | |
69 virtual void PopulateStopWaitingFrame( | |
70 QuicStopWaitingFrame* stop_waiting) = 0; | |
71 }; | |
72 | |
73 QuicPacketGenerator(QuicConnectionId connection_id, | |
74 QuicFramer* framer, | |
75 QuicRandom* random_generator, | |
76 QuicBufferAllocator* buffer_allocator, | |
77 DelegateInterface* delegate); | |
78 | |
79 ~QuicPacketGenerator(); | |
80 | |
81 // Indicates that an ACK frame should be sent. | |
82 // If |also_send_stop_waiting| is true, then it also indicates that a | |
83 // STOP_WAITING frame should be sent as well. | |
84 // The contents of the frame(s) will be generated via a call to the delegate | |
85 // CreateAckFrame() when the packet is serialized. | |
86 void SetShouldSendAck(bool also_send_stop_waiting); | |
87 | |
88 void AddControlFrame(const QuicFrame& frame); | |
89 | |
90 // Given some data, may consume part or all of it and pass it to the | |
91 // packet creator to be serialized into packets. If not in batch | |
92 // mode, these packets will also be sent during this call. | |
93 // |delegate| (if not nullptr) will be informed once all packets sent as a | |
94 // result of this call are ACKed by the peer. | |
95 QuicConsumedData ConsumeData(QuicStreamId id, | |
96 QuicIOVector iov, | |
97 QuicStreamOffset offset, | |
98 bool fin, | |
99 QuicAckListenerInterface* listener); | |
100 | |
101 // Sends as many data only packets as allowed by the send algorithm and the | |
102 // available iov. | |
103 // This path does not support FEC, padding, or bundling pending frames. | |
104 QuicConsumedData ConsumeDataFastPath(QuicStreamId id, | |
105 const QuicIOVector& iov, | |
106 QuicStreamOffset offset, | |
107 bool fin, | |
108 QuicAckListenerInterface* listener); | |
109 | |
110 // Generates an MTU discovery packet of specified size. | |
111 void GenerateMtuDiscoveryPacket(QuicByteCount target_mtu, | |
112 QuicAckListenerInterface* listener); | |
113 | |
114 // Indicates whether batch mode is currently enabled. | |
115 bool InBatchMode(); | |
116 // Disables flushing. | |
117 void StartBatchOperations(); | |
118 // Enables flushing and flushes queued data which can be sent. | |
119 void FinishBatchOperations(); | |
120 | |
121 // Flushes all queued frames, even frames which are not sendable. | |
122 void FlushAllQueuedFrames(); | |
123 | |
124 bool HasQueuedFrames() const; | |
125 | |
126 // Whether the pending packet has no frames in it at the moment. | |
127 bool IsPendingPacketEmpty() const; | |
128 | |
129 // Makes the framer not serialize the protocol version in sent packets. | |
130 void StopSendingVersion(); | |
131 | |
132 // SetDiversificationNonce sets the nonce that will be sent in each public | |
133 // header of packets encrypted at the initial encryption level. Should only | |
134 // be called by servers. | |
135 void SetDiversificationNonce(const DiversificationNonce nonce); | |
136 | |
137 // Creates a version negotiation packet which supports |supported_versions|. | |
138 // Caller owns the created packet. Also, sets the entropy hash of the | |
139 // serialized packet to a random bool and returns that value as a member of | |
140 // SerializedPacket. | |
141 QuicEncryptedPacket* SerializeVersionNegotiationPacket( | |
142 const QuicVersionVector& supported_versions); | |
143 | |
144 // Re-serializes frames with the original packet's packet number length. | |
145 // Used for retransmitting packets to ensure they aren't too long. | |
146 void ReserializeAllFrames(const PendingRetransmission& retransmission, | |
147 char* buffer, | |
148 size_t buffer_len); | |
149 | |
150 // Update the packet number length to use in future packets as soon as it | |
151 // can be safely changed. | |
152 void UpdateSequenceNumberLength(QuicPacketNumber least_packet_awaited_by_peer, | |
153 QuicPacketCount max_packets_in_flight); | |
154 | |
155 // Set the minimum number of bytes for the connection id length; | |
156 void SetConnectionIdLength(uint32_t length); | |
157 | |
158 // Sets the encrypter to use for the encryption level. | |
159 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); | |
160 | |
161 // Sets the encryption level that will be applied to new packets. | |
162 void set_encryption_level(EncryptionLevel level); | |
163 | |
164 // packet number of the last created packet, or 0 if no packets have been | |
165 // created. | |
166 QuicPacketNumber packet_number() const; | |
167 | |
168 // Returns the maximum length a current packet can actually have. | |
169 QuicByteCount GetCurrentMaxPacketLength() const; | |
170 | |
171 // Set maximum packet length in the creator immediately. May not be called | |
172 // when there are frames queued in the creator. | |
173 void SetMaxPacketLength(QuicByteCount length); | |
174 | |
175 // Sets |path_id| to be the path on which next packet is generated. | |
176 void SetCurrentPath(QuicPathId path_id, | |
177 QuicPacketNumber least_packet_awaited_by_peer, | |
178 QuicPacketCount max_packets_in_flight); | |
179 | |
180 void set_debug_delegate(QuicPacketCreator::DebugDelegate* debug_delegate) { | |
181 packet_creator_.set_debug_delegate(debug_delegate); | |
182 } | |
183 | |
184 const QuicAckFrame& pending_ack_frame() const { return pending_ack_frame_; } | |
185 | |
186 private: | |
187 friend class test::QuicPacketGeneratorPeer; | |
188 | |
189 void SendQueuedFrames(bool flush); | |
190 | |
191 // Test to see if we have pending ack, or control frames. | |
192 bool HasPendingFrames() const; | |
193 // Returns true if addition of a pending frame (which might be | |
194 // retransmittable) would still allow the resulting packet to be sent now. | |
195 bool CanSendWithNextPendingFrameAddition() const; | |
196 // Add exactly one pending frame, preferring ack frames over control frames. | |
197 // Returns true if a pending frame is successfully added. | |
198 // Returns false and flushes current open packet if the pending frame cannot | |
199 // fit into current open packet. | |
200 bool AddNextPendingFrame(); | |
201 | |
202 DelegateInterface* delegate_; | |
203 | |
204 QuicPacketCreator packet_creator_; | |
205 QuicFrames queued_control_frames_; | |
206 | |
207 // True if batch mode is currently enabled. | |
208 bool batch_mode_; | |
209 | |
210 // Flags to indicate the need for just-in-time construction of a frame. | |
211 bool should_send_ack_; | |
212 bool should_send_stop_waiting_; | |
213 // If we put a non-retransmittable frame in this packet, then we have to hold | |
214 // a reference to it until we flush (and serialize it). Retransmittable frames | |
215 // are referenced elsewhere so that they can later be (optionally) | |
216 // retransmitted. | |
217 QuicAckFrame pending_ack_frame_; | |
218 QuicStopWaitingFrame pending_stop_waiting_frame_; | |
219 | |
220 DISALLOW_COPY_AND_ASSIGN(QuicPacketGenerator); | |
221 }; | |
222 | |
223 } // namespace net | |
224 | |
225 #endif // NET_QUIC_QUIC_PACKET_GENERATOR_H_ | |
OLD | NEW |