blob: c3e796fa321ec3c50fd9965bc55b14716191e29b [file] [log] [blame]
[email protected]1d197ef52012-11-07 20:41:291// 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]f002abb2013-06-28 02:30:2110#include "base/time/time.h"
[email protected]acf24742012-11-14 07:28:2411#include "net/base/io_buffer.h"
[email protected]ccc66e8a2013-03-26 08:26:1412#include "net/base/net_errors.h"
[email protected]1d197ef52012-11-07 20:41:2913#include "net/quic/quic_utils.h"
14
15namespace net {
16
[email protected]965dbe62013-08-09 21:34:3117namespace {
18
19class 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]1d197ef52012-11-07 20:41:2983QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner,
[email protected]97693d12012-11-16 16:05:0084 const QuicClock* clock,
[email protected]9558c5d32012-12-22 00:08:1485 QuicRandom* random_generator,
[email protected]1d197ef52012-11-07 20:41:2986 DatagramClientSocket* socket)
[email protected]aa249b52013-04-30 01:04:3287 : weak_factory_(this),
[email protected]1d197ef52012-11-07 20:41:2988 task_runner_(task_runner),
89 socket_(socket),
90 clock_(clock),
[email protected]965dbe62013-08-09 21:34:3191 random_generator_(random_generator) {
[email protected]1d197ef52012-11-07 20:41:2992}
93
94QuicConnectionHelper::~QuicConnectionHelper() {
95}
96
97void QuicConnectionHelper::SetConnection(QuicConnection* connection) {
98 connection_ = connection;
99}
100
[email protected]97693d12012-11-16 16:05:00101const QuicClock* QuicConnectionHelper::GetClock() const {
[email protected]1d197ef52012-11-07 20:41:29102 return clock_;
103}
104
[email protected]9558c5d32012-12-22 00:08:14105QuicRandom* QuicConnectionHelper::GetRandomGenerator() {
106 return random_generator_;
107}
108
[email protected]1d197ef52012-11-07 20:41:29109int QuicConnectionHelper::WritePacketToWire(
[email protected]1d197ef52012-11-07 20:41:29110 const QuicEncryptedPacket& packet,
[email protected]1d197ef52012-11-07 20:41:29111 int* error) {
112 if (connection_->ShouldSimulateLostPacket()) {
[email protected]044ac2b2012-11-13 21:41:06113 DLOG(INFO) << "Dropping packet due to fake packet loss.";
[email protected]1d197ef52012-11-07 20:41:29114 *error = 0;
115 return packet.length();
116 }
117
[email protected]acf24742012-11-14 07:28:24118 scoped_refptr<StringIOBuffer> buf(
119 new StringIOBuffer(std::string(packet.data(),
120 packet.length())));
[email protected]90499482013-06-01 00:39:50121 int rv = socket_->Write(buf.get(),
122 packet.length(),
[email protected]ed26f1722012-12-22 01:23:57123 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]1d197ef52012-11-07 20:41:29132}
133
[email protected]ccc66e8a2013-03-26 08:26:14134bool QuicConnectionHelper::IsWriteBlockedDataBuffered() {
135 // Chrome sockets' Write() methods buffer the data until the Write is
136 // permitted.
137 return true;
138}
139
140bool QuicConnectionHelper::IsWriteBlocked(int error) {
141 return error == ERR_IO_PENDING;
142}
143
[email protected]965dbe62013-08-09 21:34:31144QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
145 return new QuicChromeAlarm(clock_, task_runner_, delegate);
[email protected]89503262013-01-17 02:08:55146}
147
[email protected]acf24742012-11-14 07:28:24148void QuicConnectionHelper::OnWriteComplete(int result) {
149 // TODO(rch): Inform the connection about the result.
150 connection_->OnCanWrite();
151}
152
[email protected]1d197ef52012-11-07 20:41:29153} // namespace net