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