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
3 // found in the LICENSE file.
5 #include "net/quic/quic_default_packet_writer.h"
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
16 QuicDefaultPacketWriter::QuicDefaultPacketWriter() : weak_factory_(this) {
19 QuicDefaultPacketWriter::QuicDefaultPacketWriter(DatagramClientSocket
* socket
)
21 write_blocked_(false),
25 QuicDefaultPacketWriter::~QuicDefaultPacketWriter() {}
27 WriteResult
QuicDefaultPacketWriter::WritePacket(
30 const net::IPAddressNumber
& self_address
,
31 const net::IPEndPoint
& peer_address
) {
32 scoped_refptr
<StringIOBuffer
> buf(
33 new StringIOBuffer(std::string(buffer
, buf_len
)));
34 DCHECK(!IsWriteBlocked());
35 base::TimeTicks now
= base::TimeTicks::Now();
36 int rv
= socket_
->Write(buf
.get(),
38 base::Bind(&QuicDefaultPacketWriter::OnWriteComplete
,
39 weak_factory_
.GetWeakPtr()));
40 WriteStatus status
= WRITE_STATUS_OK
;
42 if (rv
!= ERR_IO_PENDING
) {
43 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv
);
44 status
= WRITE_STATUS_ERROR
;
46 status
= WRITE_STATUS_BLOCKED
;
47 write_blocked_
= true;
51 base::TimeDelta delta
= base::TimeTicks::Now() - now
;
52 if (status
== WRITE_STATUS_OK
) {
53 UMA_HISTOGRAM_TIMES("Net.QuicSession.PacketWriteTime.Synchronous", delta
);
54 } else if (status
== WRITE_STATUS_BLOCKED
) {
55 UMA_HISTOGRAM_TIMES("Net.QuicSession.PacketWriteTime.Asynchronous", delta
);
58 return WriteResult(status
, rv
);
61 bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const {
62 // Chrome sockets' Write() methods buffer the data until the Write is
67 bool QuicDefaultPacketWriter::IsWriteBlocked() const {
68 return write_blocked_
;
71 void QuicDefaultPacketWriter::SetWritable() {
72 write_blocked_
= false;
75 void QuicDefaultPacketWriter::OnWriteComplete(int rv
) {
76 DCHECK_NE(rv
, ERR_IO_PENDING
);
77 write_blocked_
= false;
79 connection_
->OnWriteError(rv
);
81 connection_
->OnCanWrite();