[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/quic/quic_connection_helper.h" |
| 6 | |
| 7 | #include "base/location.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/task_runner.h" |
[email protected] | f002abb | 2013-06-28 02:30:21 | [diff] [blame] | 10 | #include "base/time/time.h" |
[email protected] | acf2474 | 2012-11-14 07:28:24 | [diff] [blame] | 11 | #include "net/base/io_buffer.h" |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 13 | #include "net/quic/quic_utils.h" |
| 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame^] | 17 | namespace { |
| 18 | |
| 19 | class QuicChromeAlarm : public QuicAlarm { |
| 20 | public: |
| 21 | QuicChromeAlarm(const QuicClock* clock, |
| 22 | base::TaskRunner* task_runner, |
| 23 | QuicAlarm::Delegate* delegate) |
| 24 | : QuicAlarm(delegate), |
| 25 | clock_(clock), |
| 26 | task_runner_(task_runner), |
| 27 | task_posted_(false), |
| 28 | weak_factory_(this) {} |
| 29 | |
| 30 | protected: |
| 31 | virtual void SetImpl() OVERRIDE { |
| 32 | DCHECK(deadline().IsInitialized()); |
| 33 | if (task_posted_) { |
| 34 | // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 35 | // will notice that deadline has not yet been reached, and will set |
| 36 | // the alarm for the new deadline. |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | int64 delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds(); |
| 41 | if (delay_us < 0) { |
| 42 | delay_us = 0; |
| 43 | } |
| 44 | task_runner_->PostDelayedTask( |
| 45 | FROM_HERE, |
| 46 | base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()), |
| 47 | base::TimeDelta::FromMicroseconds(delay_us)); |
| 48 | task_posted_ = true; |
| 49 | } |
| 50 | |
| 51 | virtual void CancelImpl() OVERRIDE { |
| 52 | DCHECK(!deadline().IsInitialized()); |
| 53 | // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 54 | // will notice that deadline is not Initialized and will do nothing. |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | void OnAlarm() { |
| 59 | DCHECK(task_posted_); |
| 60 | task_posted_ = false; |
| 61 | // The alarm may have been cancelled. |
| 62 | if (!deadline().IsInitialized()) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // The alarm may have been re-set to a later time. |
| 67 | if (clock_->Now() < deadline()) { |
| 68 | SetImpl(); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | Fire(); |
| 73 | } |
| 74 | |
| 75 | const QuicClock* clock_; |
| 76 | base::TaskRunner* task_runner_; |
| 77 | bool task_posted_; |
| 78 | base::WeakPtrFactory<QuicChromeAlarm> weak_factory_; |
| 79 | }; |
| 80 | |
| 81 | } // namespace |
| 82 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 83 | QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner, |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 84 | const QuicClock* clock, |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 85 | QuicRandom* random_generator, |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 86 | DatagramClientSocket* socket) |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 87 | : weak_factory_(this), |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 88 | task_runner_(task_runner), |
| 89 | socket_(socket), |
| 90 | clock_(clock), |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame^] | 91 | random_generator_(random_generator) { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | QuicConnectionHelper::~QuicConnectionHelper() { |
| 95 | } |
| 96 | |
| 97 | void QuicConnectionHelper::SetConnection(QuicConnection* connection) { |
| 98 | connection_ = connection; |
| 99 | } |
| 100 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 101 | const QuicClock* QuicConnectionHelper::GetClock() const { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 102 | return clock_; |
| 103 | } |
| 104 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 105 | QuicRandom* QuicConnectionHelper::GetRandomGenerator() { |
| 106 | return random_generator_; |
| 107 | } |
| 108 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 109 | int QuicConnectionHelper::WritePacketToWire( |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 110 | const QuicEncryptedPacket& packet, |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 111 | int* error) { |
| 112 | if (connection_->ShouldSimulateLostPacket()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 113 | DLOG(INFO) << "Dropping packet due to fake packet loss."; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 114 | *error = 0; |
| 115 | return packet.length(); |
| 116 | } |
| 117 | |
[email protected] | acf2474 | 2012-11-14 07:28:24 | [diff] [blame] | 118 | scoped_refptr<StringIOBuffer> buf( |
| 119 | new StringIOBuffer(std::string(packet.data(), |
| 120 | packet.length()))); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 121 | int rv = socket_->Write(buf.get(), |
| 122 | packet.length(), |
[email protected] | ed26f172 | 2012-12-22 01:23:57 | [diff] [blame] | 123 | base::Bind(&QuicConnectionHelper::OnWriteComplete, |
| 124 | weak_factory_.GetWeakPtr())); |
| 125 | if (rv >= 0) { |
| 126 | *error = 0; |
| 127 | } else { |
| 128 | *error = rv; |
| 129 | rv = -1; |
| 130 | } |
| 131 | return rv; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 134 | bool QuicConnectionHelper::IsWriteBlockedDataBuffered() { |
| 135 | // Chrome sockets' Write() methods buffer the data until the Write is |
| 136 | // permitted. |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | bool QuicConnectionHelper::IsWriteBlocked(int error) { |
| 141 | return error == ERR_IO_PENDING; |
| 142 | } |
| 143 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame^] | 144 | QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 145 | return new QuicChromeAlarm(clock_, task_runner_, delegate); |
[email protected] | 8950326 | 2013-01-17 02:08:55 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | acf2474 | 2012-11-14 07:28:24 | [diff] [blame] | 148 | void QuicConnectionHelper::OnWriteComplete(int result) { |
| 149 | // TODO(rch): Inform the connection about the result. |
| 150 | connection_->OnCanWrite(); |
| 151 | } |
| 152 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 153 | } // namespace net |