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 "google/protobuf/message.h" | 8 #include "google/protobuf/message.h" |
8 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
9 #include "remoting/protocol/client_control_sender.h" | 10 #include "remoting/protocol/client_control_sender.h" |
10 #include "remoting/protocol/host_message_dispatcher.h" | 11 #include "remoting/protocol/host_message_dispatcher.h" |
11 #include "remoting/protocol/host_stub.h" | 12 #include "remoting/protocol/host_stub.h" |
12 #include "remoting/protocol/input_stub.h" | 13 #include "remoting/protocol/input_stub.h" |
13 | 14 |
14 // TODO(hclam): Remove this header once MessageDispatcher is used. | 15 // TODO(hclam): Remove this header once MessageDispatcher is used. |
15 #include "remoting/base/compound_buffer.h" | 16 #include "remoting/base/compound_buffer.h" |
16 | 17 |
17 namespace remoting { | 18 namespace remoting { |
18 namespace protocol { | 19 namespace protocol { |
19 | 20 |
20 // Determine how many update streams we should count to find the size of | 21 // Determine how many update streams we should count to find the size of |
21 // average update stream. | 22 // average update stream. |
22 static const size_t kAverageUpdateStream = 10; | 23 static const size_t kAverageUpdateStream = 10; |
23 | 24 |
24 ConnectionToClient::ConnectionToClient(MessageLoop* message_loop, | 25 ConnectionToClient::ConnectionToClient(MessageLoop* message_loop, |
25 EventHandler* handler) | 26 EventHandler* handler) |
26 : loop_(message_loop), | 27 : loop_(message_loop), |
27 handler_(handler), | 28 handler_(handler), |
28 host_stub_(NULL), | 29 host_stub_(NULL), |
29 input_stub_(NULL) { | 30 input_stub_(NULL), |
| 31 control_connected_(false), |
| 32 input_connected_(false), |
| 33 video_connected_(false) { |
30 DCHECK(loop_); | 34 DCHECK(loop_); |
31 DCHECK(handler_); | 35 DCHECK(handler_); |
32 } | 36 } |
33 | 37 |
34 ConnectionToClient::~ConnectionToClient() { | 38 ConnectionToClient::~ConnectionToClient() { |
35 // TODO(hclam): When we shut down the viewer we may have to close the | 39 // TODO(hclam): When we shut down the viewer we may have to close the |
36 // connection. | 40 // connection. |
37 } | 41 } |
38 | 42 |
39 void ConnectionToClient::Init(protocol::Session* session) { | 43 void ConnectionToClient::Init(protocol::Session* session) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { | 87 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { |
84 input_stub_ = input_stub; | 88 input_stub_ = input_stub; |
85 } | 89 } |
86 | 90 |
87 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { | 91 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { |
88 DCHECK_EQ(loop_, MessageLoop::current()); | 92 DCHECK_EQ(loop_, MessageLoop::current()); |
89 | 93 |
90 DCHECK(handler_); | 94 DCHECK(handler_); |
91 switch(state) { | 95 switch(state) { |
92 case protocol::Session::CONNECTING: | 96 case protocol::Session::CONNECTING: |
| 97 // Don't care about this message. |
93 break; | 98 break; |
94 // Don't care about this message. | 99 |
95 case protocol::Session::CONNECTED: | 100 case protocol::Session::CONNECTED: |
| 101 video_writer_.reset(VideoWriter::Create(session_->config())); |
| 102 video_writer_->Init( |
| 103 session_.get(), base::Bind(&ConnectionToClient::OnVideoInitialized, |
| 104 base::Unretained(this))); |
| 105 break; |
| 106 |
| 107 case protocol::Session::CONNECTED_CHANNELS: |
96 client_control_sender_.reset( | 108 client_control_sender_.reset( |
97 new ClientControlSender(session_->control_channel())); | 109 new ClientControlSender(session_->control_channel())); |
98 video_writer_.reset(VideoWriter::Create(session_->config())); | |
99 video_writer_->Init(session_.get()); | |
100 | |
101 dispatcher_.reset(new HostMessageDispatcher()); | 110 dispatcher_.reset(new HostMessageDispatcher()); |
102 dispatcher_->Initialize(this, host_stub_, input_stub_); | 111 dispatcher_->Initialize(this, host_stub_, input_stub_); |
103 | 112 |
104 handler_->OnConnectionOpened(this); | 113 control_connected_ = true; |
| 114 input_connected_ = true; |
| 115 NotifyIfChannelsReady(); |
105 break; | 116 break; |
| 117 |
106 case protocol::Session::CLOSED: | 118 case protocol::Session::CLOSED: |
107 CloseChannels(); | 119 CloseChannels(); |
108 handler_->OnConnectionClosed(this); | 120 handler_->OnConnectionClosed(this); |
109 break; | 121 break; |
| 122 |
110 case protocol::Session::FAILED: | 123 case protocol::Session::FAILED: |
111 CloseChannels(); | 124 CloseOnError(); |
112 handler_->OnConnectionFailed(this); | |
113 break; | 125 break; |
| 126 |
114 default: | 127 default: |
115 // We shouldn't receive other states. | 128 // We shouldn't receive other states. |
116 NOTREACHED(); | 129 NOTREACHED(); |
117 } | 130 } |
118 } | 131 } |
119 | 132 |
| 133 void ConnectionToClient::OnVideoInitialized(bool successful) { |
| 134 if (!successful) { |
| 135 LOG(ERROR) << "Failed to connect video channel"; |
| 136 CloseOnError(); |
| 137 return; |
| 138 } |
| 139 |
| 140 video_connected_ = true; |
| 141 NotifyIfChannelsReady(); |
| 142 } |
| 143 |
| 144 void ConnectionToClient::NotifyIfChannelsReady() { |
| 145 if (control_connected_ && input_connected_ && video_connected_) |
| 146 handler_->OnConnectionOpened(this); |
| 147 } |
| 148 |
| 149 void ConnectionToClient::CloseOnError() { |
| 150 CloseChannels(); |
| 151 handler_->OnConnectionFailed(this); |
| 152 } |
| 153 |
120 void ConnectionToClient::CloseChannels() { | 154 void ConnectionToClient::CloseChannels() { |
121 if (video_writer_.get()) | 155 if (video_writer_.get()) |
122 video_writer_->Close(); | 156 video_writer_->Close(); |
123 if (client_control_sender_.get()) | 157 if (client_control_sender_.get()) |
124 client_control_sender_->Close(); | 158 client_control_sender_->Close(); |
125 } | 159 } |
126 | 160 |
127 } // namespace protocol | 161 } // namespace protocol |
128 } // namespace remoting | 162 } // namespace remoting |
OLD | NEW |