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

Side by Side Diff: net/quic/chromium/quic_chromium_packet_writer.cc

Issue 2325733004: Makes QuicChromiumPacketWriter use the delegate for interacting with the connection. (Closed)
Patch Set: Nits fixed. Created 4 years, 3 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/chromium/quic_chromium_packet_writer.h" 5 #include "net/quic/chromium/quic_chromium_packet_writer.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/metrics/sparse_histogram.h" 12 #include "base/metrics/sparse_histogram.h"
13 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "net/quic/chromium/quic_chromium_client_session.h" 15 #include "net/quic/chromium/quic_chromium_client_session.h"
16 16
17 namespace net { 17 namespace net {
18 18
19 QuicChromiumPacketWriter::QuicChromiumPacketWriter() : weak_factory_(this) {} 19 QuicChromiumPacketWriter::QuicChromiumPacketWriter() : weak_factory_(this) {}
20 20
21 QuicChromiumPacketWriter::QuicChromiumPacketWriter(Socket* socket) 21 QuicChromiumPacketWriter::QuicChromiumPacketWriter(Socket* socket)
22 : socket_(socket), 22 : socket_(socket),
23 connection_(nullptr), 23 delegate_(nullptr),
24 observer_(nullptr),
25 packet_(nullptr), 24 packet_(nullptr),
26 write_blocked_(false), 25 write_blocked_(false),
27 weak_factory_(this) {} 26 weak_factory_(this) {}
28 27
29 QuicChromiumPacketWriter::~QuicChromiumPacketWriter() {} 28 QuicChromiumPacketWriter::~QuicChromiumPacketWriter() {}
30 29
31 void QuicChromiumPacketWriter::Initialize(WriteErrorObserver* observer,
32 QuicConnection* connection) {
33 observer_ = observer;
34 connection_ = connection;
35 }
36
37 int QuicChromiumPacketWriter::WritePacketToSocket(StringIOBuffer* packet) { 30 int QuicChromiumPacketWriter::WritePacketToSocket(StringIOBuffer* packet) {
38 return socket_->Write(packet, packet->size(), 31 return socket_->Write(packet, packet->size(),
39 base::Bind(&QuicChromiumPacketWriter::OnWriteComplete, 32 base::Bind(&QuicChromiumPacketWriter::OnWriteComplete,
40 weak_factory_.GetWeakPtr())); 33 weak_factory_.GetWeakPtr()));
41 } 34 }
42 35
43 WriteResult QuicChromiumPacketWriter::WritePacket( 36 WriteResult QuicChromiumPacketWriter::WritePacket(
44 const char* buffer, 37 const char* buffer,
45 size_t buf_len, 38 size_t buf_len,
46 const IPAddress& self_address, 39 const IPAddress& self_address,
47 const IPEndPoint& peer_address, 40 const IPEndPoint& peer_address,
48 PerPacketOptions* /*options*/) { 41 PerPacketOptions* /*options*/) {
49 scoped_refptr<StringIOBuffer> buf( 42 scoped_refptr<StringIOBuffer> buf(
50 new StringIOBuffer(std::string(buffer, buf_len))); 43 new StringIOBuffer(std::string(buffer, buf_len)));
51 DCHECK(!IsWriteBlocked()); 44 DCHECK(!IsWriteBlocked());
52 base::TimeTicks now = base::TimeTicks::Now(); 45 base::TimeTicks now = base::TimeTicks::Now();
53 46
54 int rv = WritePacketToSocket(buf.get()); 47 int rv = WritePacketToSocket(buf.get());
55 48
56 if (rv < 0 && rv != ERR_IO_PENDING && observer_ != nullptr) { 49 if (rv < 0 && rv != ERR_IO_PENDING && delegate_ != nullptr) {
57 // If write error, then call observer's OnWriteError, which may be 50 // If write error, then call delegate's HandleWriteError, which
58 // able to migrate and rewrite packet on a new socket. 51 // may be able to migrate and rewrite packet on a new socket.
59 // OnWriteError returns the outcome of that attempt, which is returned 52 // HandleWriteError returns the outcome of that rewrite attempt.
60 // to the caller. 53 rv = delegate_->HandleWriteError(rv, buf);
61 rv = observer_->OnWriteError(rv, buf);
62 } 54 }
63 55
64 WriteStatus status = WRITE_STATUS_OK; 56 WriteStatus status = WRITE_STATUS_OK;
65 if (rv < 0) { 57 if (rv < 0) {
66 if (rv != ERR_IO_PENDING) { 58 if (rv != ERR_IO_PENDING) {
67 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv); 59 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv);
68 status = WRITE_STATUS_ERROR; 60 status = WRITE_STATUS_ERROR;
69 } else { 61 } else {
70 status = WRITE_STATUS_BLOCKED; 62 status = WRITE_STATUS_BLOCKED;
71 write_blocked_ = true; 63 write_blocked_ = true;
(...skipping 20 matching lines...) Expand all
92 bool QuicChromiumPacketWriter::IsWriteBlocked() const { 84 bool QuicChromiumPacketWriter::IsWriteBlocked() const {
93 return write_blocked_; 85 return write_blocked_;
94 } 86 }
95 87
96 void QuicChromiumPacketWriter::SetWritable() { 88 void QuicChromiumPacketWriter::SetWritable() {
97 write_blocked_ = false; 89 write_blocked_ = false;
98 } 90 }
99 91
100 void QuicChromiumPacketWriter::OnWriteComplete(int rv) { 92 void QuicChromiumPacketWriter::OnWriteComplete(int rv) {
101 DCHECK_NE(rv, ERR_IO_PENDING); 93 DCHECK_NE(rv, ERR_IO_PENDING);
102 DCHECK(connection_) << "Uninitialized connection."; 94 DCHECK(delegate_) << "Uninitialized delegate.";
103 DCHECK(observer_) << "Uninitialized observer.";
104 write_blocked_ = false; 95 write_blocked_ = false;
105 if (rv < 0) { 96 if (rv < 0) {
106 // If write error, then call into the observer's OnWriteError, 97 // If write error, then call delegate's HandleWriteError, which
107 // which may be able to rewrite the packet on a new 98 // may be able to migrate and rewrite packet on a new socket.
108 // socket. OnWriteError returns the outcome of the attempt. 99 // HandleWriteError returns the outcome of that rewrite attempt.
109 rv = observer_->OnWriteError(rv, packet_); 100 rv = delegate_->HandleWriteError(rv, packet_);
110 packet_ = nullptr; 101 packet_ = nullptr;
111 if (rv == ERR_IO_PENDING) 102 if (rv == ERR_IO_PENDING)
112 return; 103 return;
113 } 104 }
114 105
115 if (rv < 0) { 106 if (rv < 0)
116 connection_->OnWriteError(rv); 107 delegate_->OnWriteError(rv);
117 } 108 else
118 connection_->OnCanWrite(); 109 delegate_->OnWriteUnblocked();
119 } 110 }
120 111
121 QuicByteCount QuicChromiumPacketWriter::GetMaxPacketSize( 112 QuicByteCount QuicChromiumPacketWriter::GetMaxPacketSize(
122 const IPEndPoint& peer_address) const { 113 const IPEndPoint& peer_address) const {
123 return kMaxPacketSize; 114 return kMaxPacketSize;
124 } 115 }
125 116
126 } // namespace net 117 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/chromium/quic_chromium_packet_writer.h ('k') | net/quic/chromium/quic_stream_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698