| OLD | NEW |
| (Empty) | |
| 1 #include "net/quic/quic_potential_connection_store.h" |
| 2 |
| 3 #include <list> |
| 4 |
| 5 #include "base/stl_util.h" |
| 6 |
| 7 namespace net { |
| 8 |
| 9 typedef QuicPotentialConnectionStore::BufferedPacket BufferedPacket; |
| 10 typedef QuicPotentialConnectionStore::EnqueuePacketResult EnqueuePacketResult; |
| 11 |
| 12 // Max number of connections this store can keep track. |
| 13 static const size_t kDefaultMaxConnectionsInStore = 100; |
| 14 |
| 15 namespace { |
| 16 |
| 17 // This alarm removes expired entries in map each time this alarm fires. |
| 18 class NET_EXPORT_PRIVATE ConnectionExpireAlarm : public QuicAlarm::Delegate { |
| 19 public: |
| 20 explicit ConnectionExpireAlarm(QuicPotentialConnectionStore* store) |
| 21 : connection_store_(store) {} |
| 22 |
| 23 void OnAlarm() override { connection_store_->OnExpirationTimeout(); } |
| 24 |
| 25 // Disallow copy and asign. |
| 26 ConnectionExpireAlarm(const ConnectionExpireAlarm&) = delete; |
| 27 ConnectionExpireAlarm& operator=(const ConnectionExpireAlarm&) = delete; |
| 28 |
| 29 private: |
| 30 QuicPotentialConnectionStore* connection_store_; |
| 31 }; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 QuicPotentialConnectionStore::BufferedPacket::BufferedPacket( |
| 36 std::unique_ptr<QuicEncryptedPacket> packet, |
| 37 IPEndPoint server_address, |
| 38 IPEndPoint client_address) |
| 39 : packet(std::move(packet)), |
| 40 server_address(server_address), |
| 41 client_address(client_address) {} |
| 42 |
| 43 QuicPotentialConnectionStore::BufferedPacket::BufferedPacket( |
| 44 BufferedPacket&& other) { |
| 45 packet = std::move(other.packet); |
| 46 server_address = other.server_address; |
| 47 client_address = other.client_address; |
| 48 } |
| 49 |
| 50 QuicPotentialConnectionStore::BufferedPacket::~BufferedPacket() {} |
| 51 |
| 52 QuicPotentialConnectionStore::BufferedPacketList::BufferedPacketList() |
| 53 : creation_time(QuicTime::Zero()) {} |
| 54 |
| 55 QuicPotentialConnectionStore::BufferedPacketList::~BufferedPacketList() {} |
| 56 |
| 57 QuicPotentialConnectionStore::QuicPotentialConnectionStore( |
| 58 VisitorInterface* visitor, |
| 59 QuicClock* clock, |
| 60 QuicAlarmFactory* alarm_factory) |
| 61 : connection_life_span_( |
| 62 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs)), |
| 63 visitor_(visitor), |
| 64 clock_(clock), |
| 65 expiration_alarm_( |
| 66 alarm_factory->CreateAlarm(new ConnectionExpireAlarm(this))) {} |
| 67 |
| 68 QuicPotentialConnectionStore::~QuicPotentialConnectionStore() {} |
| 69 |
| 70 EnqueuePacketResult QuicPotentialConnectionStore::EnqueuePacket( |
| 71 QuicConnectionId connection_id, |
| 72 const QuicEncryptedPacket& packet, |
| 73 IPEndPoint server_address, |
| 74 IPEndPoint client_address) { |
| 75 if (!ContainsKey(undecryptable_packets_, connection_id) && |
| 76 undecryptable_packets_.size() >= kDefaultMaxConnectionsInStore) { |
| 77 // Drop the packet if store can't keep track of more connections. |
| 78 return TOO_MANY_CONNECTIONS; |
| 79 } |
| 80 BufferedPacketList& queue = |
| 81 undecryptable_packets_.find(connection_id)->second; |
| 82 |
| 83 if (queue.buffered_packets.size() >= kDefaultMaxUndecryptablePackets) { |
| 84 // If there are kMaxBufferedPacketsPerConnection packets buffered up for |
| 85 // this connection, drop the current packet. |
| 86 return TOO_MANY_PACKETS; |
| 87 } |
| 88 |
| 89 if (queue.buffered_packets.empty()) { |
| 90 // If this is the first packet arrived on a new connection, initialize the |
| 91 // creation time. |
| 92 queue.creation_time = clock_->ApproximateNow(); |
| 93 } |
| 94 |
| 95 BufferedPacket new_entry(std::unique_ptr<QuicEncryptedPacket>(packet.Clone()), |
| 96 server_address, client_address); |
| 97 |
| 98 queue.buffered_packets.push_back(std::move(new_entry)); |
| 99 |
| 100 if (!expiration_alarm_->IsSet()) { |
| 101 expiration_alarm_->Set(clock_->ApproximateNow().Add(connection_life_span_)); |
| 102 } |
| 103 return SUCCESS; |
| 104 } |
| 105 |
| 106 std::list<BufferedPacket> QuicPotentialConnectionStore::DeliverPackets( |
| 107 QuicConnectionId connection_id) { |
| 108 std::list<BufferedPacket> packets_to_deliver = std::move( |
| 109 undecryptable_packets_.find(connection_id)->second.buffered_packets); |
| 110 undecryptable_packets_.erase(connection_id); |
| 111 return packets_to_deliver; |
| 112 } |
| 113 |
| 114 void QuicPotentialConnectionStore::OnExpirationTimeout() { |
| 115 QuicTime expiration_time = |
| 116 clock_->ApproximateNow().Subtract(connection_life_span_); |
| 117 while (!undecryptable_packets_.empty()) { |
| 118 auto& entry = undecryptable_packets_.front(); |
| 119 if (entry.second.creation_time > expiration_time) { |
| 120 break; |
| 121 } |
| 122 visitor_->OnExpiredPackets(entry.first, std::move(entry.second)); |
| 123 undecryptable_packets_.erase(undecryptable_packets_.begin()); |
| 124 } |
| 125 if (!undecryptable_packets_.empty()) { |
| 126 expiration_alarm_->Set(clock_->ApproximateNow().Add(connection_life_span_)); |
| 127 } |
| 128 } |
| 129 |
| 130 } // namespace net |
| OLD | NEW |