Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_default_packet_writer.cc
blob5dd80c6fd3dfd25c72e37aff5f5cac309d82c618
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"
13 namespace net {
15 QuicDefaultPacketWriter::QuicDefaultPacketWriter() : weak_factory_(this) {
18 QuicDefaultPacketWriter::QuicDefaultPacketWriter(DatagramClientSocket* socket)
19 : weak_factory_(this),
20 socket_(socket),
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 scoped_refptr<StringIOBuffer> buf(
31 new StringIOBuffer(std::string(buffer, buf_len)));
32 DCHECK(!IsWriteBlocked());
33 int rv = socket_->Write(buf.get(),
34 buf_len,
35 base::Bind(&QuicDefaultPacketWriter::OnWriteComplete,
36 weak_factory_.GetWeakPtr()));
37 WriteStatus status = WRITE_STATUS_OK;
38 if (rv < 0) {
39 if (rv != ERR_IO_PENDING) {
40 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv);
41 status = WRITE_STATUS_ERROR;
42 } else {
43 status = WRITE_STATUS_BLOCKED;
44 write_blocked_ = true;
48 return WriteResult(status, rv);
51 bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const {
52 // Chrome sockets' Write() methods buffer the data until the Write is
53 // permitted.
54 return true;
57 bool QuicDefaultPacketWriter::IsWriteBlocked() const {
58 return write_blocked_;
61 void QuicDefaultPacketWriter::SetWritable() {
62 write_blocked_ = false;
65 void QuicDefaultPacketWriter::OnWriteComplete(int rv) {
66 DCHECK_NE(rv, ERR_IO_PENDING);
67 write_blocked_ = false;
68 WriteResult result(rv < 0 ? WRITE_STATUS_ERROR : WRITE_STATUS_OK, rv);
69 connection_->OnPacketSent(result);
70 connection_->OnCanWrite();
73 } // namespace net