Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/quic/quartc/quartc_factory.h" | |
| 6 | |
| 7 #include "net/quic/core/crypto/quic_random.h" | |
| 8 #include "net/quic/quartc/quartc_alarm_factory.h" | |
| 9 #include "net/quic/quartc/quartc_session.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // Implements the QuicAlarm with QuartcTaskRunnerInterface for the Quartc | |
| 14 // users other than Chromium. For example, WebRTC will create QuartcAlarm with | |
| 15 // a QuartcTaskRunner implemented by WebRTC. | |
| 16 class QuartcAlarm : public net::QuicAlarm, | |
| 17 public net::QuartcTaskRunnerInterface::Task { | |
| 18 public: | |
| 19 QuartcAlarm(const net::QuicClock* clock, | |
| 20 net::QuartcTaskRunnerInterface* task_runner, | |
| 21 net::QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) | |
| 22 : net::QuicAlarm(std::move(delegate)), | |
| 23 clock_(clock), | |
| 24 task_runner_(task_runner) {} | |
| 25 | |
| 26 // QuicAlarm overrides. | |
| 27 void SetImpl() override { | |
| 28 DCHECK(deadline().IsInitialized()); | |
| 29 // Cancel it if already set. | |
| 30 CancelImpl(); | |
| 31 | |
| 32 int64_t delay_ms = (deadline() - (clock_->Now())).ToMilliseconds(); | |
| 33 if (delay_ms < 0) { | |
| 34 delay_ms = 0; | |
| 35 } | |
| 36 | |
| 37 DCHECK(task_runner_); | |
| 38 DCHECK(!scheduled_task_); | |
| 39 scheduled_task_.reset((task_runner_->Schedule(this, delay_ms)).release()); | |
| 40 } | |
| 41 | |
| 42 void CancelImpl() override { | |
| 43 if (scheduled_task_) { | |
| 44 scheduled_task_->Cancel(); | |
| 45 scheduled_task_.reset(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 // QuartcTaskRunner::Task overrides. | |
| 50 void Run() override { | |
| 51 // The alarm may have been cancelled. | |
| 52 if (!deadline().IsInitialized()) { | |
| 53 return; | |
| 54 } | |
| 55 | |
| 56 // The alarm may have been re-set to a later time. | |
| 57 if (clock_->Now() < deadline()) { | |
| 58 SetImpl(); | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 Fire(); | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 // Not owned by QuartcAlarm. Owned by the QuartcFactory. | |
| 67 const net::QuicClock* clock_; | |
| 68 // Not owned by QuartcAlarm. Owned by the QuartcFactory. | |
| 69 net::QuartcTaskRunnerInterface* task_runner_; | |
| 70 // Owned by QuartcAlarm. | |
| 71 std::unique_ptr<net::QuartcTaskRunnerInterface::ScheduledTask> | |
| 72 scheduled_task_; | |
| 73 }; | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 namespace net { | |
| 78 | |
| 79 // Only one AtExitManager is expected to be created for one process. | |
| 80 static base::AtExitManager* at_exit_manager = nullptr; | |
| 81 | |
| 82 QuartcFactory::QuartcFactory(const QuartcFactoryConfig& factory_config) { | |
| 83 task_runner_.reset(factory_config.task_runner); | |
| 84 if (!at_exit_manager) { | |
|
pthatcher1
2016/10/16 01:15:27
I think a simpler solution would be to just have a
zhihuang1
2016/10/16 19:38:12
Yes, I agree. We don't have much control on the At
pthatcher2
2016/10/24 17:42:49
It depends on how we use QuicTransportChannels. I
| |
| 85 LOG(WARNING) << "The AtExitManager has already been created."; | |
| 86 return; | |
| 87 } | |
| 88 at_exit_manager = new base::AtExitManager; | |
|
pthatcher1
2016/10/16 01:15:27
What happened to the test one?
zhihuang1
2016/10/16 19:38:12
We need the test mode AtExitManager only in the un
| |
| 89 } | |
| 90 | |
| 91 QuartcFactory::~QuartcFactory() { | |
| 92 if (!at_exit_manager) { | |
| 93 delete at_exit_manager; | |
| 94 at_exit_manager = nullptr; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 std::unique_ptr<QuartcSessionInterface> QuartcFactory::CreateQuartcSession( | |
| 99 const QuartcSessionConfig& quartc_session_config) { | |
| 100 DCHECK(quartc_session_config.packet_transport); | |
| 101 | |
| 102 Perspective perspective = quartc_session_config.is_server | |
| 103 ? Perspective::IS_SERVER | |
| 104 : Perspective::IS_CLIENT; | |
| 105 std::unique_ptr<QuicConnection> quic_connection = | |
| 106 CreateQuicConnection(quartc_session_config, perspective); | |
| 107 QuicConfig quic_config; | |
| 108 return std::unique_ptr<QuartcSessionInterface>( | |
| 109 new QuartcSession(std::move(quic_connection), quic_config, | |
| 110 quartc_session_config.unique_remote_server_id, | |
| 111 perspective, this /*QuicConnectionHelperInterface*/)); | |
| 112 } | |
| 113 | |
| 114 std::unique_ptr<QuicConnection> QuartcFactory::CreateQuicConnection( | |
| 115 const QuartcSessionConfig& quartc_session_config, | |
| 116 Perspective perspective) { | |
| 117 // The QuicConnection will take the ownership. | |
| 118 std::unique_ptr<QuartcPacketWriter> writer( | |
| 119 new QuartcPacketWriter(quartc_session_config.packet_transport, | |
| 120 quartc_session_config.max_packet_size)); | |
| 121 // dummy_id and dummy_address are used because Quartc network layer will not | |
| 122 // use these two. | |
| 123 QuicConnectionId dummy_id = 0; | |
| 124 IPEndPoint dummy_address(IPAddress(0, 0, 0, 0), 0 /*Port*/); | |
| 125 return std::unique_ptr<QuicConnection>(new QuicConnection( | |
| 126 dummy_id, dummy_address, this, /*QuicConnectionHelperInterface*/ | |
| 127 this /*QuicAlarmFactory*/, writer.release(), true /*own the writer*/, | |
| 128 perspective, AllSupportedVersions())); | |
| 129 } | |
| 130 | |
| 131 QuicAlarm* QuartcFactory::CreateAlarm(QuicAlarm::Delegate* delegate) { | |
| 132 return new QuartcAlarm(GetClock(), task_runner_.get(), | |
| 133 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); | |
| 134 } | |
| 135 | |
| 136 QuicArenaScopedPtr<QuicAlarm> QuartcFactory::CreateAlarm( | |
| 137 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, | |
| 138 QuicConnectionArena* arena) { | |
| 139 return QuicArenaScopedPtr<QuicAlarm>( | |
| 140 new QuartcAlarm(GetClock(), task_runner_.get(), std::move(delegate))); | |
| 141 } | |
| 142 | |
| 143 const QuicClock* QuartcFactory::GetClock() const { | |
| 144 return &clock_; | |
| 145 } | |
| 146 | |
| 147 QuicRandom* QuartcFactory::GetRandomGenerator() { | |
| 148 return QuicRandom::GetInstance(); | |
| 149 } | |
| 150 | |
| 151 QuicBufferAllocator* QuartcFactory::GetBufferAllocator() { | |
| 152 return &buffer_allocator_; | |
| 153 } | |
| 154 | |
| 155 std::unique_ptr<QuartcFactoryInterface> CreateQuartcFactory( | |
| 156 const QuartcFactoryConfig& factory_config) { | |
| 157 return std::unique_ptr<QuartcFactoryInterface>( | |
| 158 new QuartcFactory(factory_config)); | |
| 159 } | |
| 160 | |
| 161 } // namespace net | |
| OLD | NEW |