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 DCHECK(data->BytesRemaining()); |
Wez
2015/11/25 02:21:28
CHECK(data->BytesRemaining()), so that release bui
Kevin M
2015/11/30 19:25:14
Done.
| |
55 // The packet is empty; your argument is invalid. | |
56 DLOG(ERROR) << "Attempted to write zero-length packet."; | |
57 return net::ERR_INVALID_ARGUMENT; | |
58 } | |
59 | 55 |
60 write_state_ = WriteState::HEADER; | 56 write_state_ = WriteState::HEADER; |
61 header_buffer_->SetOffset(0); | 57 header_buffer_->SetOffset(0); |
62 *reinterpret_cast<uint32*>(header_buffer_->data()) = | 58 *reinterpret_cast<uint32*>(header_buffer_->data()) = |
63 base::HostToNet32(data->BytesRemaining()); | 59 base::HostToNet32(data->BytesRemaining()); |
64 payload_buffer_ = data; | 60 payload_buffer_ = data; |
65 | 61 |
66 int result = DoWriteLoop(false); | 62 int result = DoWriteLoop(net::OK); |
67 if (result == net::ERR_IO_PENDING) { | 63 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 | 64 // Release the payload buffer, since the write operation has completed |
73 // synchronously. | 65 // synchronously. |
74 payload_buffer_ = nullptr; | 66 payload_buffer_ = nullptr; |
67 | |
68 // Adapt synchronous completion to an asynchronous style. | |
69 base::MessageLoop::current()->PostTask(FROM_HERE, | |
70 base::Bind(callback, result)); | |
71 } else { | |
72 callback_ = callback; | |
75 } | 73 } |
76 | |
77 return result; | |
78 } | 74 } |
79 | 75 |
80 int StreamPacketWriter::DoWriteLoop(int result) { | 76 int StreamPacketWriter::DoWriteLoop(int result) { |
81 DCHECK_NE(net::ERR_IO_PENDING, result); | 77 DCHECK_NE(net::ERR_IO_PENDING, result); |
82 DCHECK_GE(result, 0); | 78 DCHECK_GE(result, 0); |
83 DCHECK_NE(WriteState::IDLE, write_state_); | 79 DCHECK_NE(WriteState::IDLE, write_state_); |
84 | 80 |
85 while (result >= 0 && write_state_ != WriteState::IDLE) { | 81 while (result >= 0 && write_state_ != WriteState::IDLE) { |
86 VLOG(2) << "DoWriteLoop (state=" << write_state_ << ", result=" << result | 82 VLOG(2) << "DoWriteLoop (state=" << write_state_ << ", result=" << result |
87 << ")"; | 83 << ")"; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 | 141 |
146 // If the write finished, either successfully or by error, inform the | 142 // If the write finished, either successfully or by error, inform the |
147 // caller. | 143 // caller. |
148 if (result != net::ERR_IO_PENDING) { | 144 if (result != net::ERR_IO_PENDING) { |
149 payload_buffer_ = nullptr; | 145 payload_buffer_ = nullptr; |
150 base::ResetAndReturn(&callback_).Run(result); | 146 base::ResetAndReturn(&callback_).Run(result); |
151 } | 147 } |
152 } | 148 } |
153 | 149 |
154 } // namespace blimp | 150 } // namespace blimp |
OLD | NEW |