Chromium Code Reviews| 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)); | |
| 58 } | 59 } |
| 59 | 60 |
| 60 write_state_ = WriteState::HEADER; | 61 write_state_ = WriteState::HEADER; |
| 61 header_buffer_->SetOffset(0); | 62 header_buffer_->SetOffset(0); |
| 62 *reinterpret_cast<uint32*>(header_buffer_->data()) = | 63 *reinterpret_cast<uint32*>(header_buffer_->data()) = |
| 63 base::HostToNet32(data->BytesRemaining()); | 64 base::HostToNet32(data->BytesRemaining()); |
| 64 payload_buffer_ = data; | 65 payload_buffer_ = data; |
| 65 | 66 |
| 66 int result = DoWriteLoop(false); | 67 int result = DoWriteLoop(false); |
|
Wez
2015/11/20 23:42:13
Why are we passing DoWriteLoop a bool here, when i
Kevin M
2015/11/24 21:34:36
Done.
| |
| 67 if (result == net::ERR_IO_PENDING) { | 68 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 | 69 // Release the payload buffer, since the write operation has completed |
| 73 // synchronously. | 70 // synchronously. |
|
Wez
2015/11/20 23:42:13
Not according to the result test above, it hasn't.
Kevin M
2015/11/24 21:34:36
Done. I must've been interrupted mid-CL, because t
| |
| 74 payload_buffer_ = nullptr; | 71 payload_buffer_ = nullptr; |
| 72 | |
| 73 // Adapt synchronous completion to an asynchronous style. | |
| 74 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 75 base::Bind(callback, result)); | |
| 76 } else { | |
| 77 callback_ = callback; | |
| 75 } | 78 } |
| 76 | |
| 77 return result; | |
| 78 } | 79 } |
| 79 | 80 |
| 80 int StreamPacketWriter::DoWriteLoop(int result) { | 81 int StreamPacketWriter::DoWriteLoop(int result) { |
| 81 DCHECK_NE(net::ERR_IO_PENDING, result); | 82 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 82 DCHECK_GE(result, 0); | 83 DCHECK_GE(result, 0); |
| 83 DCHECK_NE(WriteState::IDLE, write_state_); | 84 DCHECK_NE(WriteState::IDLE, write_state_); |
| 84 | 85 |
| 85 while (result >= 0 && write_state_ != WriteState::IDLE) { | 86 while (result >= 0 && write_state_ != WriteState::IDLE) { |
| 86 VLOG(2) << "DoWriteLoop (state=" << write_state_ << ", result=" << result | 87 VLOG(2) << "DoWriteLoop (state=" << write_state_ << ", result=" << result |
| 87 << ")"; | 88 << ")"; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 | 146 |
| 146 // If the write finished, either successfully or by error, inform the | 147 // If the write finished, either successfully or by error, inform the |
| 147 // caller. | 148 // caller. |
| 148 if (result != net::ERR_IO_PENDING) { | 149 if (result != net::ERR_IO_PENDING) { |
| 149 payload_buffer_ = nullptr; | 150 payload_buffer_ = nullptr; |
| 150 base::ResetAndReturn(&callback_).Run(result); | 151 base::ResetAndReturn(&callback_).Run(result); |
| 151 } | 152 } |
| 152 } | 153 } |
| 153 | 154 |
| 154 } // namespace blimp | 155 } // namespace blimp |
| OLD | NEW |