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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 96 |
97 int StreamPacketReader::DoReadHeader(int result) { | 97 int StreamPacketReader::DoReadHeader(int result) { |
98 DCHECK_EQ(ReadState::HEADER, read_state_); | 98 DCHECK_EQ(ReadState::HEADER, read_state_); |
99 DCHECK_GT(kPacketHeaderSizeBytes, | 99 DCHECK_GT(kPacketHeaderSizeBytes, |
100 static_cast<size_t>(header_buffer_->offset())); | 100 static_cast<size_t>(header_buffer_->offset())); |
101 DCHECK_GE(result, 0); | 101 DCHECK_GE(result, 0); |
102 | 102 |
103 header_buffer_->set_offset(header_buffer_->offset() + result); | 103 header_buffer_->set_offset(header_buffer_->offset() + result); |
104 if (static_cast<size_t>(header_buffer_->offset()) < kPacketHeaderSizeBytes) { | 104 if (static_cast<size_t>(header_buffer_->offset()) < kPacketHeaderSizeBytes) { |
105 // There is more header to read. | 105 // There is more header to read. |
106 return socket_->Read(header_buffer_.get(), | 106 int result = socket_->Read( |
107 kPacketHeaderSizeBytes - header_buffer_->offset(), | 107 header_buffer_.get(), kPacketHeaderSizeBytes - header_buffer_->offset(), |
108 base::Bind(&StreamPacketReader::OnReadComplete, | 108 base::Bind(&StreamPacketReader::OnReadComplete, |
109 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr())); |
| 110 return (result != 0 ? result : net::ERR_CONNECTION_CLOSED); |
110 } | 111 } |
111 | 112 |
112 // Finished reading the header. Parse the size and prepare for payload read. | 113 // Finished reading the header. Parse the size and prepare for payload read. |
113 payload_size_ = base::NetToHost32( | 114 payload_size_ = base::NetToHost32( |
114 *reinterpret_cast<uint32_t*>(header_buffer_->StartOfBuffer())); | 115 *reinterpret_cast<uint32_t*>(header_buffer_->StartOfBuffer())); |
115 if (payload_size_ > static_cast<size_t>(payload_buffer_->capacity()) || | 116 if (payload_size_ > static_cast<size_t>(payload_buffer_->capacity()) || |
116 payload_size_ == 0) { | 117 payload_size_ == 0) { |
117 DLOG(ERROR) << "Illegal payload size: " << payload_size_; | 118 DLOG(ERROR) << "Illegal payload size: " << payload_size_; |
118 return net::ERR_INVALID_RESPONSE; | 119 return net::ERR_INVALID_RESPONSE; |
119 } | 120 } |
(...skipping 14 matching lines...) Expand all Loading... |
134 } | 135 } |
135 | 136 |
136 // Finished reading the payload. | 137 // Finished reading the payload. |
137 read_state_ = ReadState::IDLE; | 138 read_state_ = ReadState::IDLE; |
138 return net::OK; | 139 return net::OK; |
139 } | 140 } |
140 | 141 |
141 void StreamPacketReader::OnReadComplete(int result) { | 142 void StreamPacketReader::OnReadComplete(int result) { |
142 DCHECK_NE(net::ERR_IO_PENDING, result); | 143 DCHECK_NE(net::ERR_IO_PENDING, result); |
143 | 144 |
| 145 if (result == 0 /* EOF */) { |
| 146 payload_buffer_ = nullptr; |
| 147 base::ResetAndReturn(&callback_).Run(net::ERR_CONNECTION_CLOSED); |
| 148 return; |
| 149 } |
| 150 |
144 // If the read was succesful, then process the result. | 151 // If the read was succesful, then process the result. |
145 if (result > 0) { | 152 if (result > 0) { |
146 result = DoReadLoop(result); | 153 result = DoReadLoop(result); |
147 } | 154 } |
148 | 155 |
149 // If all reading completed, either successfully or by error, inform the | 156 // If all reading completed, either successfully or by error, inform the |
150 // caller. | 157 // caller. |
151 if (result != net::ERR_IO_PENDING) { | 158 if (result != net::ERR_IO_PENDING) { |
152 payload_buffer_ = nullptr; | 159 payload_buffer_ = nullptr; |
153 base::ResetAndReturn(&callback_).Run(result); | 160 base::ResetAndReturn(&callback_).Run(result); |
154 } | 161 } |
155 } | 162 } |
156 | 163 |
157 } // namespace blimp | 164 } // namespace blimp |
OLD | NEW |