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/sparse_histogram.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
15 QuicDefaultPacketWriter::QuicDefaultPacketWriter() : weak_factory_(this) {
18 QuicDefaultPacketWriter::QuicDefaultPacketWriter(DatagramClientSocket
* socket
)
19 : weak_factory_(this),
21 write_blocked_(false) {
24 QuicDefaultPacketWriter::~QuicDefaultPacketWriter() {}
26 WriteResult
QuicDefaultPacketWriter::WritePacket(
27 const char* buffer
, size_t buf_len
,
28 const net::IPAddressNumber
& self_address
,
29 const net::IPEndPoint
& peer_address
,
30 QuicBlockedWriterInterface
* blocked_writer
) {
31 scoped_refptr
<StringIOBuffer
> buf(
32 new StringIOBuffer(std::string(buffer
, buf_len
)));
33 DCHECK(!IsWriteBlocked());
34 int rv
= socket_
->Write(buf
.get(),
36 base::Bind(&QuicDefaultPacketWriter::OnWriteComplete
,
37 weak_factory_
.GetWeakPtr()));
38 WriteStatus status
= WRITE_STATUS_OK
;
40 if (rv
!= ERR_IO_PENDING
) {
41 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv
);
42 status
= WRITE_STATUS_ERROR
;
44 status
= WRITE_STATUS_BLOCKED
;
45 write_blocked_
= true;
49 return WriteResult(status
, rv
);
52 bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const {
53 // Chrome sockets' Write() methods buffer the data until the Write is
58 bool QuicDefaultPacketWriter::IsWriteBlocked() const {
59 return write_blocked_
;
62 void QuicDefaultPacketWriter::SetWritable() {
63 write_blocked_
= false;
66 void QuicDefaultPacketWriter::OnWriteComplete(int rv
) {
67 DCHECK_NE(rv
, ERR_IO_PENDING
);
68 write_blocked_
= false;
69 WriteResult
result(rv
< 0 ? WRITE_STATUS_ERROR
: WRITE_STATUS_OK
, rv
);
70 connection_
->OnPacketSent(result
);
71 connection_
->OnCanWrite();