1 // Copyright 2013 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 #include "net/quic/quic_ack_notifier_manager.h" 6 7 #include <stddef.h> 8 #include <list> 9 #include <map> 10 #include <utility> 11 #include <vector> 12 13 #include "base/stl_util.h" 14 #include "net/quic/quic_ack_notifier.h" 15 #include "net/quic/quic_protocol.h" 16 17 namespace net { 18 19 AckNotifierManager::AckNotifierManager() {} 20 21 AckNotifierManager::~AckNotifierManager() { 22 STLDeleteElements(&ack_notifiers_); 23 } 24 25 void AckNotifierManager::OnPacketAcked( 26 QuicPacketSequenceNumber sequence_number) { 27 // Inform all the registered AckNotifiers of the new ACK. 28 AckNotifierMap::iterator map_it = ack_notifier_map_.find(sequence_number); 29 if (map_it == ack_notifier_map_.end()) { 30 // No AckNotifier is interested in this sequence number. 31 return; 32 } 33 34 // One or more AckNotifiers are registered as interested in this sequence 35 // number. Iterate through them and call OnAck on each. 36 for (AckNotifierSet::iterator set_it = map_it->second.begin(); 37 set_it != map_it->second.end(); ++set_it) { 38 QuicAckNotifier* ack_notifier = *set_it; 39 ack_notifier->OnAck(sequence_number); 40 41 // If this has resulted in an empty AckNotifer, erase it. 42 if (ack_notifier->IsEmpty()) { 43 delete ack_notifier; 44 ack_notifiers_.erase(ack_notifier); 45 } 46 } 47 48 // Remove the sequence number from the map as we have notified all the 49 // registered AckNotifiers, and we won't see it again. 50 ack_notifier_map_.erase(map_it); 51 } 52 53 void AckNotifierManager::UpdateSequenceNumber( 54 QuicPacketSequenceNumber old_sequence_number, 55 QuicPacketSequenceNumber new_sequence_number) { 56 AckNotifierMap::iterator map_it = ack_notifier_map_.find(old_sequence_number); 57 if (map_it != ack_notifier_map_.end()) { 58 // We will add an entry to the map for the new sequence number, and move 59 // the 60 // list of AckNotifiers over. 61 AckNotifierSet new_set; 62 for (AckNotifierSet::iterator notifier_it = map_it->second.begin(); 63 notifier_it != map_it->second.end(); ++notifier_it) { 64 (*notifier_it) 65 ->UpdateSequenceNumber(old_sequence_number, new_sequence_number); 66 new_set.insert(*notifier_it); 67 } 68 ack_notifier_map_[new_sequence_number] = new_set; 69 ack_notifier_map_.erase(map_it); 70 } 71 } 72 73 void AckNotifierManager::OnSerializedPacket( 74 const SerializedPacket& serialized_packet) { 75 // Run through all the frames and if any of them are stream frames and have 76 // an AckNotifier registered, then inform the AckNotifier that it should be 77 // interested in this packet's sequence number. 78 79 RetransmittableFrames* frames = serialized_packet.retransmittable_frames; 80 81 // AckNotifiers can only be attached to retransmittable frames. 82 if (!frames) { 83 return; 84 } 85 86 for (QuicFrames::const_iterator it = frames->frames().begin(); 87 it != frames->frames().end(); ++it) { 88 if (it->type == STREAM_FRAME && it->stream_frame->notifier != NULL) { 89 QuicAckNotifier* notifier = it->stream_frame->notifier; 90 91 // The AckNotifier needs to know it is tracking this packet's sequence 92 // number. 93 notifier->AddSequenceNumber(serialized_packet.sequence_number); 94 95 // Update the mapping in the other direction, from sequence 96 // number to AckNotifier. 97 ack_notifier_map_[serialized_packet.sequence_number].insert(notifier); 98 99 // Take ownership of the AckNotifier. 100 ack_notifiers_.insert(notifier); 101 } 102 } 103 } 104 105 } // namespace net 106