| 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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "net/quic/quic_blocked_writer_interface.h" | 11 #include "net/quic/quic_blocked_writer_interface.h" |
| 12 #include "net/quic/quic_utils.h" | 12 #include "net/quic/quic_utils.h" |
| 13 #include "net/tools/quic/quic_default_packet_writer.h" | 13 #include "net/tools/quic/quic_default_packet_writer.h" |
| 14 #include "net/tools/quic/quic_epoll_connection_helper.h" | 14 #include "net/tools/quic/quic_epoll_connection_helper.h" |
| 15 #include "net/tools/quic/quic_packet_writer_wrapper.h" |
| 15 #include "net/tools/quic/quic_socket_utils.h" | 16 #include "net/tools/quic/quic_socket_utils.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 19 |
| 18 namespace tools { | 20 namespace tools { |
| 19 | 21 |
| 20 using base::StringPiece; | 22 using base::StringPiece; |
| 21 using std::make_pair; | 23 using std::make_pair; |
| 22 | 24 |
| 23 class DeleteSessionsAlarm : public EpollAlarm { | 25 class DeleteSessionsAlarm : public EpollAlarm { |
| 24 public: | 26 public: |
| 25 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) | 27 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher) |
| 26 : dispatcher_(dispatcher) { | 28 : dispatcher_(dispatcher) { |
| 27 } | 29 } |
| 28 | 30 |
| 29 virtual int64 OnAlarm() OVERRIDE { | 31 virtual int64 OnAlarm() OVERRIDE { |
| 30 EpollAlarm::OnAlarm(); | 32 EpollAlarm::OnAlarm(); |
| 31 dispatcher_->DeleteSessions(); | 33 dispatcher_->DeleteSessions(); |
| 32 return 0; | 34 return 0; |
| 33 } | 35 } |
| 34 | 36 |
| 35 private: | 37 private: |
| 36 QuicDispatcher* dispatcher_; | 38 QuicDispatcher* dispatcher_; |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface { | 41 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface { |
| 40 public: | 42 public: |
| 41 explicit QuicFramerVisitor(QuicDispatcher* dispatcher) | 43 explicit QuicFramerVisitor(QuicDispatcher* dispatcher) |
| 42 : dispatcher_(dispatcher) { | 44 : dispatcher_(dispatcher) {} |
| 43 } | |
| 44 | 45 |
| 45 // QuicFramerVisitorInterface implementation | 46 // QuicFramerVisitorInterface implementation |
| 46 virtual void OnPacket() OVERRIDE {} | 47 virtual void OnPacket() OVERRIDE {} |
| 47 virtual bool OnUnauthenticatedPublicHeader( | 48 virtual bool OnUnauthenticatedPublicHeader( |
| 48 const QuicPacketPublicHeader& header) OVERRIDE { | 49 const QuicPacketPublicHeader& header) OVERRIDE { |
| 49 return dispatcher_->OnUnauthenticatedPublicHeader(header); | 50 return dispatcher_->OnUnauthenticatedPublicHeader(header); |
| 50 } | 51 } |
| 51 virtual bool OnUnauthenticatedHeader( | 52 virtual bool OnUnauthenticatedHeader( |
| 52 const QuicPacketHeader& header) OVERRIDE { | 53 const QuicPacketHeader& header) OVERRIDE { |
| 53 dispatcher_->OnUnauthenticatedHeader(header); | 54 dispatcher_->OnUnauthenticatedHeader(header); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 DCHECK(false); | 117 DCHECK(false); |
| 117 } | 118 } |
| 118 | 119 |
| 119 private: | 120 private: |
| 120 QuicDispatcher* dispatcher_; | 121 QuicDispatcher* dispatcher_; |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 QuicDispatcher::QuicDispatcher(const QuicConfig& config, | 124 QuicDispatcher::QuicDispatcher(const QuicConfig& config, |
| 124 const QuicCryptoServerConfig& crypto_config, | 125 const QuicCryptoServerConfig& crypto_config, |
| 125 const QuicVersionVector& supported_versions, | 126 const QuicVersionVector& supported_versions, |
| 126 int fd, | |
| 127 EpollServer* epoll_server) | 127 EpollServer* epoll_server) |
| 128 : config_(config), | 128 : config_(config), |
| 129 crypto_config_(crypto_config), | 129 crypto_config_(crypto_config), |
| 130 time_wait_list_manager_( | |
| 131 new QuicTimeWaitListManager(this, epoll_server, supported_versions)), | |
| 132 delete_sessions_alarm_(new DeleteSessionsAlarm(this)), | 130 delete_sessions_alarm_(new DeleteSessionsAlarm(this)), |
| 133 epoll_server_(epoll_server), | 131 epoll_server_(epoll_server), |
| 134 fd_(fd), | |
| 135 helper_(new QuicEpollConnectionHelper(epoll_server_)), | 132 helper_(new QuicEpollConnectionHelper(epoll_server_)), |
| 136 writer_(new QuicDefaultPacketWriter(fd)), | |
| 137 supported_versions_(supported_versions), | 133 supported_versions_(supported_versions), |
| 138 current_packet_(NULL), | 134 current_packet_(NULL), |
| 139 framer_(supported_versions, /*unused*/ QuicTime::Zero(), true), | 135 framer_(supported_versions, /*unused*/ QuicTime::Zero(), true), |
| 140 framer_visitor_(new QuicFramerVisitor(this)) { | 136 framer_visitor_(new QuicFramerVisitor(this)) { |
| 141 framer_.set_visitor(framer_visitor_.get()); | 137 framer_.set_visitor(framer_visitor_.get()); |
| 142 } | 138 } |
| 143 | 139 |
| 144 QuicDispatcher::~QuicDispatcher() { | 140 QuicDispatcher::~QuicDispatcher() { |
| 145 STLDeleteValues(&session_map_); | 141 STLDeleteValues(&session_map_); |
| 146 STLDeleteElements(&closed_session_list_); | 142 STLDeleteElements(&closed_session_list_); |
| 147 } | 143 } |
| 148 | 144 |
| 149 void QuicDispatcher::set_fd(int fd) { | 145 void QuicDispatcher::Initialize(int fd) { |
| 150 fd_ = fd; | 146 DCHECK(writer_ == NULL); |
| 151 writer_.reset(new QuicDefaultPacketWriter(fd)); | 147 writer_.reset(CreateWriterWrapper(CreateWriter(fd))); |
| 148 time_wait_list_manager_.reset( |
| 149 new QuicTimeWaitListManager(writer_.get(), this, |
| 150 epoll_server(), supported_versions())); |
| 152 } | 151 } |
| 153 | 152 |
| 154 // TODO(fnk): remove the Writer interface implementation in favor of | 153 // TODO(fnk): remove the Writer interface implementation in favor of |
| 155 // direct requests for blocked list placement from Connection/Session. | 154 // direct requests for blocked list placement from Connection/Session. |
| 156 WriteResult QuicDispatcher::WritePacket(const char* buffer, size_t buf_len, | 155 WriteResult QuicDispatcher::WritePacket(const char* buffer, size_t buf_len, |
| 157 const IPAddressNumber& self_address, | 156 const IPAddressNumber& self_address, |
| 158 const IPEndPoint& peer_address, | 157 const IPEndPoint& peer_address, |
| 159 QuicBlockedWriterInterface* writer) { | 158 QuicBlockedWriterInterface* writer) { |
| 160 if (IsWriteBlocked()) { | 159 if (IsWriteBlocked()) { |
| 161 write_blocked_list_.insert(make_pair(writer, true)); | 160 write_blocked_list_.insert(make_pair(writer, true)); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 << QuicUtils::ErrorToString(error); | 324 << QuicUtils::ErrorToString(error); |
| 326 | 325 |
| 327 if (closed_session_list_.empty()) { | 326 if (closed_session_list_.empty()) { |
| 328 epoll_server_->RegisterAlarmApproximateDelta( | 327 epoll_server_->RegisterAlarmApproximateDelta( |
| 329 0, delete_sessions_alarm_.get()); | 328 0, delete_sessions_alarm_.get()); |
| 330 } | 329 } |
| 331 closed_session_list_.push_back(it->second); | 330 closed_session_list_.push_back(it->second); |
| 332 CleanUpSession(it); | 331 CleanUpSession(it); |
| 333 } | 332 } |
| 334 | 333 |
| 334 void QuicDispatcher::OnWriteBlocked(QuicBlockedWriterInterface* writer) { |
| 335 DCHECK(writer_->IsWriteBlocked()); |
| 336 write_blocked_list_.insert(make_pair(writer, true)); |
| 337 } |
| 338 |
| 335 QuicSession* QuicDispatcher::CreateQuicSession( | 339 QuicSession* QuicDispatcher::CreateQuicSession( |
| 336 QuicGuid guid, | 340 QuicGuid guid, |
| 337 const IPEndPoint& server_address, | 341 const IPEndPoint& server_address, |
| 338 const IPEndPoint& client_address) { | 342 const IPEndPoint& client_address) { |
| 339 QuicServerSession* session = new QuicServerSession( | 343 QuicServerSession* session = new QuicServerSession( |
| 340 config_, new QuicConnection(guid, client_address, helper_.get(), this, | 344 config_, new QuicConnection(guid, client_address, helper_.get(), this, |
| 341 true, supported_versions_), this); | 345 true, supported_versions_), this); |
| 342 session->InitializeSession(crypto_config_); | 346 session->InitializeSession(crypto_config_); |
| 343 return session; | 347 return session; |
| 344 } | 348 } |
| 345 | 349 |
| 350 QuicPacketWriter* QuicDispatcher::CreateWriter(int fd) { |
| 351 return new QuicDefaultPacketWriter(fd); |
| 352 } |
| 353 |
| 354 QuicPacketWriterWrapper* QuicDispatcher::CreateWriterWrapper( |
| 355 QuicPacketWriter* writer) { |
| 356 return new QuicPacketWriterWrapper(writer); |
| 357 } |
| 358 |
| 359 void QuicDispatcher::set_writer(QuicPacketWriter* writer) { |
| 360 writer_->set_writer(writer); |
| 361 } |
| 362 |
| 346 bool QuicDispatcher::HandlePacketForTimeWait( | 363 bool QuicDispatcher::HandlePacketForTimeWait( |
| 347 const QuicPacketPublicHeader& header) { | 364 const QuicPacketPublicHeader& header) { |
| 348 if (header.reset_flag) { | 365 if (header.reset_flag) { |
| 349 // Public reset packets do not have sequence numbers, so ignore the packet. | 366 // Public reset packets do not have sequence numbers, so ignore the packet. |
| 350 return false; | 367 return false; |
| 351 } | 368 } |
| 352 | 369 |
| 353 // Switch the framer to the correct version, so that the sequence number can | 370 // Switch the framer to the correct version, so that the sequence number can |
| 354 // be parsed correctly. | 371 // be parsed correctly. |
| 355 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromGuid( | 372 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromGuid( |
| 356 header.guid)); | 373 header.guid)); |
| 357 | 374 |
| 358 // Continue parsing the packet to extract the sequence number. Then | 375 // Continue parsing the packet to extract the sequence number. Then |
| 359 // send it to the time wait manager in OnUnathenticatedHeader. | 376 // send it to the time wait manager in OnUnathenticatedHeader. |
| 360 return true; | 377 return true; |
| 361 } | 378 } |
| 362 | 379 |
| 363 } // namespace tools | 380 } // namespace tools |
| 364 } // namespace net | 381 } // namespace net |
| OLD | NEW |