OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/net/stream_packet_writer.h" | 5 #include "blimp/net/stream_packet_writer.h" |
6 | 6 |
7 #include <iostream> | 7 #include <iostream> |
8 | 8 |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 socket_(socket), | 40 socket_(socket), |
41 header_buffer_( | 41 header_buffer_( |
42 new net::DrainableIOBuffer(new net::IOBuffer(kPacketHeaderSizeBytes), | 42 new net::DrainableIOBuffer(new net::IOBuffer(kPacketHeaderSizeBytes), |
43 kPacketHeaderSizeBytes)), | 43 kPacketHeaderSizeBytes)), |
44 weak_factory_(this) { | 44 weak_factory_(this) { |
45 DCHECK(socket_); | 45 DCHECK(socket_); |
46 } | 46 } |
47 | 47 |
48 StreamPacketWriter::~StreamPacketWriter() {} | 48 StreamPacketWriter::~StreamPacketWriter() {} |
49 | 49 |
50 int StreamPacketWriter::WritePacket(scoped_refptr<net::DrainableIOBuffer> data, | 50 void StreamPacketWriter::WritePacket(scoped_refptr<net::DrainableIOBuffer> data, |
51 const net::CompletionCallback& callback) { | 51 const net::CompletionCallback& callback) { |
52 DCHECK_EQ(WriteState::IDLE, write_state_); | 52 DCHECK_EQ(WriteState::IDLE, write_state_); |
53 DCHECK(data); | 53 DCHECK(data); |
54 if (data->BytesRemaining() == 0) { | 54 if (data->BytesRemaining() == 0) { |
55 // The packet is empty; your argument is invalid. | 55 // The packet is empty; your argument is invalid. |
56 DLOG(ERROR) << "Attempted to write zero-length packet."; | 56 DLOG(ERROR) << "Attempted to write zero-length packet."; |
57 return net::ERR_INVALID_ARGUMENT; | 57 base::MessageLoop::current()->PostTask( |
58 FROM_HERE, base::Bind(callback, net::ERR_INVALID_ARGUMENT)); | |
59 return; | |
Wez
2015/11/24 23:43:18
If we're mandating no zero-sized packets then writ
Kevin M
2015/11/25 01:18:37
Done.
| |
58 } | 60 } |
59 | 61 |
60 write_state_ = WriteState::HEADER; | 62 write_state_ = WriteState::HEADER; |
61 header_buffer_->SetOffset(0); | 63 header_buffer_->SetOffset(0); |
62 *reinterpret_cast<uint32*>(header_buffer_->data()) = | 64 *reinterpret_cast<uint32*>(header_buffer_->data()) = |
63 base::HostToNet32(data->BytesRemaining()); | 65 base::HostToNet32(data->BytesRemaining()); |
64 payload_buffer_ = data; | 66 payload_buffer_ = data; |
65 | 67 |
66 int result = DoWriteLoop(false); | 68 int result = DoWriteLoop(net::OK); |
67 if (result == net::ERR_IO_PENDING) { | 69 if (result != net::ERR_IO_PENDING) { |
68 // Store the completion callback to invoke when DoWriteLoop completes | |
69 // asynchronously. | |
70 callback_ = callback; | |
71 } else { | |
72 // Release the payload buffer, since the write operation has completed | 70 // Release the payload buffer, since the write operation has completed |
73 // synchronously. | 71 // synchronously. |
74 payload_buffer_ = nullptr; | 72 payload_buffer_ = nullptr; |
73 | |
74 // Adapt synchronous completion to an asynchronous style. | |
75 base::MessageLoop::current()->PostTask(FROM_HERE, | |
76 base::Bind(callback, result)); | |
77 } else { | |
78 callback_ = callback; | |
75 } | 79 } |
76 | |
77 return result; | |
78 } | 80 } |
79 | 81 |
80 int StreamPacketWriter::DoWriteLoop(int result) { | 82 int StreamPacketWriter::DoWriteLoop(int result) { |
81 DCHECK_NE(net::ERR_IO_PENDING, result); | 83 DCHECK_NE(net::ERR_IO_PENDING, result); |
82 DCHECK_GE(result, 0); | 84 DCHECK_GE(result, 0); |
83 DCHECK_NE(WriteState::IDLE, write_state_); | 85 DCHECK_NE(WriteState::IDLE, write_state_); |
84 | 86 |
85 while (result >= 0 && write_state_ != WriteState::IDLE) { | 87 while (result >= 0 && write_state_ != WriteState::IDLE) { |
86 VLOG(2) << "DoWriteLoop (state=" << write_state_ << ", result=" << result | 88 VLOG(2) << "DoWriteLoop (state=" << write_state_ << ", result=" << result |
87 << ")"; | 89 << ")"; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 | 147 |
146 // If the write finished, either successfully or by error, inform the | 148 // If the write finished, either successfully or by error, inform the |
147 // caller. | 149 // caller. |
148 if (result != net::ERR_IO_PENDING) { | 150 if (result != net::ERR_IO_PENDING) { |
149 payload_buffer_ = nullptr; | 151 payload_buffer_ = nullptr; |
150 base::ResetAndReturn(&callback_).Run(result); | 152 base::ResetAndReturn(&callback_).Run(result); |
151 } | 153 } |
152 } | 154 } |
153 | 155 |
154 } // namespace blimp | 156 } // namespace blimp |
OLD | NEW |