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_host.h" | 5 #include "remoting/protocol/connection_to_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 pp::Instance* pp_instance, | 29 pp::Instance* pp_instance, |
30 bool allow_nat_traversal) | 30 bool allow_nat_traversal) |
31 : message_loop_(message_loop), | 31 : message_loop_(message_loop), |
32 pp_instance_(pp_instance), | 32 pp_instance_(pp_instance), |
33 allow_nat_traversal_(allow_nat_traversal), | 33 allow_nat_traversal_(allow_nat_traversal), |
34 event_callback_(NULL), | 34 event_callback_(NULL), |
35 client_stub_(NULL), | 35 client_stub_(NULL), |
36 video_stub_(NULL), | 36 video_stub_(NULL), |
37 state_(CONNECTING), | 37 state_(CONNECTING), |
38 error_(OK), | 38 error_(OK), |
39 control_connected_(false), | 39 control_channel_connected_(false), |
40 input_connected_(false), | 40 event_channel_connected_(false), |
41 video_connected_(false) { | 41 video_channel_connected_(false) { |
42 } | 42 } |
43 | 43 |
44 ConnectionToHost::~ConnectionToHost() { | 44 ConnectionToHost::~ConnectionToHost() { |
45 } | 45 } |
46 | 46 |
47 InputStub* ConnectionToHost::input_stub() { | 47 InputStub* ConnectionToHost::input_stub() { |
48 return input_dispatcher_.get(); | 48 return event_dispatcher_.get(); |
49 } | 49 } |
50 | 50 |
51 HostStub* ConnectionToHost::host_stub() { | 51 HostStub* ConnectionToHost::host_stub() { |
52 return control_dispatcher_.get(); | 52 return control_dispatcher_.get(); |
53 } | 53 } |
54 | 54 |
55 void ConnectionToHost::Connect(scoped_refptr<XmppProxy> xmpp_proxy, | 55 void ConnectionToHost::Connect(scoped_refptr<XmppProxy> xmpp_proxy, |
56 const std::string& your_jid, | 56 const std::string& your_jid, |
57 const std::string& host_jid, | 57 const std::string& host_jid, |
58 const std::string& host_public_key, | 58 const std::string& host_public_key, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 CloseOnError(NETWORK_FAILURE); | 183 CloseOnError(NETWORK_FAILURE); |
184 } | 184 } |
185 break; | 185 break; |
186 | 186 |
187 case Session::CLOSED: | 187 case Session::CLOSED: |
188 CloseChannels(); | 188 CloseChannels(); |
189 SetState(CLOSED, OK); | 189 SetState(CLOSED, OK); |
190 break; | 190 break; |
191 | 191 |
192 case Session::CONNECTED: | 192 case Session::CONNECTED: |
193 video_reader_.reset( | 193 video_reader_.reset(VideoReader::Create( |
194 VideoReader::Create(message_loop_, session_->config())); | 194 message_loop_, session_->config())); |
195 video_reader_->Init( | 195 video_reader_->Init(session_.get(), video_stub_, base::Bind( |
196 session_.get(), video_stub_, | 196 &ConnectionToHost::OnVideoChannelInitialized, |
197 base::Bind(&ConnectionToHost::OnVideoChannelInitialized, | 197 base::Unretained(this))); |
198 base::Unretained(this))); | |
199 break; | |
200 | 198 |
201 case Session::CONNECTED_CHANNELS: | |
202 control_dispatcher_.reset(new ClientControlDispatcher()); | 199 control_dispatcher_.reset(new ClientControlDispatcher()); |
203 control_dispatcher_->Init(session_.get()); | 200 control_dispatcher_->Init(session_.get(), base::Bind( |
| 201 &ConnectionToHost::OnControlChannelInitialized, |
| 202 base::Unretained(this))); |
204 control_dispatcher_->set_client_stub(client_stub_); | 203 control_dispatcher_->set_client_stub(client_stub_); |
205 input_dispatcher_.reset(new ClientEventDispatcher()); | |
206 input_dispatcher_->Init(session_.get()); | |
207 | 204 |
208 control_connected_ = true; | 205 event_dispatcher_.reset(new ClientEventDispatcher()); |
209 input_connected_ = true; | 206 event_dispatcher_->Init(session_.get(), base::Bind( |
210 NotifyIfChannelsReady(); | 207 &ConnectionToHost::OnEventChannelInitialized, |
| 208 base::Unretained(this))); |
211 break; | 209 break; |
212 | 210 |
213 default: | 211 default: |
214 // Ignore the other states by default. | 212 // Ignore the other states by default. |
215 break; | 213 break; |
216 } | 214 } |
217 } | 215 } |
218 | 216 |
| 217 void ConnectionToHost::OnControlChannelInitialized(bool successful) { |
| 218 if (!successful) { |
| 219 LOG(ERROR) << "Failed to connect video channel"; |
| 220 CloseOnError(NETWORK_FAILURE); |
| 221 return; |
| 222 } |
| 223 |
| 224 control_channel_connected_ = true; |
| 225 NotifyIfChannelsReady(); |
| 226 } |
| 227 |
| 228 |
| 229 void ConnectionToHost::OnEventChannelInitialized(bool successful) { |
| 230 if (!successful) { |
| 231 LOG(ERROR) << "Failed to connect video channel"; |
| 232 CloseOnError(NETWORK_FAILURE); |
| 233 return; |
| 234 } |
| 235 |
| 236 event_channel_connected_ = true; |
| 237 NotifyIfChannelsReady(); |
| 238 } |
| 239 |
| 240 |
219 void ConnectionToHost::OnVideoChannelInitialized(bool successful) { | 241 void ConnectionToHost::OnVideoChannelInitialized(bool successful) { |
220 if (!successful) { | 242 if (!successful) { |
221 LOG(ERROR) << "Failed to connect video channel"; | 243 LOG(ERROR) << "Failed to connect video channel"; |
222 CloseOnError(NETWORK_FAILURE); | 244 CloseOnError(NETWORK_FAILURE); |
223 return; | 245 return; |
224 } | 246 } |
225 | 247 |
226 video_connected_ = true; | 248 video_channel_connected_ = true; |
227 NotifyIfChannelsReady(); | 249 NotifyIfChannelsReady(); |
228 } | 250 } |
229 | 251 |
230 void ConnectionToHost::NotifyIfChannelsReady() { | 252 void ConnectionToHost::NotifyIfChannelsReady() { |
231 if (control_connected_ && input_connected_ && video_connected_ && | 253 if (control_channel_connected_ && event_channel_connected_ && |
232 state_ == CONNECTING) { | 254 video_channel_connected_ && state_ == CONNECTING) { |
233 SetState(CONNECTED, OK); | 255 SetState(CONNECTED, OK); |
234 SetState(AUTHENTICATED, OK); | 256 SetState(AUTHENTICATED, OK); |
235 } | 257 } |
236 } | 258 } |
237 | 259 |
238 void ConnectionToHost::CloseOnError(Error error) { | 260 void ConnectionToHost::CloseOnError(Error error) { |
239 CloseChannels(); | 261 CloseChannels(); |
240 SetState(FAILED, error); | 262 SetState(FAILED, error); |
241 } | 263 } |
242 | 264 |
243 void ConnectionToHost::CloseChannels() { | 265 void ConnectionToHost::CloseChannels() { |
244 control_dispatcher_.reset(); | 266 control_dispatcher_.reset(); |
245 input_dispatcher_.reset(); | 267 event_dispatcher_.reset(); |
246 video_reader_.reset(); | 268 video_reader_.reset(); |
247 } | 269 } |
248 | 270 |
249 void ConnectionToHost::SetState(State state, Error error) { | 271 void ConnectionToHost::SetState(State state, Error error) { |
250 DCHECK(message_loop_->BelongsToCurrentThread()); | 272 DCHECK(message_loop_->BelongsToCurrentThread()); |
251 // |error| should be specified only when |state| is set to FAILED. | 273 // |error| should be specified only when |state| is set to FAILED. |
252 DCHECK(state == FAILED || error == OK); | 274 DCHECK(state == FAILED || error == OK); |
253 | 275 |
254 if (state != state_) { | 276 if (state != state_) { |
255 state_ = state; | 277 state_ = state; |
256 error_ = error; | 278 error_ = error; |
257 event_callback_->OnConnectionState(state_, error_); | 279 event_callback_->OnConnectionState(state_, error_); |
258 } | 280 } |
259 } | 281 } |
260 | 282 |
261 } // namespace protocol | 283 } // namespace protocol |
262 } // namespace remoting | 284 } // namespace remoting |
OLD | NEW |