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 #include "net/tools/quic/quic_dispatcher.h" | 5 #include "net/tools/quic/quic_dispatcher.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/debug/stack_trace.h" | 9 #include "base/debug/stack_trace.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | |
12 #include "net/quic/quic_blocked_writer_interface.h" | 11 #include "net/quic/quic_blocked_writer_interface.h" |
13 #include "net/quic/quic_flags.h" | 12 #include "net/quic/quic_flags.h" |
14 #include "net/quic/quic_utils.h" | 13 #include "net/quic/quic_utils.h" |
15 #include "net/tools/epoll_server/epoll_server.h" | 14 #include "net/tools/epoll_server/epoll_server.h" |
16 #include "net/tools/quic/quic_default_packet_writer.h" | 15 #include "net/tools/quic/quic_default_packet_writer.h" |
17 #include "net/tools/quic/quic_epoll_connection_helper.h" | 16 #include "net/tools/quic/quic_epoll_connection_helper.h" |
18 #include "net/tools/quic/quic_per_connection_packet_writer.h" | 17 #include "net/tools/quic/quic_per_connection_packet_writer.h" |
19 #include "net/tools/quic/quic_socket_utils.h" | 18 #include "net/tools/quic/quic_socket_utils.h" |
20 #include "net/tools/quic/quic_time_wait_list_manager.h" | 19 #include "net/tools/quic/quic_time_wait_list_manager.h" |
21 | 20 |
22 namespace net { | 21 namespace net { |
23 | 22 |
24 namespace tools { | 23 namespace tools { |
25 | 24 |
26 using base::StringPiece; | 25 using base::StringPiece; |
27 | 26 |
28 class DeleteSessionsAlarm : public EpollAlarm { | 27 namespace { |
| 28 |
| 29 // An alarm that informs the QuicDispatcher to delete old sessions. |
| 30 class DeleteSessionsAlarm : public QuicAlarm::Delegate { |
29 public: | 31 public: |
30 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) | 32 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) |
31 : dispatcher_(dispatcher) { | 33 : dispatcher_(dispatcher) { |
32 } | 34 } |
33 | 35 |
34 int64 OnAlarm() override { | 36 QuicTime OnAlarm() override { |
35 EpollAlarm::OnAlarm(); | |
36 dispatcher_->DeleteSessions(); | 37 dispatcher_->DeleteSessions(); |
37 return 0; | 38 // Let the dispatcher register the alarm at appropriate time. |
| 39 return QuicTime::Zero(); |
38 } | 40 } |
39 | 41 |
40 private: | 42 private: |
| 43 // Not owned. |
41 QuicDispatcher* dispatcher_; | 44 QuicDispatcher* dispatcher_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(DeleteSessionsAlarm); |
42 }; | 47 }; |
43 | 48 |
| 49 } // namespace |
| 50 |
44 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface { | 51 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface { |
45 public: | 52 public: |
46 explicit QuicFramerVisitor(QuicDispatcher* dispatcher) | 53 explicit QuicFramerVisitor(QuicDispatcher* dispatcher) |
47 : dispatcher_(dispatcher), | 54 : dispatcher_(dispatcher), |
48 connection_id_(0) {} | 55 connection_id_(0) {} |
49 | 56 |
50 // QuicFramerVisitorInterface implementation | 57 // QuicFramerVisitorInterface implementation |
51 void OnPacket() override {} | 58 void OnPacket() override {} |
52 bool OnUnauthenticatedPublicHeader( | 59 bool OnUnauthenticatedPublicHeader( |
53 const QuicPacketPublicHeader& header) override { | 60 const QuicPacketPublicHeader& header) override { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 QuicConnection* connection) const { | 164 QuicConnection* connection) const { |
158 return dispatcher_->packet_writer_factory_->Create( | 165 return dispatcher_->packet_writer_factory_->Create( |
159 dispatcher_->writer_.get(), | 166 dispatcher_->writer_.get(), |
160 connection); | 167 connection); |
161 } | 168 } |
162 | 169 |
163 QuicDispatcher::QuicDispatcher(const QuicConfig& config, | 170 QuicDispatcher::QuicDispatcher(const QuicConfig& config, |
164 const QuicCryptoServerConfig& crypto_config, | 171 const QuicCryptoServerConfig& crypto_config, |
165 const QuicVersionVector& supported_versions, | 172 const QuicVersionVector& supported_versions, |
166 PacketWriterFactory* packet_writer_factory, | 173 PacketWriterFactory* packet_writer_factory, |
167 EpollServer* epoll_server) | 174 QuicConnectionHelperInterface* helper) |
168 : config_(config), | 175 : config_(config), |
169 crypto_config_(crypto_config), | 176 crypto_config_(crypto_config), |
170 delete_sessions_alarm_(new DeleteSessionsAlarm(this)), | 177 helper_(helper), |
171 epoll_server_(epoll_server), | 178 delete_sessions_alarm_( |
172 helper_(new QuicEpollConnectionHelper(epoll_server_)), | 179 helper_->CreateAlarm(new DeleteSessionsAlarm(this))), |
173 packet_writer_factory_(packet_writer_factory), | 180 packet_writer_factory_(packet_writer_factory), |
174 connection_writer_factory_(this), | 181 connection_writer_factory_(this), |
175 supported_versions_(supported_versions), | 182 supported_versions_(supported_versions), |
176 current_packet_(nullptr), | 183 current_packet_(nullptr), |
177 framer_(supported_versions, | 184 framer_(supported_versions, |
178 /*unused*/ QuicTime::Zero(), | 185 /*unused*/ QuicTime::Zero(), |
179 Perspective::IS_SERVER), | 186 Perspective::IS_SERVER), |
180 framer_visitor_(new QuicFramerVisitor(this)) { | 187 framer_visitor_(new QuicFramerVisitor(this)) { |
181 framer_.set_visitor(framer_visitor_.get()); | 188 framer_.set_visitor(framer_visitor_.get()); |
182 } | 189 } |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 LOG(DFATAL) << base::debug::StackTrace().ToString(); | 365 LOG(DFATAL) << base::debug::StackTrace().ToString(); |
359 return; | 366 return; |
360 } | 367 } |
361 | 368 |
362 DLOG_IF(INFO, error != QUIC_NO_ERROR) << "Closing connection (" | 369 DLOG_IF(INFO, error != QUIC_NO_ERROR) << "Closing connection (" |
363 << connection_id | 370 << connection_id |
364 << ") due to error: " | 371 << ") due to error: " |
365 << QuicUtils::ErrorToString(error); | 372 << QuicUtils::ErrorToString(error); |
366 | 373 |
367 if (closed_session_list_.empty()) { | 374 if (closed_session_list_.empty()) { |
368 epoll_server_->RegisterAlarmApproximateDelta( | 375 delete_sessions_alarm_->Cancel(); |
369 0, delete_sessions_alarm_.get()); | 376 delete_sessions_alarm_->Set(helper()->GetClock()->ApproximateNow()); |
370 } | 377 } |
371 closed_session_list_.push_back(it->second); | 378 closed_session_list_.push_back(it->second); |
372 CleanUpSession(it); | 379 CleanUpSession(it); |
373 } | 380 } |
374 | 381 |
375 void QuicDispatcher::OnWriteBlocked( | 382 void QuicDispatcher::OnWriteBlocked( |
376 QuicBlockedWriterInterface* blocked_writer) { | 383 QuicBlockedWriterInterface* blocked_writer) { |
377 if (!writer_->IsWriteBlocked()) { | 384 if (!writer_->IsWriteBlocked()) { |
378 LOG(DFATAL) << | 385 LOG(DFATAL) << |
379 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked."; | 386 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked."; |
(...skipping 28 matching lines...) Expand all Loading... |
408 /* owns_writer= */ true, Perspective::IS_SERVER, | 415 /* owns_writer= */ true, Perspective::IS_SERVER, |
409 crypto_config_.HasProofSource(), supported_versions_); | 416 crypto_config_.HasProofSource(), supported_versions_); |
410 | 417 |
411 QuicServerSession* session = new QuicServerSession(config_, connection, this); | 418 QuicServerSession* session = new QuicServerSession(config_, connection, this); |
412 session->InitializeSession(&crypto_config_); | 419 session->InitializeSession(&crypto_config_); |
413 return session; | 420 return session; |
414 } | 421 } |
415 | 422 |
416 QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() { | 423 QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() { |
417 return new QuicTimeWaitListManager( | 424 return new QuicTimeWaitListManager( |
418 writer_.get(), this, epoll_server(), supported_versions()); | 425 writer_.get(), this, helper_.get(), supported_versions()); |
419 } | 426 } |
420 | 427 |
421 bool QuicDispatcher::HandlePacketForTimeWait( | 428 bool QuicDispatcher::HandlePacketForTimeWait( |
422 const QuicPacketPublicHeader& header) { | 429 const QuicPacketPublicHeader& header) { |
423 if (header.reset_flag) { | 430 if (header.reset_flag) { |
424 // Public reset packets do not have sequence numbers, so ignore the packet. | 431 // Public reset packets do not have sequence numbers, so ignore the packet. |
425 return false; | 432 return false; |
426 } | 433 } |
427 | 434 |
428 // Switch the framer to the correct version, so that the sequence number can | 435 // Switch the framer to the correct version, so that the sequence number can |
429 // be parsed correctly. | 436 // be parsed correctly. |
430 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromConnectionId( | 437 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromConnectionId( |
431 header.connection_id)); | 438 header.connection_id)); |
432 | 439 |
433 // Continue parsing the packet to extract the sequence number. Then | 440 // Continue parsing the packet to extract the sequence number. Then |
434 // send it to the time wait manager in OnUnathenticatedHeader. | 441 // send it to the time wait manager in OnUnathenticatedHeader. |
435 return true; | 442 return true; |
436 } | 443 } |
437 | 444 |
438 } // namespace tools | 445 } // namespace tools |
439 } // namespace net | 446 } // namespace net |
OLD | NEW |