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"
18 QuicSimpleServerPacketWriter::QuicSimpleServerPacketWriter(
19 UDPServerSocket
* socket
,
20 QuicBlockedWriterInterface
* blocked_writer
)
22 blocked_writer_(blocked_writer
),
23 write_blocked_(false),
27 QuicSimpleServerPacketWriter::~QuicSimpleServerPacketWriter() {
30 WriteResult
QuicSimpleServerPacketWriter::WritePacketWithCallback(
33 const IPAddressNumber
& self_address
,
34 const IPEndPoint
& peer_address
,
35 WriteCallback callback
) {
36 DCHECK(callback_
.is_null());
38 WriteResult result
= WritePacket(buffer
, buf_len
, self_address
, peer_address
);
39 if (result
.status
!= WRITE_STATUS_BLOCKED
) {
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.
58 bool QuicSimpleServerPacketWriter::IsWriteBlocked() const {
59 return write_blocked_
;
62 void QuicSimpleServerPacketWriter::SetWritable() {
63 write_blocked_
= false;
66 WriteResult
QuicSimpleServerPacketWriter::WritePacket(
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());
76 if (buf_len
<= static_cast<size_t>(std::numeric_limits
<int>::max())) {
79 static_cast<int>(buf_len
),
82 &QuicSimpleServerPacketWriter::OnWriteComplete
,
83 weak_factory_
.GetWeakPtr()));
87 WriteStatus status
= WRITE_STATUS_OK
;
89 if (rv
!= ERR_IO_PENDING
) {
90 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv
);
91 status
= WRITE_STATUS_ERROR
;
93 status
= WRITE_STATUS_BLOCKED
;
94 write_blocked_
= true;
97 return WriteResult(status
, rv
);