Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: blimp/net/stream_packet_writer.cc

Issue 1452823011: Make PacketReader/PacketWriter interfaces async-only. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address wez feedback Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698