| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/connection_to_client.h" | 5 #include "remoting/protocol/connection_to_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "google/protobuf/message.h" | 10 #include "google/protobuf/message.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "remoting/protocol/host_control_dispatcher.h" | 12 #include "remoting/protocol/host_control_dispatcher.h" |
| 13 #include "remoting/protocol/host_event_dispatcher.h" | 13 #include "remoting/protocol/host_event_dispatcher.h" |
| 14 #include "remoting/protocol/host_stub.h" | 14 #include "remoting/protocol/host_stub.h" |
| 15 #include "remoting/protocol/input_stub.h" | 15 #include "remoting/protocol/input_stub.h" |
| 16 | 16 |
| 17 namespace remoting { | 17 namespace remoting { |
| 18 namespace protocol { | 18 namespace protocol { |
| 19 | 19 |
| 20 ConnectionToClient::ConnectionToClient(protocol::Session* session) | 20 ConnectionToClient::ConnectionToClient(protocol::Session* session) |
| 21 : handler_(NULL), | 21 : handler_(NULL), |
| 22 host_stub_(NULL), | 22 host_stub_(NULL), |
| 23 input_stub_(NULL), | 23 input_stub_(NULL), |
| 24 session_(session), | 24 session_(session) { |
| 25 control_connected_(false), | |
| 26 input_connected_(false), | |
| 27 video_connected_(false) { | |
| 28 session_->SetStateChangeCallback( | 25 session_->SetStateChangeCallback( |
| 29 base::Bind(&ConnectionToClient::OnSessionStateChange, | 26 base::Bind(&ConnectionToClient::OnSessionStateChange, |
| 30 base::Unretained(this))); | 27 base::Unretained(this))); |
| 31 } | 28 } |
| 32 | 29 |
| 33 ConnectionToClient::~ConnectionToClient() { | 30 ConnectionToClient::~ConnectionToClient() { |
| 34 if (session_.get()) { | 31 if (session_.get()) { |
| 35 base::MessageLoopProxy::current()->DeleteSoon( | 32 base::MessageLoopProxy::current()->DeleteSoon( |
| 36 FROM_HERE, session_.release()); | 33 FROM_HERE, session_.release()); |
| 37 } | 34 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { | 91 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { |
| 95 DCHECK(CalledOnValidThread()); | 92 DCHECK(CalledOnValidThread()); |
| 96 | 93 |
| 97 DCHECK(handler_); | 94 DCHECK(handler_); |
| 98 switch(state) { | 95 switch(state) { |
| 99 case protocol::Session::CONNECTING: | 96 case protocol::Session::CONNECTING: |
| 100 // Don't care about this message. | 97 // Don't care about this message. |
| 101 break; | 98 break; |
| 102 | 99 |
| 103 case protocol::Session::CONNECTED: | 100 case protocol::Session::CONNECTED: |
| 104 video_writer_.reset( | 101 // Initialize channels. |
| 105 VideoWriter::Create(base::MessageLoopProxy::current(), | 102 control_dispatcher_.reset(new HostControlDispatcher()); |
| 106 session_->config())); | 103 control_dispatcher_->Init(session_.get(), base::Bind( |
| 107 video_writer_->Init( | 104 &ConnectionToClient::OnChannelInitialized, base::Unretained(this))); |
| 108 session_.get(), base::Bind(&ConnectionToClient::OnVideoInitialized, | 105 control_dispatcher_->set_host_stub(host_stub_); |
| 109 base::Unretained(this))); | |
| 110 break; | |
| 111 | 106 |
| 112 case protocol::Session::CONNECTED_CHANNELS: | 107 event_dispatcher_.reset(new HostEventDispatcher()); |
| 113 control_dispatcher_.reset(new HostControlDispatcher()); | 108 event_dispatcher_->Init(session_.get(), base::Bind( |
| 114 control_dispatcher_->Init(session_.get()); | 109 &ConnectionToClient::OnChannelInitialized, base::Unretained(this))); |
| 115 control_dispatcher_->set_host_stub(host_stub_); | 110 event_dispatcher_->set_input_stub(input_stub_); |
| 116 input_dispatcher_.reset(new HostEventDispatcher()); | 111 event_dispatcher_->set_sequence_number_callback(base::Bind( |
| 117 input_dispatcher_->Init(session_.get()); | |
| 118 input_dispatcher_->set_input_stub(input_stub_); | |
| 119 input_dispatcher_->set_sequence_number_callback(base::Bind( | |
| 120 &ConnectionToClient::UpdateSequenceNumber, base::Unretained(this))); | 112 &ConnectionToClient::UpdateSequenceNumber, base::Unretained(this))); |
| 121 | 113 |
| 122 control_connected_ = true; | 114 video_writer_.reset(VideoWriter::Create( |
| 123 input_connected_ = true; | 115 base::MessageLoopProxy::current(), session_->config())); |
| 124 NotifyIfChannelsReady(); | 116 video_writer_->Init(session_.get(), base::Bind( |
| 117 &ConnectionToClient::OnChannelInitialized, base::Unretained(this))); |
| 118 |
| 125 break; | 119 break; |
| 126 | 120 |
| 127 case protocol::Session::CLOSED: | 121 case protocol::Session::CLOSED: |
| 128 CloseChannels(); | 122 CloseChannels(); |
| 129 handler_->OnConnectionClosed(this); | 123 handler_->OnConnectionClosed(this); |
| 130 break; | 124 break; |
| 131 | 125 |
| 132 case protocol::Session::FAILED: | 126 case protocol::Session::FAILED: |
| 133 CloseOnError(); | 127 CloseOnError(); |
| 134 break; | 128 break; |
| 135 | 129 |
| 136 default: | 130 default: |
| 137 // We shouldn't receive other states. | 131 // We shouldn't receive other states. |
| 138 NOTREACHED(); | 132 NOTREACHED(); |
| 139 } | 133 } |
| 140 } | 134 } |
| 141 | 135 |
| 142 void ConnectionToClient::OnVideoInitialized(bool successful) { | 136 void ConnectionToClient::OnChannelInitialized(bool successful) { |
| 143 DCHECK(CalledOnValidThread()); | 137 DCHECK(CalledOnValidThread()); |
| 144 | 138 |
| 145 if (!successful) { | 139 if (!successful) { |
| 146 LOG(ERROR) << "Failed to connect video channel"; | 140 LOG(ERROR) << "Failed to connect a channel"; |
| 147 CloseOnError(); | 141 CloseOnError(); |
| 148 return; | 142 return; |
| 149 } | 143 } |
| 150 | 144 |
| 151 video_connected_ = true; | |
| 152 NotifyIfChannelsReady(); | 145 NotifyIfChannelsReady(); |
| 153 } | 146 } |
| 154 | 147 |
| 155 void ConnectionToClient::NotifyIfChannelsReady() { | 148 void ConnectionToClient::NotifyIfChannelsReady() { |
| 156 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
| 157 | 150 |
| 158 if (control_connected_ && input_connected_ && video_connected_) | 151 if (control_dispatcher_.get() && control_dispatcher_->is_connected() && |
| 152 event_dispatcher_.get() && event_dispatcher_->is_connected() && |
| 153 video_writer_.get() && video_writer_->is_connected()) { |
| 159 handler_->OnConnectionOpened(this); | 154 handler_->OnConnectionOpened(this); |
| 155 } |
| 160 } | 156 } |
| 161 | 157 |
| 162 void ConnectionToClient::CloseOnError() { | 158 void ConnectionToClient::CloseOnError() { |
| 163 CloseChannels(); | 159 CloseChannels(); |
| 164 handler_->OnConnectionFailed(this); | 160 handler_->OnConnectionFailed(this); |
| 165 } | 161 } |
| 166 | 162 |
| 167 void ConnectionToClient::CloseChannels() { | 163 void ConnectionToClient::CloseChannels() { |
| 168 control_dispatcher_.reset(); | 164 control_dispatcher_.reset(); |
| 169 input_dispatcher_.reset(); | 165 event_dispatcher_.reset(); |
| 170 video_writer_.reset(); | 166 video_writer_.reset(); |
| 171 } | 167 } |
| 172 | 168 |
| 173 } // namespace protocol | 169 } // namespace protocol |
| 174 } // namespace remoting | 170 } // namespace remoting |
| OLD | NEW |