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 ~QuartcAlarm() override { |
| 27 // Cancel the scheduled task before getting deleted. |
| 28 CancelImpl(); |
| 29 } |
| 30 |
| 31 // QuicAlarm overrides. |
| 32 void SetImpl() override { |
| 33 DCHECK(deadline().IsInitialized()); |
| 34 // Cancel it if already set. |
| 35 CancelImpl(); |
| 36 |
| 37 int64_t delay_ms = (deadline() - (clock_->Now())).ToMilliseconds(); |
| 38 if (delay_ms < 0) { |
| 39 delay_ms = 0; |
| 40 } |
| 41 |
| 42 DCHECK(task_runner_); |
| 43 DCHECK(!scheduled_task_); |
| 44 scheduled_task_.reset((task_runner_->Schedule(this, delay_ms)).release()); |
| 45 } |
| 46 |
| 47 void CancelImpl() override { |
| 48 if (scheduled_task_) { |
| 49 scheduled_task_->Cancel(); |
| 50 scheduled_task_.reset(); |
| 51 } |
| 52 } |
| 53 |
| 54 // QuartcTaskRunner::Task overrides. |
| 55 void Run() override { |
| 56 // The alarm may have been cancelled. |
| 57 if (!deadline().IsInitialized()) { |
| 58 return; |
| 59 } |
| 60 |
| 61 // The alarm may have been re-set to a later time. |
| 62 if (clock_->Now() < deadline()) { |
| 63 SetImpl(); |
| 64 return; |
| 65 } |
| 66 |
| 67 Fire(); |
| 68 } |
| 69 |
| 70 private: |
| 71 // Not owned by QuartcAlarm. Owned by the QuartcFactory. |
| 72 const net::QuicClock* clock_; |
| 73 // Not owned by QuartcAlarm. Owned by the QuartcFactory. |
| 74 net::QuartcTaskRunnerInterface* task_runner_; |
| 75 // Owned by QuartcAlarm. |
| 76 std::unique_ptr<net::QuartcTaskRunnerInterface::ScheduledTask> |
| 77 scheduled_task_; |
| 78 }; |
| 79 |
| 80 } // namespace |
| 81 |
| 82 namespace net { |
| 83 |
| 84 QuartcFactory::QuartcFactory(const QuartcFactoryConfig& factory_config) { |
| 85 task_runner_.reset(factory_config.task_runner); |
| 86 if (factory_config.create_at_exit_manager) { |
| 87 at_exit_manager_.reset(new base::AtExitManager); |
| 88 } |
| 89 } |
| 90 |
| 91 QuartcFactory::~QuartcFactory() {} |
| 92 |
| 93 std::unique_ptr<QuartcSessionInterface> QuartcFactory::CreateQuartcSession( |
| 94 const QuartcSessionConfig& quartc_session_config) { |
| 95 DCHECK(quartc_session_config.packet_transport); |
| 96 |
| 97 Perspective perspective = quartc_session_config.is_server |
| 98 ? Perspective::IS_SERVER |
| 99 : Perspective::IS_CLIENT; |
| 100 std::unique_ptr<QuicConnection> quic_connection = |
| 101 CreateQuicConnection(quartc_session_config, perspective); |
| 102 QuicConfig quic_config; |
| 103 return std::unique_ptr<QuartcSessionInterface>( |
| 104 new QuartcSession(std::move(quic_connection), quic_config, |
| 105 quartc_session_config.unique_remote_server_id, |
| 106 perspective, this /*QuicConnectionHelperInterface*/)); |
| 107 } |
| 108 |
| 109 std::unique_ptr<QuicConnection> QuartcFactory::CreateQuicConnection( |
| 110 const QuartcSessionConfig& quartc_session_config, |
| 111 Perspective perspective) { |
| 112 // The QuicConnection will take the ownership. |
| 113 std::unique_ptr<QuartcPacketWriter> writer( |
| 114 new QuartcPacketWriter(quartc_session_config.packet_transport, |
| 115 quartc_session_config.max_packet_size)); |
| 116 // dummy_id and dummy_address are used because Quartc network layer will not |
| 117 // use these two. |
| 118 QuicConnectionId dummy_id = 0; |
| 119 IPEndPoint dummy_address(IPAddress(0, 0, 0, 0), 0 /*Port*/); |
| 120 return std::unique_ptr<QuicConnection>(new QuicConnection( |
| 121 dummy_id, dummy_address, this, /*QuicConnectionHelperInterface*/ |
| 122 this /*QuicAlarmFactory*/, writer.release(), true /*own the writer*/, |
| 123 perspective, AllSupportedVersions())); |
| 124 } |
| 125 |
| 126 QuicAlarm* QuartcFactory::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 127 return new QuartcAlarm(GetClock(), task_runner_.get(), |
| 128 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); |
| 129 } |
| 130 |
| 131 QuicArenaScopedPtr<QuicAlarm> QuartcFactory::CreateAlarm( |
| 132 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
| 133 QuicConnectionArena* arena) { |
| 134 return QuicArenaScopedPtr<QuicAlarm>( |
| 135 new QuartcAlarm(GetClock(), task_runner_.get(), std::move(delegate))); |
| 136 } |
| 137 |
| 138 const QuicClock* QuartcFactory::GetClock() const { |
| 139 return &clock_; |
| 140 } |
| 141 |
| 142 QuicRandom* QuartcFactory::GetRandomGenerator() { |
| 143 return QuicRandom::GetInstance(); |
| 144 } |
| 145 |
| 146 QuicBufferAllocator* QuartcFactory::GetBufferAllocator() { |
| 147 return &buffer_allocator_; |
| 148 } |
| 149 |
| 150 std::unique_ptr<QuartcFactoryInterface> CreateQuartcFactory( |
| 151 const QuartcFactoryConfig& factory_config) { |
| 152 return std::unique_ptr<QuartcFactoryInterface>( |
| 153 new QuartcFactory(factory_config)); |
| 154 } |
| 155 |
| 156 } // namespace net |
OLD | NEW |