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* quartc_task_runner, | |
21 net::QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) | |
22 : net::QuicAlarm(std::move(delegate)), | |
23 clock_(clock), | |
24 quartc_task_runner_(quartc_task_runner) {} | |
25 | |
26 // QuicAlarm overrides. | |
27 void SetImpl() override { | |
28 DCHECK(deadline().IsInitialized()); | |
29 // Cancel it if already set. | |
30 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
| |
31 | |
32 int64_t delay_ms = (deadline() - (clock_->Now())).ToMilliseconds(); | |
33 if (delay_ms < 0) { | |
34 delay_ms = 0; | |
35 } | |
36 | |
37 DCHECK(quartc_task_runner_); | |
38 DCHECK(!canceler_); | |
39 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
| |
40 } | |
41 | |
42 void CancelImpl() override { | |
43 if (canceler_) { | |
44 canceler_->Cancel(); | |
45 canceler_.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* quartc_task_runner_ = nullptr; | |
70 // Owned by QuartcAlarm. | |
71 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.
| |
72 }; | |
73 | |
74 } // namespace | |
75 | |
76 namespace net { | |
77 | |
78 QuartcFactory::QuartcFactory(const QuartcFactoryConfig& factory_config) { | |
79 quartc_task_runner_.reset(factory_config.quartc_task_runner); | |
80 } | |
81 | |
82 QuartcFactory::~QuartcFactory() {} | |
83 | |
84 std::unique_ptr<QuartcSessionInterface> QuartcFactory::CreateQuartcSession( | |
85 const QuartcSessionConfig& quartc_session_config) { | |
86 DCHECK(quartc_session_config.packet_transport); | |
87 | |
88 Perspective perspective = quartc_session_config.is_server | |
89 ? Perspective::IS_SERVER | |
90 : Perspective::IS_CLIENT; | |
91 std::unique_ptr<QuicConnection> quic_connection = | |
92 CreateQuicConnection(quartc_session_config, perspective); | |
93 QuicConfig quic_config; | |
94 return std::unique_ptr<QuartcSessionInterface>( | |
95 new QuartcSession(std::move(quic_connection), quic_config, | |
96 quartc_session_config.unique_remote_server_id, | |
97 perspective, this /*QuicConnectionHelperInterface*/)); | |
98 } | |
99 | |
100 std::unique_ptr<QuicConnection> QuartcFactory::CreateQuicConnection( | |
101 const QuartcSessionConfig& quartc_session_config, | |
102 Perspective perspective) { | |
103 // The QuicConnection will take the ownership. | |
104 std::unique_ptr<QuartcPacketWriter> writer( | |
105 new QuartcPacketWriter(quartc_session_config.packet_transport)); | |
106 // dummy_id and dummy_address are used because Quartc network layer will not | |
107 // use these two. | |
108 QuicConnectionId dummy_id = 0; | |
109 IPEndPoint dummy_address(IPAddress(0, 0, 0, 0), 0 /*Port*/); | |
110 return std::unique_ptr<QuicConnection>(new QuicConnection( | |
111 dummy_id, dummy_address, this, /*QuicConnectionHelperInterface*/ | |
112 this /*QuicAlarmFactory*/, writer.release(), true /*own the writer*/, | |
113 perspective, AllSupportedVersions())); | |
114 } | |
115 | |
116 QuicAlarm* QuartcFactory::CreateAlarm(QuicAlarm::Delegate* delegate) { | |
117 return new QuartcAlarm(GetClock(), quartc_task_runner_.get(), | |
118 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); | |
119 } | |
120 | |
121 QuicArenaScopedPtr<QuicAlarm> QuartcFactory::CreateAlarm( | |
122 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, | |
123 QuicConnectionArena* arena) { | |
124 return QuicArenaScopedPtr<QuicAlarm>(new QuartcAlarm( | |
125 GetClock(), quartc_task_runner_.get(), std::move(delegate))); | |
126 } | |
127 | |
128 const QuicClock* QuartcFactory::GetClock() const { | |
129 return &clock_; | |
130 } | |
131 | |
132 QuicRandom* QuartcFactory::GetRandomGenerator() { | |
133 return QuicRandom::GetInstance(); | |
134 } | |
135 | |
136 QuicBufferAllocator* QuartcFactory::GetBufferAllocator() { | |
137 return &buffer_allocator_; | |
138 } | |
139 | |
140 std::unique_ptr<QuartcFactoryInterface> CreateQuartcFactory( | |
141 const QuartcFactoryConfig& factory_config) { | |
142 return std::unique_ptr<QuartcFactoryInterface>( | |
143 new QuartcFactory(factory_config)); | |
144 } | |
145 | |
146 } // namespace net | |
OLD | NEW |