[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" |
| 10 | #include "base/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 | |
| 17 | QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner, |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 18 | const QuicClock* clock, |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 19 | QuicRandom* random_generator, |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 20 | DatagramClientSocket* socket) |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 21 | : weak_factory_(this), |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 22 | task_runner_(task_runner), |
| 23 | socket_(socket), |
| 24 | clock_(clock), |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 25 | random_generator_(random_generator), |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 26 | send_alarm_registered_(false), |
[email protected] | 516f6f8 | 2013-01-14 23:31:17 | [diff] [blame] | 27 | timeout_alarm_registered_(false), |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 28 | retransmission_alarm_registered_(false), |
| 29 | retransmission_alarm_running_(false), |
[email protected] | 8950326 | 2013-01-17 02:08:55 | [diff] [blame] | 30 | ack_alarm_registered_(false), |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 31 | ack_alarm_time_(QuicTime::Zero()) { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | QuicConnectionHelper::~QuicConnectionHelper() { |
| 35 | } |
| 36 | |
| 37 | void QuicConnectionHelper::SetConnection(QuicConnection* connection) { |
| 38 | connection_ = connection; |
| 39 | } |
| 40 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 41 | const QuicClock* QuicConnectionHelper::GetClock() const { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 42 | return clock_; |
| 43 | } |
| 44 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 45 | QuicRandom* QuicConnectionHelper::GetRandomGenerator() { |
| 46 | return random_generator_; |
| 47 | } |
| 48 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 49 | int QuicConnectionHelper::WritePacketToWire( |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 50 | const QuicEncryptedPacket& packet, |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 51 | int* error) { |
| 52 | if (connection_->ShouldSimulateLostPacket()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 53 | DLOG(INFO) << "Dropping packet due to fake packet loss."; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 54 | *error = 0; |
| 55 | return packet.length(); |
| 56 | } |
| 57 | |
[email protected] | acf2474 | 2012-11-14 07:28:24 | [diff] [blame] | 58 | scoped_refptr<StringIOBuffer> buf( |
| 59 | new StringIOBuffer(std::string(packet.data(), |
| 60 | packet.length()))); |
[email protected] | ed26f172 | 2012-12-22 01:23:57 | [diff] [blame] | 61 | int rv = socket_->Write(buf, packet.length(), |
| 62 | base::Bind(&QuicConnectionHelper::OnWriteComplete, |
| 63 | weak_factory_.GetWeakPtr())); |
| 64 | if (rv >= 0) { |
| 65 | *error = 0; |
| 66 | } else { |
| 67 | *error = rv; |
| 68 | rv = -1; |
| 69 | } |
| 70 | return rv; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 73 | bool QuicConnectionHelper::IsWriteBlockedDataBuffered() { |
| 74 | // Chrome sockets' Write() methods buffer the data until the Write is |
| 75 | // permitted. |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool QuicConnectionHelper::IsWriteBlocked(int error) { |
| 80 | return error == ERR_IO_PENDING; |
| 81 | } |
| 82 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 83 | void QuicConnectionHelper::SetRetransmissionAlarm(QuicTime::Delta delay) { |
| 84 | if (!retransmission_alarm_registered_) { |
[email protected] | 516f6f8 | 2013-01-14 23:31:17 | [diff] [blame] | 85 | task_runner_->PostDelayedTask( |
| 86 | FROM_HERE, |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 87 | base::Bind(&QuicConnectionHelper::OnRetransmissionAlarm, |
[email protected] | 516f6f8 | 2013-01-14 23:31:17 | [diff] [blame] | 88 | weak_factory_.GetWeakPtr()), |
| 89 | base::TimeDelta::FromMicroseconds(delay.ToMicroseconds())); |
| 90 | } |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 8950326 | 2013-01-17 02:08:55 | [diff] [blame] | 93 | void QuicConnectionHelper::SetAckAlarm(QuicTime::Delta delay) { |
| 94 | if (!ack_alarm_registered_) { |
| 95 | task_runner_->PostDelayedTask( |
| 96 | FROM_HERE, |
| 97 | base::Bind(&QuicConnectionHelper::OnAckAlarm, |
| 98 | weak_factory_.GetWeakPtr()), |
| 99 | base::TimeDelta::FromMicroseconds(delay.ToMicroseconds())); |
| 100 | } |
| 101 | ack_alarm_registered_ = true; |
| 102 | ack_alarm_time_ = clock_->Now().Add(delay); |
| 103 | } |
| 104 | |
| 105 | void QuicConnectionHelper::ClearAckAlarm() { |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 106 | ack_alarm_time_ = QuicTime::Zero(); |
[email protected] | 8950326 | 2013-01-17 02:08:55 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 109 | void QuicConnectionHelper::SetSendAlarm(QuicTime alarm_time) { |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 110 | send_alarm_registered_ = true; |
| 111 | task_runner_->PostDelayedTask( |
| 112 | FROM_HERE, |
| 113 | base::Bind(&QuicConnectionHelper::OnSendAlarm, |
| 114 | weak_factory_.GetWeakPtr()), |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 115 | base::TimeDelta::FromMicroseconds( |
| 116 | alarm_time.Subtract(QuicTime::Zero()).ToMicroseconds())); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 2a960e0 | 2012-11-11 14:48:10 | [diff] [blame] | 119 | void QuicConnectionHelper::SetTimeoutAlarm(QuicTime::Delta delay) { |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame^] | 120 | // CheckForTimeout will call SetTimeoutAlarm for the remaining time if alarm |
| 121 | // goes off before the delay. |
| 122 | if (timeout_alarm_registered_) |
| 123 | return; |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 124 | timeout_alarm_registered_ = true; |
| 125 | task_runner_->PostDelayedTask( |
| 126 | FROM_HERE, |
| 127 | base::Bind(&QuicConnectionHelper::OnTimeoutAlarm, |
| 128 | weak_factory_.GetWeakPtr()), |
[email protected] | 2a960e0 | 2012-11-11 14:48:10 | [diff] [blame] | 129 | base::TimeDelta::FromMicroseconds(delay.ToMicroseconds())); |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool QuicConnectionHelper::IsSendAlarmSet() { |
| 133 | return send_alarm_registered_; |
| 134 | } |
| 135 | |
| 136 | void QuicConnectionHelper::UnregisterSendAlarmIfRegistered() { |
| 137 | send_alarm_registered_ = false; |
| 138 | } |
| 139 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 140 | void QuicConnectionHelper::OnRetransmissionAlarm() { |
| 141 | QuicTime when = connection_->OnRetransmissionTimeout(); |
| 142 | if (!when.IsInitialized()) { |
| 143 | SetRetransmissionAlarm(clock_->Now().Subtract(when)); |
[email protected] | 516f6f8 | 2013-01-14 23:31:17 | [diff] [blame] | 144 | } |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void QuicConnectionHelper::OnSendAlarm() { |
| 148 | if (send_alarm_registered_) { |
| 149 | send_alarm_registered_ = false; |
| 150 | connection_->OnCanWrite(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void QuicConnectionHelper::OnTimeoutAlarm() { |
| 155 | timeout_alarm_registered_ = false; |
| 156 | connection_->CheckForTimeout(); |
| 157 | } |
| 158 | |
[email protected] | 8950326 | 2013-01-17 02:08:55 | [diff] [blame] | 159 | void QuicConnectionHelper::OnAckAlarm() { |
| 160 | ack_alarm_registered_ = false; |
| 161 | // Alarm may have been cleared. |
| 162 | if (!ack_alarm_time_.IsInitialized()) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Alarm may have been reset to a later time. |
| 167 | if (clock_->Now() < ack_alarm_time_) { |
| 168 | SetAckAlarm(ack_alarm_time_.Subtract(clock_->Now())); |
| 169 | return; |
| 170 | } |
| 171 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 172 | ack_alarm_time_ = QuicTime::Zero(); |
[email protected] | 8950326 | 2013-01-17 02:08:55 | [diff] [blame] | 173 | connection_->SendAck(); |
| 174 | } |
| 175 | |
[email protected] | acf2474 | 2012-11-14 07:28:24 | [diff] [blame] | 176 | void QuicConnectionHelper::OnWriteComplete(int result) { |
| 177 | // TODO(rch): Inform the connection about the result. |
| 178 | connection_->OnCanWrite(); |
| 179 | } |
| 180 | |
[email protected] | 1d197ef5 | 2012-11-07 20:41:29 | [diff] [blame] | 181 | } // namespace net |