Chromium Code Reviews| 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/blimp_message_pump.h" | 5 #include "blimp/net/blimp_message_pump.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "blimp/common/proto/blimp_message.pb.h" | 8 #include "blimp/common/proto/blimp_message.pb.h" |
| 9 #include "blimp/net/blimp_message_processor.h" | 9 #include "blimp/net/blimp_message_processor.h" |
| 10 #include "blimp/net/common.h" | 10 #include "blimp/net/common.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 buffer_->SetCapacity(kMaxPacketPayloadSizeBytes); | 33 buffer_->SetCapacity(kMaxPacketPayloadSizeBytes); |
| 34 ReadNextPacket(); | 34 ReadNextPacket(); |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 void BlimpMessagePump::ReadNextPacket() { | 38 void BlimpMessagePump::ReadNextPacket() { |
| 39 DCHECK(processor_); | 39 DCHECK(processor_); |
| 40 buffer_->set_offset(0); | 40 buffer_->set_offset(0); |
| 41 read_packet_callback_.Reset(base::Bind( | 41 read_packet_callback_.Reset(base::Bind( |
| 42 &BlimpMessagePump::OnReadPacketComplete, base::Unretained(this))); | 42 &BlimpMessagePump::OnReadPacketComplete, base::Unretained(this))); |
| 43 int result = | 43 reader_->ReadPacket(buffer_.get(), read_packet_callback_.callback()); |
|
Wez
2015/11/30 21:33:24
See my other comment re cancellable callbacks.
Kevin M
2015/12/01 01:50:27
Done.
| |
| 44 reader_->ReadPacket(buffer_.get(), read_packet_callback_.callback()); | |
| 45 if (result != net::ERR_IO_PENDING) { | |
| 46 // Read completed synchronously. | |
| 47 OnReadPacketComplete(result); | |
| 48 } | |
| 49 } | 44 } |
| 50 | 45 |
| 51 void BlimpMessagePump::OnReadPacketComplete(int result) { | 46 void BlimpMessagePump::OnReadPacketComplete(int result) { |
| 52 if (result > 0) { | 47 if (result == net::OK) { |
| 53 // The result is the size of the packet in bytes. | |
| 54 scoped_ptr<BlimpMessage> message(new BlimpMessage); | 48 scoped_ptr<BlimpMessage> message(new BlimpMessage); |
| 55 bool parse_result = | 49 if (message->ParseFromArray(buffer_->StartOfBuffer(), buffer_->offset())) { |
| 56 message->ParseFromArray(buffer_->StartOfBuffer(), result); | |
| 57 if (parse_result) { | |
| 58 process_msg_callback_.Reset(base::Bind( | 50 process_msg_callback_.Reset(base::Bind( |
| 59 &BlimpMessagePump::OnProcessMessageComplete, base::Unretained(this))); | 51 &BlimpMessagePump::OnProcessMessageComplete, base::Unretained(this))); |
| 60 processor_->ProcessMessage(message.Pass(), | 52 processor_->ProcessMessage(message.Pass(), |
| 61 process_msg_callback_.callback()); | 53 process_msg_callback_.callback()); |
| 62 return; | 54 return; |
| 55 } else { | |
|
Wez
2015/11/30 21:33:24
Never use constructs of the form if (condition) {
Kevin M
2015/12/01 01:50:27
Done.
| |
| 56 result = net::ERR_FAILED; | |
| 63 } | 57 } |
| 64 result = net::ERR_FAILED; | |
| 65 } | 58 } |
| 59 | |
| 60 // Error reporting. | |
| 61 DCHECK_NE(net::OK, result); | |
| 66 if (error_observer_) | 62 if (error_observer_) |
| 67 error_observer_->OnConnectionError(result); | 63 error_observer_->OnConnectionError(result); |
| 68 } | 64 } |
| 69 | 65 |
| 70 void BlimpMessagePump::OnProcessMessageComplete(int result) { | 66 void BlimpMessagePump::OnProcessMessageComplete(int result) { |
| 71 // No error is expected from the message receiver. | 67 // No error is expected from the message receiver. |
| 72 DCHECK_EQ(result, net::OK); | 68 DCHECK_EQ(net::OK, result); |
| 73 ReadNextPacket(); | 69 ReadNextPacket(); |
| 74 } | 70 } |
| 75 | 71 |
| 76 } // namespace blimp | 72 } // namespace blimp |
| OLD | NEW |