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_macros.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(Socket
* socket
)
20 : socket_(socket
), write_blocked_(false), weak_factory_(this) {}
22 QuicDefaultPacketWriter::~QuicDefaultPacketWriter() {}
24 WriteResult
QuicDefaultPacketWriter::WritePacket(
27 const IPAddressNumber
& self_address
,
28 const IPEndPoint
& peer_address
) {
29 scoped_refptr
<StringIOBuffer
> buf(
30 new StringIOBuffer(std::string(buffer
, buf_len
)));
31 DCHECK(!IsWriteBlocked());
32 base::TimeTicks now
= base::TimeTicks::Now();
33 int rv
= socket_
->Write(buf
.get(),
35 base::Bind(&QuicDefaultPacketWriter::OnWriteComplete
,
36 weak_factory_
.GetWeakPtr()));
37 WriteStatus status
= WRITE_STATUS_OK
;
39 if (rv
!= ERR_IO_PENDING
) {
40 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv
);
41 status
= WRITE_STATUS_ERROR
;
43 status
= WRITE_STATUS_BLOCKED
;
44 write_blocked_
= true;
48 base::TimeDelta delta
= base::TimeTicks::Now() - now
;
49 if (status
== WRITE_STATUS_OK
) {
50 UMA_HISTOGRAM_TIMES("Net.QuicSession.PacketWriteTime.Synchronous", delta
);
51 } else if (status
== WRITE_STATUS_BLOCKED
) {
52 UMA_HISTOGRAM_TIMES("Net.QuicSession.PacketWriteTime.Asynchronous", delta
);
55 return WriteResult(status
, rv
);
58 bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const {
59 // Chrome sockets' Write() methods buffer the data until the Write is
64 bool QuicDefaultPacketWriter::IsWriteBlocked() const {
65 return write_blocked_
;
68 void QuicDefaultPacketWriter::SetWritable() {
69 write_blocked_
= false;
72 void QuicDefaultPacketWriter::OnWriteComplete(int rv
) {
73 DCHECK_NE(rv
, ERR_IO_PENDING
);
74 write_blocked_
= false;
76 connection_
->OnWriteError(rv
);
78 connection_
->OnCanWrite();