| 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/location.h" | 10 #include "base/location.h" | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 31       out << "PAYLOAD"; | 31       out << "PAYLOAD"; | 
| 32       break; | 32       break; | 
| 33     case StreamPacketReader::ReadState::IDLE: | 33     case StreamPacketReader::ReadState::IDLE: | 
| 34       out << "IDLE"; | 34       out << "IDLE"; | 
| 35       break; | 35       break; | 
| 36   } | 36   } | 
| 37   return out; | 37   return out; | 
| 38 } | 38 } | 
| 39 | 39 | 
| 40 StreamPacketReader::StreamPacketReader(net::StreamSocket* socket) | 40 StreamPacketReader::StreamPacketReader(net::StreamSocket* socket) | 
| 41     : read_state_(ReadState::IDLE), socket_(socket), weak_factory_(this) { | 41     : read_state_(ReadState::IDLE), | 
|  | 42       socket_(socket), | 
|  | 43       payload_size_(0), | 
|  | 44       weak_factory_(this) { | 
| 42   DCHECK(socket_); | 45   DCHECK(socket_); | 
| 43   header_buffer_ = new net::GrowableIOBuffer; | 46   header_buffer_ = new net::GrowableIOBuffer; | 
| 44   header_buffer_->SetCapacity(kPacketHeaderSizeBytes); | 47   header_buffer_->SetCapacity(kPacketHeaderSizeBytes); | 
| 45 } | 48 } | 
| 46 | 49 | 
| 47 StreamPacketReader::~StreamPacketReader() {} | 50 StreamPacketReader::~StreamPacketReader() {} | 
| 48 | 51 | 
| 49 void StreamPacketReader::ReadPacket( | 52 void StreamPacketReader::ReadPacket( | 
| 50     const scoped_refptr<net::GrowableIOBuffer>& buf, | 53     const scoped_refptr<net::GrowableIOBuffer>& buf, | 
| 51     const net::CompletionCallback& callback) { | 54     const net::CompletionCallback& callback) { | 
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 146 | 149 | 
| 147 void StreamPacketReader::OnReadComplete(int result) { | 150 void StreamPacketReader::OnReadComplete(int result) { | 
| 148   DCHECK_NE(net::ERR_IO_PENDING, result); | 151   DCHECK_NE(net::ERR_IO_PENDING, result); | 
| 149 | 152 | 
| 150   if (result == 0 /* EOF */) { | 153   if (result == 0 /* EOF */) { | 
| 151     payload_buffer_ = nullptr; | 154     payload_buffer_ = nullptr; | 
| 152     base::ResetAndReturn(&callback_).Run(net::ERR_CONNECTION_CLOSED); | 155     base::ResetAndReturn(&callback_).Run(net::ERR_CONNECTION_CLOSED); | 
| 153     return; | 156     return; | 
| 154   } | 157   } | 
| 155 | 158 | 
| 156   // If the read was succesful, then process the result. | 159   // If the read was successful, then process the result. | 
| 157   if (result > 0) { | 160   if (result > 0) { | 
| 158     result = DoReadLoop(result); | 161     result = DoReadLoop(result); | 
| 159   } | 162   } | 
| 160 | 163 | 
| 161   // If all reading completed, either successfully or by error, inform the | 164   // If all reading completed, either successfully or by error, inform the | 
| 162   // caller. | 165   // caller. | 
| 163   if (result != net::ERR_IO_PENDING) { | 166   if (result != net::ERR_IO_PENDING) { | 
| 164     payload_buffer_ = nullptr; | 167     payload_buffer_ = nullptr; | 
| 165     base::ResetAndReturn(&callback_).Run(result); | 168     base::ResetAndReturn(&callback_).Run(result); | 
| 166   } | 169   } | 
| 167 } | 170 } | 
| 168 | 171 | 
| 169 int StreamPacketReader::DoRead(net::IOBuffer* buf, int buf_len) { | 172 int StreamPacketReader::DoRead(net::IOBuffer* buf, int buf_len) { | 
| 170   int result = socket_->Read(buf, buf_len, | 173   int result = socket_->Read(buf, buf_len, | 
| 171                              base::Bind(&StreamPacketReader::OnReadComplete, | 174                              base::Bind(&StreamPacketReader::OnReadComplete, | 
| 172                                         weak_factory_.GetWeakPtr())); | 175                                         weak_factory_.GetWeakPtr())); | 
| 173   return (result != 0 ? result : net::ERR_CONNECTION_CLOSED); | 176   return (result != 0 ? result : net::ERR_CONNECTION_CLOSED); | 
| 174 } | 177 } | 
| 175 | 178 | 
| 176 }  // namespace blimp | 179 }  // namespace blimp | 
| OLD | NEW | 
|---|