QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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/third_party/quiche/src/quic/quartc/quartc_endpoint.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/quic_version_manager.h" |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 9 | #include "net/third_party/quiche/src/quic/quartc/quartc_connection_helper.h" |
| 10 | #include "net/third_party/quiche/src/quic/quartc/quartc_crypto_helpers.h" |
| 11 | #include "net/third_party/quiche/src/quic/quartc/quartc_dispatcher.h" |
| 12 | |
| 13 | namespace quic { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // Wrapper around a QuicAlarmFactory which delegates to the wrapped factory. |
| 18 | // Usee to convert an unowned pointer into an owned pointer, so that the new |
| 19 | // "owner" does not delete the underlying factory. Note that this is only valid |
| 20 | // when the unowned pointer is already guaranteed to outlive the new "owner". |
| 21 | class QuartcAlarmFactoryWrapper : public QuicAlarmFactory { |
| 22 | public: |
| 23 | explicit QuartcAlarmFactoryWrapper(QuicAlarmFactory* impl) : impl_(impl) {} |
| 24 | |
| 25 | QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override; |
| 26 | QuicArenaScopedPtr<QuicAlarm> CreateAlarm( |
| 27 | QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
| 28 | QuicConnectionArena* arena) override; |
| 29 | |
| 30 | private: |
| 31 | QuicAlarmFactory* impl_; |
| 32 | }; |
| 33 | |
| 34 | QuicAlarm* QuartcAlarmFactoryWrapper::CreateAlarm( |
| 35 | QuicAlarm::Delegate* delegate) { |
| 36 | return impl_->CreateAlarm(delegate); |
| 37 | } |
| 38 | |
| 39 | QuicArenaScopedPtr<QuicAlarm> QuartcAlarmFactoryWrapper::CreateAlarm( |
| 40 | QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
| 41 | QuicConnectionArena* arena) { |
| 42 | return impl_->CreateAlarm(std::move(delegate), arena); |
| 43 | } |
| 44 | |
| 45 | QuartcFactoryConfig CreateFactoryConfig(QuicAlarmFactory* alarm_factory, |
| 46 | const QuicClock* clock) { |
| 47 | QuartcFactoryConfig config; |
| 48 | config.alarm_factory = alarm_factory; |
| 49 | config.clock = clock; |
| 50 | return config; |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | QuartcClientEndpoint::QuartcClientEndpoint( |
| 56 | QuicAlarmFactory* alarm_factory, |
| 57 | const QuicClock* clock, |
| 58 | QuartcEndpoint::Delegate* delegate, |
| 59 | const QuartcSessionConfig& config, |
| 60 | QuicStringPiece serialized_server_config, |
| 61 | std::unique_ptr<QuicVersionManager> version_manager) |
| 62 | : alarm_factory_(alarm_factory), |
| 63 | clock_(clock), |
| 64 | delegate_(delegate), |
| 65 | serialized_server_config_(serialized_server_config), |
| 66 | version_manager_(version_manager ? std::move(version_manager) |
| 67 | : QuicMakeUnique<QuicVersionManager>( |
| 68 | AllSupportedVersions())), |
| 69 | create_session_alarm_(QuicWrapUnique( |
| 70 | alarm_factory_->CreateAlarm(new CreateSessionDelegate(this)))), |
| 71 | factory_(QuicMakeUnique<QuartcFactory>( |
| 72 | CreateFactoryConfig(alarm_factory, clock))), |
| 73 | config_(config) {} |
| 74 | |
| 75 | void QuartcClientEndpoint::Connect(QuartcPacketTransport* packet_transport) { |
| 76 | packet_transport_ = packet_transport; |
| 77 | create_session_alarm_->Set(clock_->Now()); |
| 78 | } |
| 79 | |
| 80 | void QuartcClientEndpoint::OnCreateSessionAlarm() { |
| 81 | session_ = factory_->CreateQuartcClientSession( |
| 82 | config_, version_manager_->GetSupportedVersions(), |
| 83 | serialized_server_config_, packet_transport_); |
| 84 | delegate_->OnSessionCreated(session_.get()); |
| 85 | } |
| 86 | |
| 87 | QuartcServerEndpoint::QuartcServerEndpoint( |
| 88 | QuicAlarmFactory* alarm_factory, |
| 89 | const QuicClock* clock, |
| 90 | QuartcEndpoint::Delegate* delegate, |
| 91 | const QuartcSessionConfig& config, |
| 92 | std::unique_ptr<QuicVersionManager> version_manager) |
| 93 | : alarm_factory_(alarm_factory), |
| 94 | delegate_(delegate), |
| 95 | config_(config), |
| 96 | version_manager_(version_manager ? std::move(version_manager) |
| 97 | : QuicMakeUnique<QuicVersionManager>( |
| 98 | AllSupportedVersions())), |
| 99 | pre_connection_helper_(QuicMakeUnique<QuartcConnectionHelper>(clock)), |
| 100 | crypto_config_( |
| 101 | CreateCryptoServerConfig(pre_connection_helper_->GetRandomGenerator(), |
| 102 | clock, |
| 103 | config.pre_shared_key)) {} |
| 104 | |
| 105 | void QuartcServerEndpoint::Connect(QuartcPacketTransport* packet_transport) { |
| 106 | DCHECK(pre_connection_helper_ != nullptr); |
| 107 | dispatcher_ = QuicMakeUnique<QuartcDispatcher>( |
| 108 | QuicMakeUnique<QuicConfig>(CreateQuicConfig(config_)), |
| 109 | std::move(crypto_config_.config), crypto_config_.serialized_crypto_config, |
| 110 | version_manager_.get(), std::move(pre_connection_helper_), |
| 111 | QuicMakeUnique<QuartcCryptoServerStreamHelper>(), |
| 112 | QuicMakeUnique<QuartcAlarmFactoryWrapper>(alarm_factory_), |
| 113 | QuicMakeUnique<QuartcPacketWriter>(packet_transport, |
| 114 | config_.max_packet_size), |
| 115 | this); |
| 116 | // The dispatcher requires at least one call to |ProcessBufferedChlos| to |
| 117 | // set the number of connections it is allowed to create. |
| 118 | dispatcher_->ProcessBufferedChlos(/*max_connections_to_create=*/1); |
| 119 | } |
| 120 | |
| 121 | void QuartcServerEndpoint::OnSessionCreated(QuartcSession* session) { |
| 122 | delegate_->OnSessionCreated(session); |
| 123 | } |
| 124 | |
| 125 | } // namespace quic |