Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / tools / quic / quic_simple_server_packet_writer.cc
blobaaa8689f8dc90381b31803d91b551a9d01a0a235
1 // Copyright 2014 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/tools/quic/quic_simple_server_packet_writer.h"
7 #include "base/callback_helpers.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/udp/udp_server_socket.h"
15 namespace net {
16 namespace tools {
18 QuicSimpleServerPacketWriter::QuicSimpleServerPacketWriter(
19 UDPServerSocket* socket,
20 QuicBlockedWriterInterface* blocked_writer)
21 : socket_(socket),
22 blocked_writer_(blocked_writer),
23 write_blocked_(false),
24 weak_factory_(this) {
27 QuicSimpleServerPacketWriter::~QuicSimpleServerPacketWriter() {
30 WriteResult QuicSimpleServerPacketWriter::WritePacketWithCallback(
31 const char* buffer,
32 size_t buf_len,
33 const IPAddressNumber& self_address,
34 const IPEndPoint& peer_address,
35 WriteCallback callback) {
36 DCHECK(callback_.is_null());
37 callback_ = callback;
38 WriteResult result = WritePacket(buffer, buf_len, self_address, peer_address);
39 if (result.status != WRITE_STATUS_BLOCKED) {
40 callback_.Reset();
42 return result;
45 void QuicSimpleServerPacketWriter::OnWriteComplete(int rv) {
46 DCHECK_NE(rv, ERR_IO_PENDING);
47 write_blocked_ = false;
48 WriteResult result(rv < 0 ? WRITE_STATUS_ERROR : WRITE_STATUS_OK, rv);
49 base::ResetAndReturn(&callback_).Run(result);
50 blocked_writer_->OnCanWrite();
53 bool QuicSimpleServerPacketWriter::IsWriteBlockedDataBuffered() const {
54 // UDPServerSocket::SendTo buffers the data until the Write is permitted.
55 return true;
58 bool QuicSimpleServerPacketWriter::IsWriteBlocked() const {
59 return write_blocked_;
62 void QuicSimpleServerPacketWriter::SetWritable() {
63 write_blocked_ = false;
66 WriteResult QuicSimpleServerPacketWriter::WritePacket(
67 const char* buffer,
68 size_t buf_len,
69 const IPAddressNumber& self_address,
70 const IPEndPoint& peer_address) {
71 scoped_refptr<StringIOBuffer> buf(
72 new StringIOBuffer(std::string(buffer, buf_len)));
73 DCHECK(!IsWriteBlocked());
74 DCHECK(!callback_.is_null());
75 int rv;
76 if (buf_len <= static_cast<size_t>(std::numeric_limits<int>::max())) {
77 rv = socket_->SendTo(
78 buf.get(),
79 static_cast<int>(buf_len),
80 peer_address,
81 base::Bind(
82 &QuicSimpleServerPacketWriter::OnWriteComplete,
83 weak_factory_.GetWeakPtr()));
84 } else {
85 rv = ERR_MSG_TOO_BIG;
87 WriteStatus status = WRITE_STATUS_OK;
88 if (rv < 0) {
89 if (rv != ERR_IO_PENDING) {
90 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv);
91 status = WRITE_STATUS_ERROR;
92 } else {
93 status = WRITE_STATUS_BLOCKED;
94 write_blocked_ = true;
97 return WriteResult(status, rv);
100 } // namespace tools
101 } // namespace net