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_connection.h" | 5 #include "blimp/net/blimp_connection.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "blimp/common/logging.h" | 12 #include "blimp/common/logging.h" |
| 13 #include "blimp/common/proto/blimp_message.pb.h" | 13 #include "blimp/common/proto/blimp_message.pb.h" |
| 14 #include "blimp/net/blimp_connection_statistics.h" | |
| 14 #include "blimp/net/blimp_message_processor.h" | 15 #include "blimp/net/blimp_message_processor.h" |
| 15 #include "blimp/net/blimp_message_pump.h" | 16 #include "blimp/net/blimp_message_pump.h" |
| 16 #include "blimp/net/common.h" | 17 #include "blimp/net/common.h" |
| 17 #include "blimp/net/connection_error_observer.h" | 18 #include "blimp/net/connection_error_observer.h" |
| 18 #include "blimp/net/packet_reader.h" | 19 #include "blimp/net/packet_reader.h" |
| 19 #include "blimp/net/packet_writer.h" | 20 #include "blimp/net/packet_writer.h" |
| 20 #include "net/base/completion_callback.h" | 21 #include "net/base/completion_callback.h" |
| 21 | 22 |
| 22 namespace blimp { | 23 namespace blimp { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Forwards incoming blimp messages to PacketWriter. | 26 // Forwards incoming blimp messages to PacketWriter. |
| 26 class BlimpMessageSender : public BlimpMessageProcessor { | 27 class BlimpMessageSender : public BlimpMessageProcessor { |
| 27 public: | 28 public: |
| 28 explicit BlimpMessageSender(PacketWriter* writer); | 29 explicit BlimpMessageSender(PacketWriter* writer); |
| 29 ~BlimpMessageSender() override; | 30 ~BlimpMessageSender() override; |
| 30 | 31 |
| 31 void set_error_observer(ConnectionErrorObserver* observer) { | 32 void set_error_observer(ConnectionErrorObserver* observer) { |
| 32 error_observer_ = observer; | 33 error_observer_ = observer; |
| 33 } | 34 } |
| 34 | 35 |
| 35 // BlimpMessageProcessor implementation. | 36 // BlimpMessageProcessor implementation. |
| 36 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | 37 void ProcessMessage(std::unique_ptr<BlimpMessage> message, |
| 37 const net::CompletionCallback& callback) override; | 38 const net::CompletionCallback& callback) override; |
| 38 | 39 |
| 40 void SetBlimpConnectionStatistics(BlimpConnectionStatistics* statistics) { | |
| 41 DCHECK(writer_); | |
|
Kevin M
2016/05/24 01:02:01
Not needed - the constructor guarantees that write
shaktisahu
2016/05/24 21:02:44
Removed function since we are passing in construct
| |
| 42 writer_->set_blimp_connection_statistics(statistics); | |
| 43 } | |
| 44 | |
| 39 private: | 45 private: |
| 40 void OnWritePacketComplete(int result); | 46 void OnWritePacketComplete(int result); |
| 41 | 47 |
| 42 PacketWriter* writer_; | 48 PacketWriter* writer_; |
| 43 ConnectionErrorObserver* error_observer_ = nullptr; | 49 ConnectionErrorObserver* error_observer_ = nullptr; |
| 44 scoped_refptr<net::IOBuffer> buffer_; | 50 scoped_refptr<net::IOBuffer> buffer_; |
| 45 net::CompletionCallback pending_process_msg_callback_; | 51 net::CompletionCallback pending_process_msg_callback_; |
| 46 base::WeakPtrFactory<BlimpMessageSender> weak_factory_; | 52 base::WeakPtrFactory<BlimpMessageSender> weak_factory_; |
| 47 | 53 |
| 48 DISALLOW_COPY_AND_ASSIGN(BlimpMessageSender); | 54 DISALLOW_COPY_AND_ASSIGN(BlimpMessageSender); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 : reader_(std::move(reader)), | 110 : reader_(std::move(reader)), |
| 105 message_pump_(new BlimpMessagePump(reader_.get())), | 111 message_pump_(new BlimpMessagePump(reader_.get())), |
| 106 writer_(std::move(writer)), | 112 writer_(std::move(writer)), |
| 107 outgoing_msg_processor_(new BlimpMessageSender(writer_.get())) { | 113 outgoing_msg_processor_(new BlimpMessageSender(writer_.get())) { |
| 108 DCHECK(writer_); | 114 DCHECK(writer_); |
| 109 | 115 |
| 110 // Observe the connection errors received by any of this connection's network | 116 // Observe the connection errors received by any of this connection's network |
| 111 // objects. | 117 // objects. |
| 112 message_pump_->set_error_observer(this); | 118 message_pump_->set_error_observer(this); |
| 113 BlimpMessageSender* sender = | 119 BlimpMessageSender* sender = |
| 114 static_cast<BlimpMessageSender*>(outgoing_msg_processor_.get()); | 120 static_cast<BlimpMessageSender*>(outgoing_msg_processor_.get()); |
|
Kevin M
2016/05/24 01:02:01
You can remove this cast too, if we make outgoing_
| |
| 115 sender->set_error_observer(this); | 121 sender->set_error_observer(this); |
| 116 } | 122 } |
| 117 | 123 |
| 118 BlimpConnection::BlimpConnection() {} | 124 BlimpConnection::BlimpConnection() {} |
| 119 | 125 |
| 120 BlimpConnection::~BlimpConnection() { | 126 BlimpConnection::~BlimpConnection() { |
| 121 VLOG(1) << "BlimpConnection destroyed."; | 127 VLOG(1) << "BlimpConnection destroyed."; |
| 122 } | 128 } |
| 123 | 129 |
| 124 void BlimpConnection::AddConnectionErrorObserver( | 130 void BlimpConnection::AddConnectionErrorObserver( |
| 125 ConnectionErrorObserver* observer) { | 131 ConnectionErrorObserver* observer) { |
| 126 error_observers_.AddObserver(observer); | 132 error_observers_.AddObserver(observer); |
| 127 } | 133 } |
| 128 | 134 |
| 129 void BlimpConnection::RemoveConnectionErrorObserver( | 135 void BlimpConnection::RemoveConnectionErrorObserver( |
| 130 ConnectionErrorObserver* observer) { | 136 ConnectionErrorObserver* observer) { |
| 131 error_observers_.RemoveObserver(observer); | 137 error_observers_.RemoveObserver(observer); |
| 132 } | 138 } |
| 133 | 139 |
| 134 void BlimpConnection::SetIncomingMessageProcessor( | 140 void BlimpConnection::SetIncomingMessageProcessor( |
| 135 BlimpMessageProcessor* processor) { | 141 BlimpMessageProcessor* processor) { |
| 136 message_pump_->SetMessageProcessor(processor); | 142 message_pump_->SetMessageProcessor(processor); |
| 137 } | 143 } |
| 138 | 144 |
| 145 void BlimpConnection::SetBlimpConnectionStatistics( | |
|
Kevin M
2016/05/24 01:02:01
We should be setting this at stream creation time,
| |
| 146 BlimpConnectionStatistics* statistics) { | |
| 147 message_pump_->SetBlimpConnectionStatistics(statistics); | |
| 148 BlimpMessageSender* sender = | |
| 149 static_cast<BlimpMessageSender*>(outgoing_msg_processor_.get()); | |
|
Kevin M
2016/05/24 01:02:01
See previous comment about making outgoing_msg_pro
| |
| 150 sender->SetBlimpConnectionStatistics(statistics); | |
| 151 } | |
| 152 | |
| 139 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { | 153 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { |
| 140 return outgoing_msg_processor_.get(); | 154 return outgoing_msg_processor_.get(); |
| 141 } | 155 } |
| 142 | 156 |
| 143 void BlimpConnection::OnConnectionError(int error) { | 157 void BlimpConnection::OnConnectionError(int error) { |
| 144 VLOG(1) << "OnConnectionError, error=" << error; | 158 VLOG(1) << "OnConnectionError, error=" << error; |
| 145 | 159 |
| 146 // Propagate the error to all observers. | 160 // Propagate the error to all observers. |
| 147 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, | 161 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, |
| 148 OnConnectionError(error)); | 162 OnConnectionError(error)); |
| 149 } | 163 } |
| 150 | 164 |
| 151 } // namespace blimp | 165 } // namespace blimp |
| OLD | NEW |