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), | 25 control_channel_connected_(false), |
26 input_connected_(false), | 26 event_channel_connected_(false), |
27 video_connected_(false) { | 27 video_channel_connected_(false) { |
28 session_->SetStateChangeCallback( | 28 session_->SetStateChangeCallback( |
29 base::Bind(&ConnectionToClient::OnSessionStateChange, | 29 base::Bind(&ConnectionToClient::OnSessionStateChange, |
30 base::Unretained(this))); | 30 base::Unretained(this))); |
31 } | 31 } |
32 | 32 |
33 ConnectionToClient::~ConnectionToClient() { | 33 ConnectionToClient::~ConnectionToClient() { |
34 if (session_.get()) { | 34 if (session_.get()) { |
35 base::MessageLoopProxy::current()->DeleteSoon( | 35 base::MessageLoopProxy::current()->DeleteSoon( |
36 FROM_HERE, session_.release()); | 36 FROM_HERE, session_.release()); |
37 } | 37 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { | 94 void ConnectionToClient::OnSessionStateChange(protocol::Session::State state) { |
95 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
96 | 96 |
97 DCHECK(handler_); | 97 DCHECK(handler_); |
98 switch(state) { | 98 switch(state) { |
99 case protocol::Session::CONNECTING: | 99 case protocol::Session::CONNECTING: |
100 // Don't care about this message. | 100 // Don't care about this message. |
101 break; | 101 break; |
102 | 102 |
103 case protocol::Session::CONNECTED: | 103 case protocol::Session::CONNECTED: |
104 video_writer_.reset( | 104 // Initialize channels. |
105 VideoWriter::Create(base::MessageLoopProxy::current(), | 105 control_dispatcher_.reset(new HostControlDispatcher()); |
106 session_->config())); | 106 control_dispatcher_->Init(session_.get(), base::Bind( |
107 video_writer_->Init( | 107 &ConnectionToClient::OnControlChannelInitialized, |
108 session_.get(), base::Bind(&ConnectionToClient::OnVideoInitialized, | 108 base::Unretained(this))); |
109 base::Unretained(this))); | 109 control_dispatcher_->set_host_stub(host_stub_); |
110 break; | |
111 | 110 |
112 case protocol::Session::CONNECTED_CHANNELS: | 111 event_dispatcher_.reset(new HostEventDispatcher()); |
113 control_dispatcher_.reset(new HostControlDispatcher()); | 112 event_dispatcher_->Init(session_.get(), base::Bind( |
114 control_dispatcher_->Init(session_.get()); | 113 &ConnectionToClient::OnEventChannelInitialized, |
115 control_dispatcher_->set_host_stub(host_stub_); | 114 base::Unretained(this))); |
116 input_dispatcher_.reset(new HostEventDispatcher()); | 115 event_dispatcher_->set_input_stub(input_stub_); |
117 input_dispatcher_->Init(session_.get()); | 116 event_dispatcher_->set_sequence_number_callback(base::Bind( |
118 input_dispatcher_->set_input_stub(input_stub_); | |
119 input_dispatcher_->set_sequence_number_callback(base::Bind( | |
120 &ConnectionToClient::UpdateSequenceNumber, base::Unretained(this))); | 117 &ConnectionToClient::UpdateSequenceNumber, base::Unretained(this))); |
121 | 118 |
122 control_connected_ = true; | 119 video_writer_.reset(VideoWriter::Create( |
123 input_connected_ = true; | 120 base::MessageLoopProxy::current(), session_->config())); |
124 NotifyIfChannelsReady(); | 121 video_writer_->Init(session_.get(), base::Bind( |
| 122 &ConnectionToClient::OnVideoChannelInitialized, |
| 123 base::Unretained(this))); |
| 124 |
125 break; | 125 break; |
126 | 126 |
127 case protocol::Session::CLOSED: | 127 case protocol::Session::CLOSED: |
128 CloseChannels(); | 128 CloseChannels(); |
129 handler_->OnConnectionClosed(this); | 129 handler_->OnConnectionClosed(this); |
130 break; | 130 break; |
131 | 131 |
132 case protocol::Session::FAILED: | 132 case protocol::Session::FAILED: |
133 CloseOnError(); | 133 CloseOnError(); |
134 break; | 134 break; |
135 | 135 |
136 default: | 136 default: |
137 // We shouldn't receive other states. | 137 // We shouldn't receive other states. |
138 NOTREACHED(); | 138 NOTREACHED(); |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
142 void ConnectionToClient::OnVideoInitialized(bool successful) { | 142 void ConnectionToClient::OnControlChannelInitialized(bool successful) { |
143 DCHECK(CalledOnValidThread()); | 143 DCHECK(CalledOnValidThread()); |
144 | 144 |
145 if (!successful) { | 145 if (!successful) { |
| 146 LOG(ERROR) << "Failed to connect control channel"; |
| 147 CloseOnError(); |
| 148 return; |
| 149 } |
| 150 |
| 151 control_channel_connected_ = true; |
| 152 NotifyIfChannelsReady(); |
| 153 } |
| 154 |
| 155 void ConnectionToClient::OnEventChannelInitialized(bool successful) { |
| 156 DCHECK(CalledOnValidThread()); |
| 157 |
| 158 if (!successful) { |
| 159 LOG(ERROR) << "Failed to connect event channel"; |
| 160 CloseOnError(); |
| 161 return; |
| 162 } |
| 163 |
| 164 event_channel_connected_ = true; |
| 165 NotifyIfChannelsReady(); |
| 166 } |
| 167 |
| 168 void ConnectionToClient::OnVideoChannelInitialized(bool successful) { |
| 169 DCHECK(CalledOnValidThread()); |
| 170 |
| 171 if (!successful) { |
146 LOG(ERROR) << "Failed to connect video channel"; | 172 LOG(ERROR) << "Failed to connect video channel"; |
147 CloseOnError(); | 173 CloseOnError(); |
148 return; | 174 return; |
149 } | 175 } |
150 | 176 |
151 video_connected_ = true; | 177 video_channel_connected_ = true; |
152 NotifyIfChannelsReady(); | 178 NotifyIfChannelsReady(); |
153 } | 179 } |
154 | 180 |
155 void ConnectionToClient::NotifyIfChannelsReady() { | 181 void ConnectionToClient::NotifyIfChannelsReady() { |
156 DCHECK(CalledOnValidThread()); | 182 DCHECK(CalledOnValidThread()); |
157 | 183 |
158 if (control_connected_ && input_connected_ && video_connected_) | 184 if (control_channel_connected_ && event_channel_connected_ && |
| 185 video_channel_connected_) { |
159 handler_->OnConnectionOpened(this); | 186 handler_->OnConnectionOpened(this); |
| 187 } |
160 } | 188 } |
161 | 189 |
162 void ConnectionToClient::CloseOnError() { | 190 void ConnectionToClient::CloseOnError() { |
163 CloseChannels(); | 191 CloseChannels(); |
164 handler_->OnConnectionFailed(this); | 192 handler_->OnConnectionFailed(this); |
165 } | 193 } |
166 | 194 |
167 void ConnectionToClient::CloseChannels() { | 195 void ConnectionToClient::CloseChannels() { |
168 control_dispatcher_.reset(); | 196 control_dispatcher_.reset(); |
169 input_dispatcher_.reset(); | 197 event_dispatcher_.reset(); |
170 video_writer_.reset(); | 198 video_writer_.reset(); |
171 } | 199 } |
172 | 200 |
173 } // namespace protocol | 201 } // namespace protocol |
174 } // namespace remoting | 202 } // namespace remoting |
OLD | NEW |