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