Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: net/quic/quic_connection_helper.cc

Issue 26331009: Fix QuicConnectionHelper's alarm implementation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/quic/quic_connection_helper_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/quic_connection_helper.h" 5 #include "net/quic/quic_connection_helper.h"
6 6
7 #include "base/location.h" 7 #include "base/location.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/metrics/sparse_histogram.h" 9 #include "base/metrics/sparse_histogram.h"
10 #include "base/task_runner.h" 10 #include "base/task_runner.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "net/base/io_buffer.h" 12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
14 #include "net/quic/quic_utils.h" 14 #include "net/quic/quic_utils.h"
15 15
16 namespace net { 16 namespace net {
17 17
18 namespace { 18 namespace {
19 19
20 class QuicChromeAlarm : public QuicAlarm { 20 class QuicChromeAlarm : public QuicAlarm {
21 public: 21 public:
22 QuicChromeAlarm(const QuicClock* clock, 22 QuicChromeAlarm(const QuicClock* clock,
23 base::TaskRunner* task_runner, 23 base::TaskRunner* task_runner,
24 QuicAlarm::Delegate* delegate) 24 QuicAlarm::Delegate* delegate)
25 : QuicAlarm(delegate), 25 : QuicAlarm(delegate),
26 clock_(clock), 26 clock_(clock),
27 task_runner_(task_runner), 27 task_runner_(task_runner),
28 task_posted_(false), 28 task_deadline_(QuicTime::Zero()),
29 weak_factory_(this) {} 29 weak_factory_(this) {}
30 30
31 protected: 31 protected:
32 virtual void SetImpl() OVERRIDE { 32 virtual void SetImpl() OVERRIDE {
33 DCHECK(deadline().IsInitialized()); 33 DCHECK(deadline().IsInitialized());
34 if (task_posted_) { 34 if (task_deadline_.IsInitialized()) {
35 // Since tasks can not be un-posted, OnAlarm will be invoked which 35 if (task_deadline_ <= deadline()) {
36 // will notice that deadline has not yet been reached, and will set 36 // Since tasks can not be un-posted, OnAlarm will be invoked which
37 // the alarm for the new deadline. 37 // will notice that deadline has not yet been reached, and will set
38 return; 38 // the alarm for the new deadline.
39 return;
40 }
41 // The scheduled task is after new deadline. Invalidate the weak ptrs
42 // so that task does not execute when we're not expecting it.
43 weak_factory_.InvalidateWeakPtrs();
39 } 44 }
40 45
41 int64 delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds(); 46 int64 delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds();
42 if (delay_us < 0) { 47 if (delay_us < 0) {
43 delay_us = 0; 48 delay_us = 0;
44 } 49 }
45 task_runner_->PostDelayedTask( 50 task_runner_->PostDelayedTask(
46 FROM_HERE, 51 FROM_HERE,
47 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()), 52 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()),
48 base::TimeDelta::FromMicroseconds(delay_us)); 53 base::TimeDelta::FromMicroseconds(delay_us));
49 task_posted_ = true; 54 task_deadline_ = deadline();
50 } 55 }
51 56
52 virtual void CancelImpl() OVERRIDE { 57 virtual void CancelImpl() OVERRIDE {
53 DCHECK(!deadline().IsInitialized()); 58 DCHECK(!deadline().IsInitialized());
54 // Since tasks can not be un-posted, OnAlarm will be invoked which 59 // Since tasks can not be un-posted, OnAlarm will be invoked which
55 // will notice that deadline is not Initialized and will do nothing. 60 // will notice that deadline is not Initialized and will do nothing.
56 } 61 }
57 62
58 private: 63 private:
59 void OnAlarm() { 64 void OnAlarm() {
60 DCHECK(task_posted_); 65 DCHECK(task_deadline_.IsInitialized());
61 task_posted_ = false; 66 task_deadline_ = QuicTime::Zero();
62 // The alarm may have been cancelled. 67 // The alarm may have been cancelled.
63 if (!deadline().IsInitialized()) { 68 if (!deadline().IsInitialized()) {
64 return; 69 return;
65 } 70 }
66 71
67 // The alarm may have been re-set to a later time. 72 // The alarm may have been re-set to a later time.
68 if (clock_->Now() < deadline()) { 73 if (clock_->Now() < deadline()) {
69 SetImpl(); 74 SetImpl();
70 return; 75 return;
71 } 76 }
72 77
73 Fire(); 78 Fire();
74 } 79 }
75 80
76 const QuicClock* clock_; 81 const QuicClock* clock_;
77 base::TaskRunner* task_runner_; 82 base::TaskRunner* task_runner_;
78 bool task_posted_; 83 // If a task has been posted to the message loop, this is the time it
84 // was scheduled to fire. Tracking this allows us to avoid posting a
85 // new tast if the new deadline is in the future, but permits us to
86 // post a new task when the deadline is in the past.
jar (doing other things) 2013/10/12 21:05:04 nit: "...deadline is in the past" --> "...deadline
Ryan Hamilton 2013/10/15 20:58:44 Done.
87 QuicTime task_deadline_;
79 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_; 88 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_;
80 }; 89 };
81 90
82 } // namespace 91 } // namespace
83 92
84 QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner, 93 QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner,
85 const QuicClock* clock, 94 const QuicClock* clock,
86 QuicRandom* random_generator, 95 QuicRandom* random_generator,
87 DatagramClientSocket* socket) 96 DatagramClientSocket* socket)
88 : weak_factory_(this), 97 : weak_factory_(this),
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { 157 QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
149 return new QuicChromeAlarm(clock_, task_runner_, delegate); 158 return new QuicChromeAlarm(clock_, task_runner_, delegate);
150 } 159 }
151 160
152 void QuicConnectionHelper::OnWriteComplete(int result) { 161 void QuicConnectionHelper::OnWriteComplete(int result) {
153 // TODO(rch): Inform the connection about the result. 162 // TODO(rch): Inform the connection about the result.
154 connection_->OnCanWrite(); 163 connection_->OnCanWrite();
155 } 164 }
156 165
157 } // namespace net 166 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/quic/quic_connection_helper_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698