| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/protocol/channel_dispatcher_base.h" | 5 #include "remoting/protocol/channel_dispatcher_base.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "remoting/protocol/p2p_stream_socket.h" | 10 #include "remoting/protocol/p2p_stream_socket.h" |
| 11 #include "remoting/protocol/stream_channel_factory.h" | 11 #include "remoting/protocol/stream_channel_factory.h" |
| 12 #include "remoting/protocol/stream_message_pipe_adapter.h" |
| 12 | 13 |
| 13 namespace remoting { | 14 namespace remoting { |
| 14 namespace protocol { | 15 namespace protocol { |
| 15 | 16 |
| 16 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name) | 17 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name) |
| 17 : channel_name_(channel_name), | 18 : channel_name_(channel_name), |
| 18 channel_factory_(nullptr), | 19 channel_factory_(nullptr), |
| 19 event_handler_(nullptr) { | 20 event_handler_(nullptr) {} |
| 20 } | |
| 21 | 21 |
| 22 ChannelDispatcherBase::~ChannelDispatcherBase() { | 22 ChannelDispatcherBase::~ChannelDispatcherBase() { |
| 23 if (channel_factory_) | 23 if (channel_factory_) |
| 24 channel_factory_->CancelChannelCreation(channel_name_); | 24 channel_factory_->CancelChannelCreation(channel_name_); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void ChannelDispatcherBase::Init(StreamChannelFactory* channel_factory, | 27 void ChannelDispatcherBase::Init(StreamChannelFactory* channel_factory, |
| 28 EventHandler* event_handler) { | 28 EventHandler* event_handler) { |
| 29 channel_factory_ = channel_factory; | 29 channel_factory_ = channel_factory; |
| 30 event_handler_ = event_handler; | 30 event_handler_ = event_handler; |
| 31 | 31 |
| 32 channel_factory_->CreateChannel(channel_name_, base::Bind( | 32 channel_factory_->CreateChannel(channel_name_, base::Bind( |
| 33 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this))); | 33 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this))); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void ChannelDispatcherBase::OnChannelReady( | 36 void ChannelDispatcherBase::OnChannelReady( |
| 37 scoped_ptr<P2PStreamSocket> socket) { | 37 scoped_ptr<P2PStreamSocket> socket) { |
| 38 if (!socket.get()) { | 38 if (!socket.get()) { |
| 39 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR); | 39 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR); |
| 40 return; | 40 return; |
| 41 } | 41 } |
| 42 | 42 |
| 43 channel_factory_ = nullptr; | 43 channel_factory_ = nullptr; |
| 44 channel_ = std::move(socket); | 44 message_pipe_.reset(new StreamMessagePipeAdapter( |
| 45 writer_.Start( | 45 std::move(socket), |
| 46 base::Bind(&P2PStreamSocket::Write, base::Unretained(channel_.get())), | 46 base::Bind(&ChannelDispatcherBase::OnPipeError, base::Unretained(this)))); |
| 47 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed, | 47 message_pipe_->StartReceiving(base::Bind( |
| 48 base::Unretained(this))); | 48 &ChannelDispatcherBase::OnIncomingMessage, base::Unretained(this))); |
| 49 reader_.StartReading(channel_.get(), | |
| 50 base::Bind(&ChannelDispatcherBase::OnIncomingMessage, | |
| 51 base::Unretained(this)), | |
| 52 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed, | |
| 53 base::Unretained(this))); | |
| 54 | 49 |
| 55 event_handler_->OnChannelInitialized(this); | 50 event_handler_->OnChannelInitialized(this); |
| 56 } | 51 } |
| 57 | 52 |
| 58 void ChannelDispatcherBase::OnReadWriteFailed(int error) { | 53 void ChannelDispatcherBase::OnPipeError(int error) { |
| 59 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR); | 54 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR); |
| 60 } | 55 } |
| 61 | 56 |
| 62 } // namespace protocol | 57 } // namespace protocol |
| 63 } // namespace remoting | 58 } // namespace remoting |
| OLD | NEW |