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/base/compound_buffer.h" |
11 #include "remoting/protocol/stream_channel_factory.h" | 11 #include "remoting/protocol/message_channel_factory.h" |
12 #include "remoting/protocol/stream_message_pipe_adapter.h" | 12 #include "remoting/protocol/message_pipe.h" |
13 | 13 |
14 namespace remoting { | 14 namespace remoting { |
15 namespace protocol { | 15 namespace protocol { |
16 | 16 |
17 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name) | 17 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name) |
18 : channel_name_(channel_name), | 18 : channel_name_(channel_name) {} |
19 channel_factory_(nullptr), | |
20 event_handler_(nullptr) {} | |
21 | 19 |
22 ChannelDispatcherBase::~ChannelDispatcherBase() { | 20 ChannelDispatcherBase::~ChannelDispatcherBase() { |
23 if (channel_factory_) | 21 if (channel_factory_) |
24 channel_factory_->CancelChannelCreation(channel_name_); | 22 channel_factory_->CancelChannelCreation(channel_name_); |
25 } | 23 } |
26 | 24 |
27 void ChannelDispatcherBase::Init(StreamChannelFactory* channel_factory, | 25 void ChannelDispatcherBase::Init(MessageChannelFactory* channel_factory, |
28 EventHandler* event_handler) { | 26 EventHandler* event_handler) { |
29 channel_factory_ = channel_factory; | 27 channel_factory_ = channel_factory; |
30 event_handler_ = event_handler; | 28 event_handler_ = event_handler; |
31 | 29 |
32 channel_factory_->CreateChannel(channel_name_, base::Bind( | 30 channel_factory_->CreateChannel(channel_name_, base::Bind( |
33 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this))); | 31 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this))); |
34 } | 32 } |
35 | 33 |
36 void ChannelDispatcherBase::OnChannelReady( | 34 void ChannelDispatcherBase::OnChannelReady( |
37 scoped_ptr<P2PStreamSocket> socket) { | 35 scoped_ptr<MessagePipe> message_pipe) { |
38 if (!socket.get()) { | |
39 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR); | |
40 return; | |
41 } | |
42 | |
43 channel_factory_ = nullptr; | 36 channel_factory_ = nullptr; |
44 message_pipe_.reset(new StreamMessagePipeAdapter( | 37 message_pipe_ = std::move(message_pipe); |
45 std::move(socket), | |
46 base::Bind(&ChannelDispatcherBase::OnPipeError, base::Unretained(this)))); | |
47 message_pipe_->StartReceiving(base::Bind( | 38 message_pipe_->StartReceiving(base::Bind( |
48 &ChannelDispatcherBase::OnIncomingMessage, base::Unretained(this))); | 39 &ChannelDispatcherBase::OnIncomingMessage, base::Unretained(this))); |
49 | 40 |
50 event_handler_->OnChannelInitialized(this); | 41 event_handler_->OnChannelInitialized(this); |
51 } | 42 } |
52 | 43 |
53 void ChannelDispatcherBase::OnPipeError(int error) { | |
54 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR); | |
55 } | |
56 | |
57 } // namespace protocol | 44 } // namespace protocol |
58 } // namespace remoting | 45 } // namespace remoting |
OLD | NEW |