| 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 "google/protobuf/message.h" | 7 #include "google/protobuf/message.h" |
| 8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
| 9 #include "remoting/protocol/client_control_sender.h" | 9 #include "remoting/protocol/client_control_sender.h" |
| 10 #include "remoting/protocol/host_message_dispatcher.h" | 10 #include "remoting/protocol/host_message_dispatcher.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 50 |
| 51 void ConnectionToClient::Disconnect() { | 51 void ConnectionToClient::Disconnect() { |
| 52 // This method can be called from main thread so perform threading switching. | 52 // This method can be called from main thread so perform threading switching. |
| 53 if (MessageLoop::current() != loop_) { | 53 if (MessageLoop::current() != loop_) { |
| 54 loop_->PostTask( | 54 loop_->PostTask( |
| 55 FROM_HERE, | 55 FROM_HERE, |
| 56 NewRunnableMethod(this, &ConnectionToClient::Disconnect)); | 56 NewRunnableMethod(this, &ConnectionToClient::Disconnect)); |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 | 59 |
| 60 CloseChannels(); |
| 61 |
| 60 // If there is a channel then close it and release the reference. | 62 // If there is a channel then close it and release the reference. |
| 61 if (session_) { | 63 if (session_) { |
| 62 session_->Close(NewRunnableMethod(this, &ConnectionToClient::OnClosed)); | 64 session_->Close(NewRunnableMethod(this, &ConnectionToClient::OnClosed)); |
| 63 session_ = NULL; | 65 session_ = NULL; |
| 64 } | 66 } |
| 65 } | 67 } |
| 66 | 68 |
| 67 void ConnectionToClient::UpdateSequenceNumber(int64 sequence_number) { | 69 void ConnectionToClient::UpdateSequenceNumber(int64 sequence_number) { |
| 68 handler_->OnSequenceNumberUpdated(this, sequence_number); | 70 handler_->OnSequenceNumberUpdated(this, sequence_number); |
| 69 } | 71 } |
| 70 | 72 |
| 71 VideoStub* ConnectionToClient::video_stub() { | 73 VideoStub* ConnectionToClient::video_stub() { |
| 72 return video_writer_.get(); | 74 return video_writer_.get(); |
| 73 } | 75 } |
| 74 | 76 |
| 75 // Return pointer to ClientStub. | 77 // Return pointer to ClientStub. |
| 76 ClientStub* ConnectionToClient::client_stub() { | 78 ClientStub* ConnectionToClient::client_stub() { |
| 77 return client_stub_.get(); | 79 return client_control_sender_.get(); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void ConnectionToClient::set_host_stub(protocol::HostStub* host_stub) { | 82 void ConnectionToClient::set_host_stub(protocol::HostStub* host_stub) { |
| 81 host_stub_ = host_stub; | 83 host_stub_ = host_stub; |
| 82 } | 84 } |
| 83 | 85 |
| 84 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { | 86 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { |
| 85 input_stub_ = input_stub; | 87 input_stub_ = input_stub; |
| 86 } | 88 } |
| 87 | 89 |
| 88 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { | 90 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { |
| 89 if (state == protocol::Session::CONNECTED) { | |
| 90 client_stub_.reset(new ClientControlSender(session_->control_channel())); | |
| 91 video_writer_.reset(VideoWriter::Create(session_->config())); | |
| 92 video_writer_->Init(session_); | |
| 93 | |
| 94 dispatcher_.reset(new HostMessageDispatcher()); | |
| 95 dispatcher_->Initialize(this, host_stub_, input_stub_); | |
| 96 } | |
| 97 | |
| 98 // This method can be called from main thread so perform threading switching. | |
| 99 if (MessageLoop::current() != loop_) { | |
| 100 loop_->PostTask( | |
| 101 FROM_HERE, | |
| 102 NewRunnableMethod(this, &ConnectionToClient::StateChangeTask, state)); | |
| 103 } else { | |
| 104 StateChangeTask(state); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void ConnectionToClient::StateChangeTask(protocol::Session::State state) { | |
| 109 DCHECK_EQ(loop_, MessageLoop::current()); | 91 DCHECK_EQ(loop_, MessageLoop::current()); |
| 110 | 92 |
| 111 DCHECK(handler_); | 93 DCHECK(handler_); |
| 112 switch(state) { | 94 switch(state) { |
| 113 case protocol::Session::CONNECTING: | 95 case protocol::Session::CONNECTING: |
| 114 break; | 96 break; |
| 115 // Don't care about this message. | 97 // Don't care about this message. |
| 116 case protocol::Session::CONNECTED: | 98 case protocol::Session::CONNECTED: |
| 99 client_control_sender_.reset( |
| 100 new ClientControlSender(session_->control_channel())); |
| 101 video_writer_.reset(VideoWriter::Create(session_->config())); |
| 102 video_writer_->Init(session_); |
| 103 |
| 104 dispatcher_.reset(new HostMessageDispatcher()); |
| 105 dispatcher_->Initialize(this, host_stub_, input_stub_); |
| 106 |
| 117 handler_->OnConnectionOpened(this); | 107 handler_->OnConnectionOpened(this); |
| 118 break; | 108 break; |
| 119 case protocol::Session::CLOSED: | 109 case protocol::Session::CLOSED: |
| 110 CloseChannels(); |
| 120 handler_->OnConnectionClosed(this); | 111 handler_->OnConnectionClosed(this); |
| 121 break; | 112 break; |
| 122 case protocol::Session::FAILED: | 113 case protocol::Session::FAILED: |
| 114 CloseChannels(); |
| 123 handler_->OnConnectionFailed(this); | 115 handler_->OnConnectionFailed(this); |
| 124 break; | 116 break; |
| 125 default: | 117 default: |
| 126 // We shouldn't receive other states. | 118 // We shouldn't receive other states. |
| 127 NOTREACHED(); | 119 NOTREACHED(); |
| 128 } | 120 } |
| 129 } | 121 } |
| 130 | 122 |
| 123 void ConnectionToClient::CloseChannels() { |
| 124 if (video_writer_.get()) |
| 125 video_writer_->Close(); |
| 126 if (client_control_sender_.get()) |
| 127 client_control_sender_->Close(); |
| 128 } |
| 129 |
| 131 // OnClosed() is used as a callback for protocol::Session::Close(). | 130 // OnClosed() is used as a callback for protocol::Session::Close(). |
| 132 void ConnectionToClient::OnClosed() { | 131 void ConnectionToClient::OnClosed() { |
| 133 } | 132 } |
| 134 | 133 |
| 135 } // namespace protocol | 134 } // namespace protocol |
| 136 } // namespace remoting | 135 } // namespace remoting |
| OLD | NEW |