Index: blimp/net/tcp_connection.cc |
diff --git a/blimp/net/blimp_connection.cc b/blimp/net/tcp_connection.cc |
similarity index 53% |
copy from blimp/net/blimp_connection.cc |
copy to blimp/net/tcp_connection.cc |
index 57a3580d74394de21c9ab30ecba1141a29171e29..e6ba35a70ae03d91214266f10a86c063f6c04762 100644 |
--- a/blimp/net/blimp_connection.cc |
+++ b/blimp/net/tcp_connection.cc |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "blimp/net/blimp_connection.h" |
+#include "blimp/net/tcp_connection.h" |
#include <utility> |
@@ -69,12 +69,13 @@ void BlimpMessageSender::ProcessMessage( |
DCHECK(error_observer_); |
VLOG(1) << "Sending " << *message; |
- if (message->ByteSize() > static_cast<int>(kMaxPacketPayloadSizeBytes)) { |
+ const int msg_byte_size = message->ByteSize(); |
Wez
2016/11/09 22:47:17
nit: Per style-guide, this should be message_bytes
|
+ if (msg_byte_size > static_cast<int>(kMaxPacketPayloadSizeBytes)) { |
DLOG(ERROR) << "Message rejected (too large): " << *message; |
callback.Run(net::ERR_MSG_TOO_BIG); |
return; |
} |
- if (!message->SerializeToArray(buffer_->data(), message->GetCachedSize())) { |
+ if (!message->SerializeToArray(buffer_->data(), msg_byte_size)) { |
DLOG(ERROR) << "Failed to serialize message."; |
callback.Run(net::ERR_INVALID_ARGUMENT); |
return; |
@@ -86,7 +87,7 @@ void BlimpMessageSender::ProcessMessage( |
writer_->WritePacket( |
scoped_refptr<net::DrainableIOBuffer>( |
- new net::DrainableIOBuffer(buffer_.get(), message->ByteSize())), |
+ new net::DrainableIOBuffer(buffer_.get(), msg_byte_size)), |
base::Bind(&BlimpMessageSender::OnWritePacketComplete, |
weak_factory_.GetWeakPtr())); |
} |
@@ -107,93 +108,28 @@ void BlimpMessageSender::OnWritePacketComplete(int result) { |
process_callback.Run(result); |
} |
-// MessageProcessor filter used to route EndConnection messages through to |
-// OnConnectionError notifications on the owning BlimpConnection. |
-class BlimpConnection::EndConnectionFilter : public BlimpMessageProcessor { |
- public: |
- explicit EndConnectionFilter(BlimpConnection* connection); |
- |
- void set_message_handler(BlimpMessageProcessor* message_handler) { |
- message_handler_ = message_handler; |
- } |
- |
- // BlimpMessageProcessor implementation. |
- void ProcessMessage(std::unique_ptr<BlimpMessage> message, |
- const net::CompletionCallback& callback) override; |
- |
- private: |
- // Owning BlimpConnection, on which to call OnConnectionError. |
- BlimpConnection* connection_; |
- |
- // Caller-provided message handler to forward non-EndConnection messages to. |
- BlimpMessageProcessor* message_handler_; |
- |
- DISALLOW_COPY_AND_ASSIGN(EndConnectionFilter); |
-}; |
- |
-BlimpConnection::EndConnectionFilter::EndConnectionFilter( |
- BlimpConnection* connection) |
- : connection_(connection), message_handler_(nullptr) {} |
- |
-void BlimpConnection::EndConnectionFilter::ProcessMessage( |
- std::unique_ptr<BlimpMessage> message, |
- const net::CompletionCallback& callback) { |
- if (message->has_protocol_control() && |
- message->protocol_control().has_end_connection()) { |
- // Report the EndConnection reason to connection error observers. |
- connection_->OnConnectionError( |
- message->protocol_control().end_connection().reason()); |
- |
- // Caller must ensure |callback| safe to call after OnConnectionError. |
- callback.Run(message->protocol_control().end_connection().reason()); |
- return; |
- } |
- |
- message_handler_->ProcessMessage(std::move(message), callback); |
-} |
- |
-BlimpConnection::BlimpConnection(std::unique_ptr<MessagePort> message_port) |
- : message_port_(std::move(message_port)), |
+TCPConnection::TCPConnection(std::unique_ptr<MessagePort> message_port) |
+ : BlimpConnection(), |
+ message_port_(std::move(message_port)), |
message_pump_(new BlimpMessagePump(message_port_->reader())), |
- outgoing_msg_processor_(new BlimpMessageSender(message_port_->writer())), |
- end_connection_filter_(new EndConnectionFilter(this)) { |
+ outgoing_msg_processor_(new BlimpMessageSender(message_port_->writer())) { |
message_pump_->set_error_observer(this); |
outgoing_msg_processor_->set_error_observer(this); |
} |
-BlimpConnection::BlimpConnection() {} |
- |
-BlimpConnection::~BlimpConnection() { |
- VLOG(1) << "BlimpConnection destroyed."; |
-} |
- |
-void BlimpConnection::AddConnectionErrorObserver( |
- ConnectionErrorObserver* observer) { |
- error_observers_.AddObserver(observer); |
-} |
- |
-void BlimpConnection::RemoveConnectionErrorObserver( |
- ConnectionErrorObserver* observer) { |
- error_observers_.RemoveObserver(observer); |
+TCPConnection::~TCPConnection() { |
+ VLOG(1) << "TCPConnection destroyed."; |
} |
-void BlimpConnection::SetIncomingMessageProcessor( |
+void TCPConnection::SetIncomingMessageProcessor( |
BlimpMessageProcessor* processor) { |
- end_connection_filter_->set_message_handler(processor); |
- message_pump_->SetMessageProcessor(processor ? end_connection_filter_.get() |
- : nullptr); |
+ AddEndConnectionProcessor(processor); |
+ message_pump_->SetMessageProcessor( |
+ (processor != nullptr) ? GetEndConnectionProcessor() : nullptr); |
Wez
2016/11/09 22:47:17
nit: You don't need the != nullptr here.
|
} |
-BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { |
+BlimpMessageProcessor* TCPConnection::GetOutgoingMessageProcessor() { |
return outgoing_msg_processor_.get(); |
} |
-void BlimpConnection::OnConnectionError(int error) { |
- VLOG(1) << "OnConnectionError, error=" << error; |
- |
- // Propagate the error to all observers. |
- for (auto& observer : error_observers_) |
- observer.OnConnectionError(error); |
-} |
- |
} // namespace blimp |