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. 4 5 #include "net/quic/quic_default_packet_writer.h" 6 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" 12 13 namespace net { 14 15 QuicDefaultPacketWriter::QuicDefaultPacketWriter() : weak_factory_(this) { 16 } 17 18 QuicDefaultPacketWriter::QuicDefaultPacketWriter(DatagramClientSocket* socket) 19 : weak_factory_(this), 20 socket_(socket) { 21 } 22 23 QuicDefaultPacketWriter::~QuicDefaultPacketWriter() {} 24 25 WriteResult QuicDefaultPacketWriter::WritePacket( 26 const char* buffer, size_t buf_len, 27 const net::IPAddressNumber& self_address, 28 const net::IPEndPoint& peer_address, 29 QuicBlockedWriterInterface* blocked_writer) { 30 scoped_refptr<StringIOBuffer> buf( 31 new StringIOBuffer(std::string(buffer, buf_len))); 32 int rv = socket_->Write(buf.get(), 33 buf_len, 34 base::Bind(&QuicDefaultPacketWriter::OnWriteComplete, 35 weak_factory_.GetWeakPtr())); 36 WriteStatus status = WRITE_STATUS_OK; 37 if (rv < 0) { 38 if (rv != ERR_IO_PENDING) { 39 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv); 40 status = WRITE_STATUS_ERROR; 41 } else { 42 status = WRITE_STATUS_BLOCKED; 43 } 44 } 45 46 return WriteResult(status, rv); 47 } 48 49 bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const { 50 // Chrome sockets' Write() methods buffer the data until the Write is 51 // permitted. 52 return true; 53 } 54 55 void QuicDefaultPacketWriter::OnWriteComplete(int rv) { 56 DCHECK_NE(rv, ERR_IO_PENDING); 57 WriteResult result(rv < 0 ? WRITE_STATUS_ERROR : WRITE_STATUS_OK, rv); 58 connection_->OnPacketSent(result); 59 connection_->OnCanWrite(); 60 } 61 62 } // namespace net 63