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_reader.h" | 5 #include "blimp/net/stream_packet_reader.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 header_buffer_ = new net::GrowableIOBuffer; | 40 header_buffer_ = new net::GrowableIOBuffer; |
41 header_buffer_->SetCapacity(kPacketHeaderSizeBytes); | 41 header_buffer_->SetCapacity(kPacketHeaderSizeBytes); |
42 } | 42 } |
43 | 43 |
44 StreamPacketReader::~StreamPacketReader() {} | 44 StreamPacketReader::~StreamPacketReader() {} |
45 | 45 |
46 void StreamPacketReader::ReadPacket( | 46 void StreamPacketReader::ReadPacket( |
47 const scoped_refptr<net::GrowableIOBuffer>& buf, | 47 const scoped_refptr<net::GrowableIOBuffer>& buf, |
48 const net::CompletionCallback& callback) { | 48 const net::CompletionCallback& callback) { |
49 DCHECK_EQ(ReadState::IDLE, read_state_); | 49 DCHECK_EQ(ReadState::IDLE, read_state_); |
50 DCHECK_GT(buf->capacity(), 0); | 50 if (static_cast<size_t>(buf->capacity()) < kPacketHeaderSizeBytes) { |
| 51 buf->SetCapacity(kPacketHeaderSizeBytes); |
| 52 } |
51 | 53 |
52 header_buffer_->set_offset(0); | 54 header_buffer_->set_offset(0); |
53 payload_buffer_ = buf; | 55 payload_buffer_ = buf; |
54 payload_buffer_->set_offset(0); | 56 payload_buffer_->set_offset(0); |
55 read_state_ = ReadState::HEADER; | 57 read_state_ = ReadState::HEADER; |
56 | 58 |
57 int result = DoReadLoop(net::OK); | 59 int result = DoReadLoop(net::OK); |
58 if (result != net::ERR_IO_PENDING) { | 60 if (result != net::ERR_IO_PENDING) { |
59 // Release the payload buffer, since the read operation has completed | 61 // Release the payload buffer, since the read operation has completed |
60 // synchronously. | 62 // synchronously. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 int result = socket_->Read( | 109 int result = socket_->Read( |
108 header_buffer_.get(), kPacketHeaderSizeBytes - header_buffer_->offset(), | 110 header_buffer_.get(), kPacketHeaderSizeBytes - header_buffer_->offset(), |
109 base::Bind(&StreamPacketReader::OnReadComplete, | 111 base::Bind(&StreamPacketReader::OnReadComplete, |
110 weak_factory_.GetWeakPtr())); | 112 weak_factory_.GetWeakPtr())); |
111 return (result != 0 ? result : net::ERR_CONNECTION_CLOSED); | 113 return (result != 0 ? result : net::ERR_CONNECTION_CLOSED); |
112 } | 114 } |
113 | 115 |
114 // Finished reading the header. Parse the size and prepare for payload read. | 116 // Finished reading the header. Parse the size and prepare for payload read. |
115 payload_size_ = base::NetToHost32( | 117 payload_size_ = base::NetToHost32( |
116 *reinterpret_cast<uint32_t*>(header_buffer_->StartOfBuffer())); | 118 *reinterpret_cast<uint32_t*>(header_buffer_->StartOfBuffer())); |
117 if (payload_size_ > static_cast<size_t>(payload_buffer_->capacity()) || | 119 if (payload_size_ == 0 || payload_size_ > kMaxPacketPayloadSizeBytes) { |
118 payload_size_ == 0) { | |
119 DLOG(ERROR) << "Illegal payload size: " << payload_size_; | 120 DLOG(ERROR) << "Illegal payload size: " << payload_size_; |
120 return net::ERR_INVALID_RESPONSE; | 121 return net::ERR_INVALID_RESPONSE; |
121 } | 122 } |
| 123 if (static_cast<size_t>(payload_buffer_->capacity()) < payload_size_) { |
| 124 payload_buffer_->SetCapacity(payload_size_); |
| 125 } |
122 read_state_ = ReadState::PAYLOAD; | 126 read_state_ = ReadState::PAYLOAD; |
123 return net::OK; | 127 return net::OK; |
124 } | 128 } |
125 | 129 |
126 int StreamPacketReader::DoReadPayload(int result) { | 130 int StreamPacketReader::DoReadPayload(int result) { |
127 DCHECK_EQ(ReadState::PAYLOAD, read_state_); | 131 DCHECK_EQ(ReadState::PAYLOAD, read_state_); |
128 DCHECK_GE(result, 0); | 132 DCHECK_GE(result, 0); |
129 | 133 |
130 payload_buffer_->set_offset(payload_buffer_->offset() + result); | 134 payload_buffer_->set_offset(payload_buffer_->offset() + result); |
131 if (static_cast<size_t>(payload_buffer_->offset()) < payload_size_) { | 135 if (static_cast<size_t>(payload_buffer_->offset()) < payload_size_) { |
(...skipping 26 matching lines...) Expand all Loading... |
158 // If all reading completed, either successfully or by error, inform the | 162 // If all reading completed, either successfully or by error, inform the |
159 // caller. | 163 // caller. |
160 if (result != net::ERR_IO_PENDING) { | 164 if (result != net::ERR_IO_PENDING) { |
161 payload_buffer_ = nullptr; | 165 payload_buffer_ = nullptr; |
162 base::ResetAndReturn(&callback_) | 166 base::ResetAndReturn(&callback_) |
163 .Run(result == net::OK ? payload_size_ : result); | 167 .Run(result == net::OK ? payload_size_ : result); |
164 } | 168 } |
165 } | 169 } |
166 | 170 |
167 } // namespace blimp | 171 } // namespace blimp |
OLD | NEW |