Index: net/quic/quartc/quartc_factory.cc |
diff --git a/net/quic/quartc/quartc_factory.cc b/net/quic/quartc/quartc_factory.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..24191743b3bfb025bf65555869517d58b4ac450d |
--- /dev/null |
+++ b/net/quic/quartc/quartc_factory.cc |
@@ -0,0 +1,146 @@ |
+// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/quic/quartc/quartc_factory.h" |
+ |
+#include "net/quic/core/crypto/quic_random.h" |
+#include "net/quic/quartc/quartc_alarm_factory.h" |
+#include "net/quic/quartc/quartc_session.h" |
+ |
+namespace { |
+ |
+// Implements the QuicAlarm with QuartcTaskRunnerInterface for the Quartc |
+// users other than Chromium. For example, WebRTC will create QuartcAlarm with |
+// a QuartcTaskRunner implemented by WebRTC. |
+class QuartcAlarm : public net::QuicAlarm, |
+ public net::QuartcTaskRunnerInterface::Task { |
+ public: |
+ QuartcAlarm(const net::QuicClock* clock, |
+ net::QuartcTaskRunnerInterface* quartc_task_runner, |
+ net::QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) |
+ : net::QuicAlarm(std::move(delegate)), |
+ clock_(clock), |
+ quartc_task_runner_(quartc_task_runner) {} |
+ |
+ // QuicAlarm overrides. |
+ void SetImpl() override { |
+ DCHECK(deadline().IsInitialized()); |
+ // Cancel it if already set. |
+ CancelImpl(); |
pthatcher1
2016/10/14 18:59:00
Isn't SetImpl guaranteed to only be called after C
zhihuang1
2016/10/16 00:45:28
https://cs.chromium.org/chromium/src/net/quic/core
|
+ |
+ int64_t delay_ms = (deadline() - (clock_->Now())).ToMilliseconds(); |
+ if (delay_ms < 0) { |
+ delay_ms = 0; |
+ } |
+ |
+ DCHECK(quartc_task_runner_); |
+ DCHECK(!canceler_); |
+ canceler_.reset(quartc_task_runner_->Schedule(this, delay_ms).release()); |
pthatcher1
2016/10/14 18:59:00
It seems like a std::move should be used.
zhihuang1
2016/10/16 00:45:28
The compiler complains when I use move. I must mis
|
+ } |
+ |
+ void CancelImpl() override { |
+ if (canceler_) { |
+ canceler_->Cancel(); |
+ canceler_.reset(); |
+ } |
+ } |
+ |
+ // QuartcTaskRunner::Task overrides. |
+ void Run() override { |
+ // The alarm may have been cancelled. |
+ if (!deadline().IsInitialized()) { |
+ return; |
+ } |
+ |
+ // The alarm may have been re-set to a later time. |
+ if (clock_->Now() < deadline()) { |
+ SetImpl(); |
+ return; |
+ } |
+ |
+ Fire(); |
+ } |
+ |
+ private: |
+ // Not owned by QuartcAlarm. Owned by the QuartcFactory. |
+ const net::QuicClock* clock_; |
+ // Not owned by QuartcAlarm. Owned by the QuartcFactory. |
+ net::QuartcTaskRunnerInterface* quartc_task_runner_ = nullptr; |
+ // Owned by QuartcAlarm. |
+ std::unique_ptr<net::QuartcTaskRunnerInterface::Canceler> canceler_; |
pthatcher1
2016/10/14 18:59:00
Sorry for the churn, but I thought of a better nam
zhihuang1
2016/10/16 00:45:28
Done.
|
+}; |
+ |
+} // namespace |
+ |
+namespace net { |
+ |
+QuartcFactory::QuartcFactory(const QuartcFactoryConfig& factory_config) { |
+ quartc_task_runner_.reset(factory_config.quartc_task_runner); |
+} |
+ |
+QuartcFactory::~QuartcFactory() {} |
+ |
+std::unique_ptr<QuartcSessionInterface> QuartcFactory::CreateQuartcSession( |
+ const QuartcSessionConfig& quartc_session_config) { |
+ DCHECK(quartc_session_config.packet_transport); |
+ |
+ Perspective perspective = quartc_session_config.is_server |
+ ? Perspective::IS_SERVER |
+ : Perspective::IS_CLIENT; |
+ std::unique_ptr<QuicConnection> quic_connection = |
+ CreateQuicConnection(quartc_session_config, perspective); |
+ QuicConfig quic_config; |
+ return std::unique_ptr<QuartcSessionInterface>( |
+ new QuartcSession(std::move(quic_connection), quic_config, |
+ quartc_session_config.unique_remote_server_id, |
+ perspective, this /*QuicConnectionHelperInterface*/)); |
+} |
+ |
+std::unique_ptr<QuicConnection> QuartcFactory::CreateQuicConnection( |
+ const QuartcSessionConfig& quartc_session_config, |
+ Perspective perspective) { |
+ // The QuicConnection will take the ownership. |
+ std::unique_ptr<QuartcPacketWriter> writer( |
+ new QuartcPacketWriter(quartc_session_config.packet_transport)); |
+ // dummy_id and dummy_address are used because Quartc network layer will not |
+ // use these two. |
+ QuicConnectionId dummy_id = 0; |
+ IPEndPoint dummy_address(IPAddress(0, 0, 0, 0), 0 /*Port*/); |
+ return std::unique_ptr<QuicConnection>(new QuicConnection( |
+ dummy_id, dummy_address, this, /*QuicConnectionHelperInterface*/ |
+ this /*QuicAlarmFactory*/, writer.release(), true /*own the writer*/, |
+ perspective, AllSupportedVersions())); |
+} |
+ |
+QuicAlarm* QuartcFactory::CreateAlarm(QuicAlarm::Delegate* delegate) { |
+ return new QuartcAlarm(GetClock(), quartc_task_runner_.get(), |
+ QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); |
+} |
+ |
+QuicArenaScopedPtr<QuicAlarm> QuartcFactory::CreateAlarm( |
+ QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
+ QuicConnectionArena* arena) { |
+ return QuicArenaScopedPtr<QuicAlarm>(new QuartcAlarm( |
+ GetClock(), quartc_task_runner_.get(), std::move(delegate))); |
+} |
+ |
+const QuicClock* QuartcFactory::GetClock() const { |
+ return &clock_; |
+} |
+ |
+QuicRandom* QuartcFactory::GetRandomGenerator() { |
+ return QuicRandom::GetInstance(); |
+} |
+ |
+QuicBufferAllocator* QuartcFactory::GetBufferAllocator() { |
+ return &buffer_allocator_; |
+} |
+ |
+std::unique_ptr<QuartcFactoryInterface> CreateQuartcFactory( |
+ const QuartcFactoryConfig& factory_config) { |
+ return std::unique_ptr<QuartcFactoryInterface>( |
+ new QuartcFactory(factory_config)); |
+} |
+ |
+} // namespace net |